Uploaded by Tyler Smith

It Pascal Notes

advertisement
A variable is a storage location for data that can change during the execution of a program
Rules for naming variable:
1. The name should not begin with a number or special character.
2. The name must not contain spaces(an underscore must be used)
3. They should be meaningful.
Types of Data**
-String - Groups of characters
-Char - a single character/symbol
-Integer - Negative/Positive numbers without decimal places
-Real - Negative/Positive numbers
Problem statement
Write an algorithm that will ask the user to enter three numbers. Calculate and print the average of the
three numbers.
Input:
Processing:
> num1
> average=(num1+num2+num3)/3 >average
> num2
> num3
Output:
Problem statement 2
Write an algorithm that will ask the user to enter the length of a square. Calculate and print the area of
the square
Input:
Processing:
Output:
>length >area= length * length >area
Pseudocode 2:
Start
Write "Please enter the length of a square"
Read L
Area= L * L
Write Area
Stop
Pseudocode1:
Start
Write "Please enter 3 numbers"
Read num1, num2,num3
average=(num1+num2+num3)/3
Write average
Stop
3/12/2024
Write an algorithm that will ask 50 persons to enter their name. Output a message telling each person
"Good Morning."
Pseudocode:
Start
Declare Name as String
y as integer
Name= ""
y= 0
For y = 1 to 50 do
Write "Please enter your name."
Read Name
Write "Good Morning “, Name
Endfor
Stop
Pascal
Program Goodmorning:
Uses CRT;
Var Name:string;
y: integer;
Begin
Name: = '';
y: = 0;
For y:= 1 to 50 do
Begin
Writeln ('Please enter a name');
Readln(Name);
Writeln ('Good morning ', Name);
End;
Readln;
End.
**If Statements**
If <Condition> then
<instruction to be carried out if the condition is true>
Endif
Eg**
Write "Please enter the hour"
Read hour
If hour=12 then
Write" It is lunch time"
Endif
**If then Else**
If <Condition> then
<instruction to be carried out if the condition is true>
Else
<instructions to ne carried out if the condition is false>
Endif
Pascal eg:
Program lunchtime;
Uses CRT;
Var hour:integer;
Begin
Hour:=0;
Writeln('Please enter the hour');
Readln(hour);
If hour=12 then
Begin
Writeln('It is lunch time');
End
Else
Begin
Writeln('It is not lunchtime')
End;
Readkey;
End.
**IF THEN ELSE IF**
If <Condition> then
<instruction to be carried out if the condition is true>
Else
If <Condition> then
<instruction to be carried out if the condition is true>
Endif
Endif
Start
Declare x as integer
area as char
Area_A,Area_B,Area_C,Area_D,T_rev as real
X=0
Area=""
Area_A=0
Area_B=0
Area_C=0
Area_D=0
T_rev=0
For x=1 to 100 do
Write "Please enter an area"
Read Area
If Area="A" then
Area_A= Area_A +1000
Else
If Area="B" then
Area_B= Area_B+1500
Else
If Area="C" then
Area_C= Area_C+2000
Else
If Area="D" then
Area_D= Area_D=4000
Else
Write "Wrong Area Entered."
Endif
Endif
Endif
Endif
Endfor
T_rev= Area_A + Area_B + Area_C + Area_D
Write "The Total collected for Area A is ", Area_A
Write "The Total collected for Area B is ", Area_B
Write "The Total collected for Area C is ", Area_C
Write "The Total collected for Area D is ", Area_D
Write "The Total revenue for the event ",T_rev
Stop
**Trace Table**
A= 2
B= 3
C= 1
While B < 45 do
A= A + B
B= B + A
C= C + B
Endwhile
Trace Table Example.
A
B
C
2
5
13
34
3
8
21
55
1
9
30
85
N
2
2
10
26
58
P
1
3
11
27
59
Question 1
M= 1
N= 2
P= 1
While M< 30 do
N= M * 2
P= N + 1
M= P + 2
M
1
5
13
29
61
Read N
For J = 1 to N do
If J<5 then
S= (N + J) * 2
Else
S= (N - J) * 2
Endif
Endfor
J
1
2
3
4
5
6
N
6
6
6
6
6
6
S
14
16
18
20
2
0
Download