# PASCAL PROGRAMMING (THE WHILE LOOP)

Document Sample

```					PASCAL PROGRAMMING (THE WHILE LOOP)
The While loop is used to execute other statements while a condition holds true. The structure is as follows: while <condition> do begin <statement>; <statement>; <statement>; end; The condition can be a boolean expression e.g. while ok = true do where ok has been declared as boolean in the var section. The loop terminates as soon as the condition is no longer true. Example 1 program printnumber; uses crt; var counter : integer; begin clrscr; writeln(„while counter‟); counter:=1; while counter<10 do begin writeln(counter); counter:=counter+1 end; readln end. Example 2 (finding the total of a number of items in a list) program total; uses crt; var total : integer; more : char; amount : integer;

begin clrscr; total:=0; amount:=0; more:=‟y‟; while more = „y‟ do begin write(„enter the amount‟); readln(amount); total:=total+amount; write(„any more items...y/n‟);

PASCAL PROGRAMMING (THE WHILE LOOP)
readln(more); end; writeln(„The total number of items is „,total); readln; end. Example 3 (Count the number of letters in a word) program countletters; var count : integer; ch : char;

begin count:=0; writeln(„enter a word, then a space, then press <ENTER>‟); read(ch); while ch<>‟ „ do {„ „ indicates a space} begin count:=count+1; read(ch); end; writeln; writeln(„number of letters: „,count:4); writeln; end. Test programs 1. Write a program for a child‟s game as follows: the child is asked to enter two integers; the program multiplies the smaller integer by 2; the child carries out the same calculation and enters it in reply to a prompt. If the child‟s answer is correct, “Well done!” is displayed, otherwise the correct answer is displayed. (Use a while...do loop which ends when zero or less is entered for the first integer). 2. Write a program to output any times table „n‟, from 1 times up to 12 times. 3. Write a program to print out a triangle of stars, with 41 stars in the last row. * *** ***** ******* etc. Do not use 41 writeln statements!

```
DOCUMENT INFO
Shared By:
Categories:
Stats:
 views: 60 posted: 11/28/2009 language: English pages: 2
Jun Wang Dr
About Some of Those documents come from internet for research purpose,if you have the copyrights of one of them,tell me by mail vixychina@gmail.com.Thank you!