# Looping while … do …. Condition Process 2 Process 1 Y Repeated Loop.

## Presentation on theme: "Looping while … do …. Condition Process 2 Process 1 Y Repeated Loop."— Presentation transcript:

Looping while … do …

Condition Process 2 Process 1 Y Repeated Loop

Condition Process 1 Process 3 N

Condition Process 2 Process 1 Process 3 Y N while … do...

i<=5 i:=i+1; i=1; Y while i<=5 do i:=1; writeln(i); i:=i+1; begin end; i=1output 1i=2output 2i=3output 3i=4output 4i=5output 5i=6? writeln(i);

i<=5 i=1; N end.

i<=5 i:=i+1; i=1; Y N end. writeln(i);

Ex.1 Use “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: Bye!

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 Ex.1 Use “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.

n>=0 Y write(‘Input an integer’); readln(n); writeln(‘Square of ’, n,’ is ’,sqr(n)); write(‘Input an integer’); readln(n); N writeln(‘Bye!’); inputn>=0n=9n=4n=7n=-1 Y Y Y N Ex.1

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 …

Condition Process 2 Process 1 N Repeated Loop

Condition Process 2 Process 1 Y Process 3

Condition Process 2 Process 1 N Y Process 3 repeat … until...

writeln(i); i:=1; i:=i+1; repeat until i>5; i>5 i:=1; N i:=i+1; i>5 writeln(i); i=2Ni=3Ni=4Ni=5Ni=6?

writeln(i); i:=1; i:=i+1; repeat until i>5; i>5 i:=1; Y i:=i+1; end. writeln(i); i>5 i=2Ni=3Ni=4Ni=5Ni=6Y

writeln(i); i:=1; i:=i+1; repeat until i>5; i>5 i:=1; N i:=i+1; Y end. writeln(i);

Condition Process 2 Process 1 Process 3 Y N Condition Process 2 Process 1 N Y Process 3 Compare while … do … and repeat … until... Condition Process 2

Ex.2 Use “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: Bye!

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 Ex.2 Use “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.

n<0 Y write(‘Input an integer’); readln(n); writeln(‘Square of ’, n,’ is ’,sqr(n)); write(‘Input an integer’); readln(n); N writeln(‘Bye!’); inputn<0n=9n=4n=7n=-1 N N N Y Ex.2

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.

Download ppt "Looping while … do …. Condition Process 2 Process 1 Y Repeated Loop."

Similar presentations