230 likes | 340 Vues
This guide explores two fundamental looping constructs in programming: "while...do" and "repeat...until". It provides practical examples illustrating how to write programs that accept user input, calculate and output the square of integers, and terminate when a negative number is entered. The first example utilizes a "while" loop, while the second employs a "repeat" loop. Both showcase interactive user prompts and handle input validation, demonstrating essential programming techniques for controlling flow and ensuring correctness in outputs. Perfect for beginners aiming to solidify their understanding of loops.
E N D
Looping while … do …
Repeated Loop Process 1 Condition Y Process 2
Process 1 N Condition Process 3
while … do ... Process 1 N Condition Y Process 2 Process 3
i=6 i=5 i=4 i=3 i=1 i=2 ? output 3 output 2 output 5 output 1 output 4 i=1; i:=1; i<=5 while i<=5 do Y begin writeln(i); writeln(i); i:=i+1; i:=i+1; end;
i=1; i<=5 N end.
i=1; i<=5 N Y writeln(i); end. i:=i+1;
Ex.1Use “while…do” to write a program to accept any integer and output its square value until the user input a negative number. The output should be as follow. The value after “:” is inputted by user. Welcome! Input an integer: 9 Square of 9 is 81 Input an integer:4 Square of 4 is 16 Input an integer:7 Square of 7 is 49 Input an integer: -1 Bye!
Ex.1Use “while…do” to write a program to accept any integer and output its square value until the user input a negative number. The output should be as follow. The value after “:” is inputted by user. Welcome! Input an integer: 9 Square of 9 is 81 Input an integer:4 Square of 4 is 16 Input an integer:7 Square of 7 is 49 Input an integer: -1 Bye! while…do … segment
input n>=0 n=-1 n=7 n=4 n=9 Ex.1 write(‘Input an integer’); readln(n); n>=0 N Y Y Y Y N writeln(‘Square of ’, n,’ is ’,sqr(n)); writeln(‘Bye!’); write(‘Input an integer’); readln(n);
program ex1; var n:integer; begin writeln(‘Welcome!’); write(‘Input an integer: ’); readln(n); while n>=0 do begin writeln(‘Square of ’,n,‘ is ’,sqr(n)); write(‘Input an integer: ’); readln(n); end; writeln(‘Bye!’); end.
Looping repeat … until …
Repeated Loop Process 1 Process 2 Condition N
Process 1 Process 2 Condition Y Process 3
repeat … until ... Process 1 Process 2 Condition N Y Process 3
i=2 N i=3 N i=4 N i=5 N i=6 ? i:=1; i:=1; i>5 repeat writeln(i); writeln(i); i:=i+1; i:=i+1; i>5 N until i>5;
i=2 N i=3 N i=4 N i=5 N i=6 Y i:=1; i:=1; i>5 repeat writeln(i); writeln(i); i:=i+1; i:=i+1; i>5 until i>5; Y end.
i:=1; i:=1; repeat writeln(i); writeln(i); i:=i+1; i:=i+1; i>5 N until i>5; Y end.
Compare while … do … and repeat … until ... Process 1 Process 1 Process 2 Process 2 N Condition Condition Y Condition Condition N Process 2 Process 2 Process 3 Y Process 3
Ex.2Use “repeat …until” to write a program to accept any integer and output its square value until the user inputs a negative number. The output should be as follows. The value after “:” is inputted by user. Welcome! Input an integer: 9 Square of 9 is 81 Input an integer:4 Square of 4 is 16 Input an integer:7 Square of 7 is 49 Input an integer: -1 Bye!
Ex.2Use “repeat …until” to write a program to accept any integer and output its square value until the user inputs a negative number. The output should be as follows. The value after “:” is inputted by user. Welcome! Input an integer: 9 Square of 9 is 81 Input an integer:4 Square of 4 is 16 Input an integer:7 Square of 7 is 49 Input an integer: -1 Bye! repeat …until … segment
input n<0 n=-1 n=7 n=4 n=9 Ex.2 write(‘Input an integer’); readln(n); writeln(‘Square of ’, n,’ is ’,sqr(n)); N N write(‘Input an integer’); readln(n); N Y n<0 N Y writeln(‘Bye!’);
program ex2; var n:integer; begin writeln(‘Welcome!’); write(‘Input an integer: ’); readln(n); repeat writeln(‘Square of ’,n,‘ is ’,sqr(n)); write(‘Input an integer: ’); readln(n); until (n<0); writeln(‘Bye!’); end.