Ch3_Repition - 1 Repetition Statements Loops • In the...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Repetition Statements Loops • In the programs studied so far a statement is executed only once. • Sometimes a process can be repeated many times: repetition • The repetition of steps in a program is called Loop • Pascal has three loop control statements: – While – For – Repeat 2 The For Loop • Called A fixed repetition loop is used when we do know in advance how many times “A segment of code” will be repeated. • Syntax: For counter := initial to final do Begin Statement1; … End; 3 For Loop • The index variable (The Counter) is automatically increased by 1after each loop. • After the loop is finished, the value of the index variable becomes unassigned and program control is transferred to the first executable statement following the loop . 4 (* A program that will write “Hello” *) (* 1 thousand times *) Program Hello_10000; uses wincrt; var counter : integer; begin For counter := 1 to 1000 do writeln(‘Hello’); Writeln (‘Bye’); End. Example 5 Example • Write a program that reads a number and computes the sum from 1 to that number. • For example, if you type 7 the output will be 28 = 1 + 2 + 3+ 4 + 5 +6 +7 6 Program ComputeSum; uses wincrt; var num, count, sum : integer; begin Writeln ('Please enter a number:'); Readln(num); for count:=1 to num do begin sum:= sum+count; end; Writeln('Sum is:',Sum); end. 7 Example-Factorial • Write a program that computes the factorial of a number....
View Full Document

{[ snackBarMessage ]}

Page1 / 25

Ch3_Repition - 1 Repetition Statements Loops • In the...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online