Download presentation
Published byYazmin Wardwell Modified over 10 years ago
1
E.g.9 For to do loop for i:=1 to 10 do writeln(i); While do loop i:=1; while i<=10 do begin writeln(i); i:=i+1; end; Repeat until loop i:=1; repeat writeln(i); i:=i+1; until i>10; increase the value of i by 1
2
E.g.10 For to do loop for i:=6 to 12 do writeln(i*2); While do loop i:=6; while i<=12 do begin writeln(i*2); i:=i+1; end; Repeat until loop i:=6; repeat writeln(i*2); i:=i+1; until i>12; increase the value of i by 1
3
Ex.3 For to do loop for a:=1 to 10 do Begin write(a*3,’ square is ’); writeln(sqr(a*3)) End.
4
Ex.3 While do loop a:=1; while a<=10 do begin write(a*3, ‘square is ’); writeln(sqr(a*3)); a:=a+1; end;
5
Ex.3 Repeat Until loop a:=1; repeat write(a*3, ‘square is’); writeln(sqr(a*3)); a:=a+1; until a>10;
6
Ex.4 For to do loop for i:=1 to 5 do begin for j:=1 to i do write(‘*’); writeln; End.
7
Ex.4 while do loop i:=1; while i<=5 do begin j:=1; while j<=i do write(‘*’); j:=j+1; end; writeln; i:=i+1;
8
Ex.4 repeat until loop i:=1; repeat j:=1; write(‘*’); j:=j+1; until j >i; writeln; i:=i+1; until i>5;
9
Ex. Game Use “while. do’’ or ‘’repeat
Ex.Game Use “while..do’’ or ‘’repeat..until” to write a program which let the user guesses a number from 1 to 10 until he got the random number generated by computer. The output should be as follows. The value after “guess ->” is inputted by user. Guess the number from 1 to 10 Guess -> 1 Guess -> 2 Guess -> 3 Good! You got the number!
10
Ex. Game Use “while. do’’ or ‘’repeat
Ex.Game Use “while..do’’ or ‘’repeat..until” to write a program which let the user guesses a number from 1 to 10 until he got the random number generated by computer. The output should be as follows. The value after “guess ->” is inputted by user. Guess the number from 1 to 10 Guess -> 1 Guess -> 2 Guess -> 3 Good! You got the number! repeated segment
11
Ex.Game (while do loop) Random a number key <> guess N Y write(‘Guess the number from 1 to 10’); Write(‘Guess ->’); readln(guess); writeln(‘Good!You got the number!’);
12
Ex.Game (while do loop) Program game Var key,guess:integer; begin
randomize; key:=random(10)+1; writeln(‘Guess the number from 1 to 10’); write(‘Guess ->’); readln(guess); while guess<>key do end; writeln(‘Good!’ You got the number!’); end. Random function
13
Ex.Game (repeat until loop) Random a number writeln(‘Guess the number from 1 to 10’); Write(‘Guess ->’); readln(guess); key = guess N Y writeln(‘Good!You got the number!’);
14
Ex.Game (repeat until loop)
Program game; var key,guess:integer; begin randomize; key:=random(10)+1; repeat writeln('Guess the number from 1 to 10'); write('Guess -> '); readln(guess); until key=guess; writeln('Good! You got the number!'); end.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.