Professional Documents
Culture Documents
Loops
Programming Fundamentals
Loops provide a way to repeat one or more statement
statements
Loops Loops
A loop condition can take a number of different forms,
Two essential elements of a loop: to provide different ways of controlling the loop
Execute the loop for a given number of times
1. Statement or block of statements that forms the
Execute the loop until a given value exceeds another
body of the loop, to be executed repeatedly
value
2. A loop condition of some kind that determines Execute the loop until a particular character is entered
when to stop repeating the loop from the keyboard
We can set the loop condition to suit the
circumstances
Next Statement
1
10/12/2017
No
This semicolon is
absolutely compulsory
Next Statement
9 Programming Fundamentals- Loops 10 Programming Fundamentals- Loops
2
10/12/2017
3
10/12/2017
Summary
A loop is a mechanism for repeating a block of statements
There are three kinds of loop that we can use: while, do-while a
nd for
The while loop repeats for as long as a specified condition is
true
The do-while loop always performs at least one iteration, and
continues for as long as a specified condition is true
The for loop is typically used to repeat a given number of times,
and has three expressions
Any kind of loop may be nested within any other kind
Executing a continue statement within a loop will skip the
remainder of the current iteration and go straight to the next itera
tion
Executing a break statement within a loop will cause an
immediate exit from the loop
A loop defines a scope, so that variables declared within loop
are not accessible outside the loop. Variables declared in the
initialization expression of for loop are not accessible outside
21 Programming Fundamentals- Loops