Download presentation
Presentation is loading. Please wait.
Published byShavonne Fleming Modified over 9 years ago
1
Assignment statement and Arithmetic operation 1 The major part of data processing
2
Data processing As you should still remember, there are 3 major steps in data processing: FINISH START INPUTPROCESSINGOUTPUT readln???? writeln, write We have learnt INPUT commands And also the OUTPUT commands What about the PROCESSING?
3
Assignment statements n Assignment statement is one of the statements used in processing data n It allows us to: –store DATA in variables – COPY data between variables and –store CALCULATION RESULT in variables
4
How to use it When we want to STORE the data 4 in variable X, we can use the following statement: Put the TARGET X at the front Put the special signs ‘:= ‘ next Put the DATA 4 at last
5
How to use it When we want to COPY the data stored in variable X into the variable Y, we can use the following statement: Put the TARGET Y at the front Put the special sign ‘ := ‘ next Put the SOURCE X at last
6
How to use it When we want to Store the sum of the values in A and B into Y, we can use the following statement: Put the TARGET Y at the front Put the special sign ‘:= ‘ next Put the FORMULA A+B at last
7
000 How does it work? program ARITHMETIC(input,output); varX, Y, Z : integer; begin X := 4; Y := X; Z := X + Y; writeln(X, Y, Z); end. Let’s consider the following example: 448 Store the data 4 into X Copy the content of X into Y Put the sum of X and Y in Z What is the output?
8
program ARITHMETIC(input,output); varX, Y, Z : integer; begin X := 4; Y := X; Z := X + Y; writeln(X,’+’,Y,’=’,Z); end. 4+4=8 This is the output
9
program AREA(input,output); varX, Y, Z : integer; begin X := 4; Y := 7; Z := X * Y; writeln(‘The area is ‘,Z); end. The area is 28 What is the output? This is the output
10
A, B, C are variables of integer type. What do the following statements do? 1. A:=B+C; 2. A+B:=A+B; 3. 3:=A+B; 4. C:=(A+B+C)/3; 5. A+3:=3+A; 6. B:=B; 7. C:= C + 1; Store the sum of B and C into A INVALID Compute the average of A, B and C, then store it in C (over writing the old value) INVALID Store the value in B back to B Store the sum of C and 1 back to C Increase the value in C by 1
11
program INCREMENT(input,output); varAGE : integer; begin AGE := 14; writeln( ‘ I am ‘,AGE, years old. ’ ); AGE := AGE + 1; writeln( ‘ I will be ‘,AGE, years old ’ ); writeln( ‘ next year. ’ ); end. I am 14 years old. I will be 15 years old next year. What is the output? This is the output
12
0 How does it work? 14 program INCREMENT(input,output); varAGE : integer; begin AGE := 14; writeln( ‘ I am ‘,AGE, ’ years old. ’ ); AGE := AGE + 1; writeln( ‘ I will be ‘,AGE, ’ years old ’ ); writeln( ‘ next year. ’ ); end. 15 Store the data 14 into AGE Display the first message Put the sum of AGE and 1 in AGE Display the second message. Note that the value of AGE has changed.
13
program INCREMENT2(input,output); varAGE : integer; begin write( ‘ Please input your age: ’ ); readln(AGE); writeln( ‘ You are ‘,AGE, years old. ’ ); AGE := AGE + 1; writeln( ‘ You will be ‘,AGE, years old ’ ); writeln( ‘ next year. ’ ); end. Please input your age:20 You are 20 years old. You will be 21 years old next year. What is the output if the input is 20? This is the output
14
Task 1. Write a program which asks the user to input TWO numbers and display the sum of them. Here is the sample output: Please input the first number : 21 Please input the second number: 11 The sum of them is : 32
15
Task 2. Write a program which asks the user to input THREE numbers and display the mean of them. Here is the sample output: Please input the first number : 5 Please input the second number: 13 Please input the third number: 9 The sum of them is : 9
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.