CSCI 150, Spring 2003

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

Pascal For Loops

Note: This information is not in the book, so make sure you study this page!

These loops are used for count controlled loops.

Counting up:

	for counter := expression1 to expression2 do
	begin
		Pascal Code;
	end;

Expression1 and Expression2 have integer values. The loop counts from expression1 up to expression2.

Execution of a for loop:

Counting down:

	for counter := expression1 downto expression2 do
	begin
		Pascal Code;
	end;

Expression1 and Expression2 have integer values. The loop counts from expression1 down to expression2.

Example of a for loop:

	
	{Program BillTotal
	Purpose:  Compute the sum of 12 monthly bills entered by user.}
	program BillTotal;
	var
		count: integer;
		bill, sum: real;
	begin
		sum := 0.0;
		for count := 1 to 12 do
		begin
			writeln('Enter amount of next monthly bill: ');
			readln(bill);
			sum := sum + bill;
		end;
		writeln('The total bill for the year is: ', sum:8:2);
	end.