1 / 23

Utilizing Loops in Programming: "while...do" and "repeat...until" Examples

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.

anana
Télécharger la présentation

Utilizing Loops in Programming: "while...do" and "repeat...until" Examples

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Looping while … do …

  2. Repeated Loop Process 1 Condition Y Process 2

  3. Process 1 N Condition Process 3

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

  5. 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;

  6. i=1; i<=5 N end.

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

  8. 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!

  9. 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

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

  11. 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.

  12. Looping repeat … until …

  13. Repeated Loop Process 1 Process 2 Condition N

  14. Process 1 Process 2 Condition Y Process 3

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

  16. 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;

  17. 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.

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

  19. 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

  20. 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!

  21. 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

  22. 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!’);

  23. 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.

More Related