Top Banner
Looping Looping while … do … while … do …
23

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

Dec 14, 2015

Download

Documents

Tatyana Yarrow
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Looping while … do …. Condition Process 2 Process 1 Y Repeated Loop.

Looping Looping

while … do …while … do …

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

Condition

Process 2

Process 1

YRepeatedLoop

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

Condition

Process 1

Process 3

N

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

Condition

Process 2

Process 1

Process 3

Y

N

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

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

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);

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

i<=5

i=1;

N

end.

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

i<=5

i:=i+1;

i=1;

Y

N

end.writeln(i);

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

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!

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

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.

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

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

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

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.

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

Looping Looping

repeat … until …repeat … until …

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

Condition

Process 2

Process 1

N

RepeatedLoop

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

Condition

Process 2

Process 1

Y

Process 3

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

Condition

Process 2

Process 1

N

Y

Process 3

repeat … until ...repeat … until ...

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

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 ?

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

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

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

writeln(i);

i:=1;

i:=i+1;

repeat

until i>5;i>5

i:=1;

N

i:=i+1;

Y

end.

writeln(i);

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

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

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

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!

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

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.

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

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

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

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.