CSCI 150, Spring 2003

Home | | Course Schedule | | Assignments | | Lecture Notes

While Loops and Logical Expressions

Loops:
A loop is used to execute a block of code repeatedly.

A Pascal while loop:

	while condition do
	begin
		Pascal Code;	{body of loop}
	end;

Execution of a while loop:

Example of a while loop:

	program passwordCheck;
	var
		password, entry: string;
	begin
		password := 'sesame';
		writeln('Please enter your password');
		readln(entry);
		while entry <> password do
		begin
			writeln('Invalid Password!');
			writeln('Please enter password:');
			readln(entry);
		end;
		writeln('Password correct.  Welcome!');
	end.

Logical Expressions:

1) Relational operators:

A = B		true if A and B are the same
A < B		true if A is less than B
A > B		true if A is greater than B
A <> B		true if A is not equal to B
A <= B		true if A is less than or equal to B
A >= B		true if A is greater than or equal to B

2) Logical Expressions using the operators: and, or, not:

p and q		true if both p is true and q is true
p or q		true if p is true or q is true or both are true
not p		true if p is false

Example:

	yards := 5;
	down := 2;

	if (yards > 2) and (down < 4) then
	begin
		writeln('Throw a pass;');
	end;