0% found this document useful (0 votes)
76 views18 pages

Programming Fundamentals: Understanding Loops

Loops allow code to be repeated and can take three forms: while loops repeat while a condition is true, do-while loops always run at least once and repeat while a condition remains true, and for loops allow initialization of loop variables, a condition test and iteration steps. Loops can be nested within each other and variables declared within a loop are not accessible outside of the loop's scope. The continue and break statements can respectively skip the current iteration or exit the loop entirely.

Uploaded by

kanwal hafeez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views18 pages

Programming Fundamentals: Understanding Loops

Loops allow code to be repeated and can take three forms: while loops repeat while a condition is true, do-while loops always run at least once and repeat while a condition remains true, and for loops allow initialization of loop variables, a condition test and iteration steps. Loops can be nested within each other and variables declared within a loop are not accessible outside of the loop's scope. The continue and break statements can respectively skip the current iteration or exit the loop entirely.

Uploaded by

kanwal hafeez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Programming Fundamentals

Understanding Loops
Loops
• Loops provide a way to repeat one or more

statements as many times as our application requires

until a particular condition is met

• The statements inside a loop are called iteration

statements

Programming Fundamentals- Loops 2


Loops
• Two essential elements of a loop:
1. Statement or block of statements that forms the

body of the loop, to be executed repeatedly

2. A loop condition of some kind that determines

when to stop repeating the loop

Programming Fundamentals- Loops 3


Loops
• A loop condition can take a number of different forms,
to provide different ways of controlling the loop
– Execute the loop for a given number of times
– Execute the loop until a given value exceeds another
value
– Execute the loop until a particular character is entered
from the keyboard
• We can set the loop condition to suit the
circumstances

Programming Fundamentals- Loops 4


Types of Loops
• There are three different kinds of loop in C++.

– The while loop

– The do-while loop

– The for loop

Programming Fundamentals- Loops 5


while loop

Condition No
while(condition) is true?
{
Yes
// iteration statement(s)
Loop
} //end of while loop Statement(s)

Next Statement

Programming Fundamentals- Loops 6


while loop
• If the condition controlling the loop
produces an integer, the loop will continue
as long as the value is non-zero
• Any non-zero integer is converted to type
bool as true , and only 0 converts to false

Programming Fundamentals- Loops 7


The do-while Loop

do
{ Loop
Statement(s)
// iteration statement (s)
} while (condition);
Condition Yes
is true?

No
This semicolon is
absolutely compulsory
Next Statement

Programming Fundamentals- Loops 8


More Complex while Loop
conditions

• while (choice ==‘y’ || choice ==‘Y’)

• while (tolower(choice) ==‘y’)

Programming Fundamentals- Loops 9


This expression is The for loop
Executed once, at the This expression is evaluated
beginning of the loop. at the end of each loop iteration.
It is typically used to It is typically used to modify
initialize one Or more the values of Variables initialized
loop variables in the First expression

for(initializing_expression; condition; iteration_expression )


{
The semi-colons must be present.
//iteration statement (s) The expression need not be

} //end of for loop


This expression is evaluated
at the beginning of each loop
iteration. If it is true the loop
continues, and if it is false
execution with the statements
after the loop

Programming Fundamentals- Loops 10


The for loop
Evaluate
Initialization Flow chart
expression

Condition No
is true?
Yes
Loop
Statement(s)

Evaluate
Iteration
expression

Next Statement

Programming Fundamentals- Loops 11


Loops and Variable Scope

• for loop, like while and do-while loops, defines a


scope
• The loop statement or block, as well as any
expressions used to control the loop, all fall within the
scope of a loop
• This includes all three expressions used to control for
loop
• Any automatic variables declared within the scope of
a loop do not exist outside the loop

Programming Fundamentals- Loops 12


Loops and Variable Scope

• The variable i, declared in the for loop


initialization expression is destroyed when the
loop ends (in example)
• Therefore, variable i in the for loop example
cannot be referenced after the loops ends

Programming Fundamentals- Loops 13


Nested Loops
• We can place a loop inside another loop

• We can ‘nest’ loops to whatever depth we require for


solving a problem
• Nested loops can be of any kind:

• A for loop, inside a while loop inside a do-while


loop, they can be mixed in any way we want

Programming Fundamentals- Loops 14


Skipping loop Iteration
• If we want to skip one iteration of the loop

and jump onto the next iteration we make use

of continue statement.

continue;

Programming Fundamentals- Loops 15


Breaking Out of a Loop
• If we want to end the loop prematurely,

• The break statement will be used

break;

• Its effect is same as we saw in the switch statement

Programming Fundamentals- Loops 16


Indefinite Loops
• An indefinite loop can potentially run forever

• If we leave out the test conditions in for loop,


it runs forever

• Indefinite loop is useful when we don’t know


in advance how many iterations are required

Programming Fundamentals- Loops 17


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
and 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
iteration
• 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
Programming Fundamentals- Loops 18

You might also like