There is another type of variable in pascal called boolean. It can have only two values, true or false. A statement which results in a true or false value is a boolean expression. ex: n = 8 results in a true value for the expression if n equalto 8. Note the equalto (=) sign in the expression which is different from assignment sign(:=).
Pascal provides language structures to control conditional processing. They are as follows:
1. if --condition-- then --statement1-- else --statement2--;
condition could be a = 10 or boolean. Notice the equal(=) sign in the condition.
Statement1 may not have ';' at the end, if it is followed by else. If it has results could be what you do not expect.
2. while --condition-- do --statement;
As long as the While condition is satisfied the statement processing continues. As such the condition should be changed in the statement otherwise the processing continues for ever.
3. Repeat ---- statements ---- until ---- boolean expression ----;
As noted in While the boolean expression value should change sometime or other, otherwise processing continues for ever.
4. Case --variable-- of --vales of variable-- : statement1; otherwise statement2; end;
ex: case n of 2,3,6 : writeln('value of n is ', n); otherwise writeln('end of case'); end;
For values of n equal to 2,3 or 6 write (value of n is) otherwise write (end of case).
Program depicting the conditional statements follows:
Program program5;
{ Author: Rao S Lakkaraju }
{ This is my fifth pascal program }
(* Condition Processing in Pascal *)
uses crt,graph;
var n,number1, number2,number3 : integer;
interest1, interest2,interest3 : real;
name1, name2, name3 : string;
rich : boolean;
begin
clrscr;
number1 := 1;
number2 := 10;
interest1 := 2.5;
interest2 := 5.4;
name1 := 'Rao';
name2 := ' Lakkaraju';
(**** Lesson 4 *****)
for number3 := number1 to number2 do write(number3);
write(' ');
for number3 := number2 downto number1 do begin write(number3); end;
writeln(' ');
name3 := name1 + name2;
interest3 := interest1 + interest2;
Writeln('number1 = ', number1);
writeln('number2 = ', number2);
writeln('number3 = ', number3);
writeln('interest1 = ', interest1, ' interest2 = ', interest2);
writeln('interest3 = ', interest3);
writeln('My Name is ', name3);
(**** Lesson 5 *****)
rich := false;
if not rich then writeln('I am not rich ')
else writeln('I am rich');
while rich = false do rich := true;
n := 1;
repeat n := n + 1; Writeln('I am in repeat'); until n = 8;
n := 2;
case n of 2,3,6 : writeln('value of n is ', n);
otherwise writeln('end of case'); end;
writeln('My fifth Pascal Program');
readln;
end.
Summary: Pascal provides a variable called boolean which could have only two values true or false. A boolean expression is one which after evaluation results in a boolean true or false value . Pascal provides statement execution depending on the value of a boolean expression. These pascal structures are if, while, repeat and case.
Showing posts with label Pascal Programming tutor mypascal condition processing. Show all posts
Showing posts with label Pascal Programming tutor mypascal condition processing. Show all posts
Friday, October 23, 2009
Subscribe to:
Posts (Atom)