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

23
Looping Looping while … do … while … do …

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

Looping Looping

while … do …while … do …

Condition

Process 2

Process 1

YRepeatedLoop

Condition

Process 1

Process 3

N

Condition

Process 2

Process 1

Process 3

Y

N

while … do ...while … do ...

i<=5

i:=i+1;

i=1;

Y

while i<=5 do

i:=1;

writeln(i);

i:=i+1;

begin

end;

i=1 output 1

i=2 output 2

i=3 output 3

i=4 output 4

i=5 output 5

i=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: -1

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 … segmentwhile…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!’);

input n>=0

n=9

n=4

n=7

n=-1

Y

Y

Y

N

Ex.1Ex.1

program ex1;

var n:integer;

beginwriteln(‘Welcome!’);write(‘Input an integer: ’);readln(n);while n>=0 dobegin

writeln(‘Square of ’,n,‘ is ’,sqr(n));write(‘Input an integer: ’);readln(n);

end;writeln(‘Bye!’);

end.

Looping Looping

repeat … until …repeat … until …

Condition

Process 2

Process 1

N

RepeatedLoop

Condition

Process 2

Process 1

Y

Process 3

Condition

Process 2

Process 1

N

Y

Process 3

repeat … until ...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=2 N

i=3 N

i=4 N

i=5 N

i=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=2 N

i=3 N

i=4 N

i=5 N

i=6 Y

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 Compare while … do …while … do … and and repeat … until ...repeat … until ...

Condition

Condition

Process 2

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: -1

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 … segmentrepeat …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!’);

input n<0

n=9

n=4

n=7

n=-1

N

N

N

Y

Ex.2Ex.2

program ex2;

var n:integer;

beginwriteln(‘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.