Вы находитесь на странице: 1из 22

Lecture 8: Iterative Structures

|
Outline

• Review of lab exercise


• Iterative Structures
– while
– do…while
– for loop

03/08/2020 Lecture 8: Iterative Structures


|
Iterative Structures

• while loop
• do… while loop
• for loop

03/08/2020 Lecture 8: Iterative Structures


|
Loops

• What is a loop?
• Loops have a purpose to repeat a statement a certain
number of times or while a condition is fulfilled.

03/08/2020 Lecture 8: Iterative Structures


|
while loop

03/08/2020 Lecture 8: Iterative Structures


|
while loop

• A repetition statement that specifies that a program


should repeat an action while some condition
remains true.
• Syntax
while(condition)
{
//statements to be executed
}

03/08/2020 Lecture 8: Iterative Structures


|
Example

• Write a program that will output the numbers 1-10

03/08/2020 Lecture 8: Iterative Structures


|
Solution
#include<iostream>
using namespace std;

int main()
{
int count;
count=1;//why do we need to initialize?
while(count<=10)
{
cout<<count<<" ";
count=count+1;/*the new value of count is incremented. What is the
shorthand code for this?*/
}
system("pause");
return 0;
}

03/08/2020 Lecture 8: Iterative Structures


|
Factors to consider when creating
loops: Infinite loop
• The loop has to end at some point (avoid infinite
loops).
• Therefore we must provide within the block of
statements a way to force the condition to become
false
• Otherwise the loop will continue forever
• In our code…
count=count+1;//always a better approach to use the
shorthand code for this.

03/08/2020 Lecture 8: Iterative Structures


|
Elements of a loop

• The elements required for any loop include:


– The name of a control variable
– The initial value of the control variable
– The loop-continuation condition
• Tests for the final value of the control variable (whether looping
should continue)
– The increment or decrement by which the control variable
is modified each time through the loop

03/08/2020 Lecture 8: Iterative Structures


|
Class Exercise

• Using the while loop, write a program that outputs


the numbers 12-28.
• Using a while loop write a program to compute the
sum of numbers between 20 and 25
• Using a while loop write a program that calculates
the sum of integers input by a user. The program
should allow the user to enter as many numbers as
possible, until the user enters 0.

03/08/2020 Lecture 8: Iterative Structures


|
do…while loop

03/08/2020 Lecture 8: Iterative Structures


|
do… while loop

• The purpose of the do…while loop is to repeat a


statement while the condition set is true.
• This is exactly the same as the while loop, except…

03/08/2020 Lecture 8: Iterative Structures


|
Syntax for do…while

• Syntax
do
{

}while (condition);

• Note the semi-colon at the end of the while


statement

03/08/2020 Lecture 8: Iterative Structures


|
while vs do…while loop

• In the while statement, the loop continuation


condition test occurs at the beginning of the loop
BEFORE the body executes.
• The do…while loop tests the loop-continuation
condition AFTER the loop body executes; therefore
the loop body always executes ATLEAST once.

03/08/2020 Lecture 8: Iterative Structures


|
Class Exercise

• Using the do…while loop, write a program that


outputs the even numbers between 12-50.
• Using a do…while loop write a program to compute
the sum of numbers between 20 and 25
• Using a do…while loop write a program that
calculates the sum of integers input by a user. The
program should allow the user to enter as many
numbers as possible, until the user enters 0.

03/08/2020 Lecture 8: Iterative Structures


|
For loop

03/08/2020 Lecture 8: Iterative Structures


|
For loop

• The main function of the for loop is to repeat a


statement while a condition remains true, like the
while loop. This sounds familiar.
• But in addition, the for loop provides specific
location to contain an initialization statement,
condition and increment/decrement statement

03/08/2020 Lecture 8: Iterative Structures


|
Syntax

for (initialization; condition; increase/decrease)


{
statements;
}
• Note the positioning of the semi-colons

03/08/2020 Lecture 8: Iterative Structures


|
Class Exercise

• Using the for loop, write a program that outputs the


numbers: 12, 14, 16,18,20,22,24,26,28

03/08/2020 Lecture 8: Iterative Structures


|
Exercise
• What will be the output of the program if executed?

Write the
equivalent of this
program using a
for loop.

03/08/2020 Lecture 8: Iterative Structures


|
Elements of a loop: Recap

• The elements required for any loop include:


– The name of a control variable
– The initial value of the control variable
– The loop-continuation condition
• Tests for the final value of the control variable (whether looping
should continue)
– The increment or decrement by which the control variable
is modified each time through the loop

03/08/2020 Lecture 8: Iterative Structures


|

Вам также может понравиться