Variable name implies that it can take many values. The way we do that is to assign values at different times by an assignment statement like num1 := 10;. Writing multiple statements for assigning values is a cumbersome affair. For this purpose pascal has a for statement. It comes in two flavours, we can increase the value of a variable in steps of 1 or decrease the value of a variable in steps of one.
In the following program the for statements are self explanatory. After do we can have only one statement. If we want to have a bunch of statements after the do, we use the begin, multiple statements, end;. We can also see how strings are added. I want to mention write statement writes on the same line whereas writeln moves to a different line after writing the line.
Program Follows:
Program program4;
(* Author: Rao S Lakkaraju *)
{ This is my fourth pascal program }
(* Manipulating Pascal Variables *)
uses crt,graph;
var number1, number2,number3 : integer;
interest1, interest2,interest3 : real;
name1, name2, name3 : string;
begin
clrscr;
number1 := 1;
number2 := 10;
interest1 := 2.5;
interest2 := 5.4;
name1 := 'Rao';
name2 := ' Lakkaraju';
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);
writeln('My fourth Pascal Program');
readln;
end.
Summary: We can vary the value of a pascal variable by using for statements. we can use begin... end; block to put together a bunch of pascal statements for execution as a block. We can add string variables.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment