CS-114 Fundamentals of Computer Programming: Control Statements - III
CS-114 Fundamentals of Computer Programming: Control Statements - III
Fundamentals of Computer
Programming
Lecture 7
Control Statements – III
Programming Foundations I
4
OVERVIEW
C++ has three kinds of iterative statements
The while loop
The for loop
The do-while loop
Programming Foundations I
5
ITERATIVE
STATEMENTS
PART 1
WHILE LOOPS
WHILE LOOPS
A while loop iteratively executes a block of code
We need to specify the following:
The initialization code to execute before the loop
The logical expression for continuing iteration
The block of code to be repeated inside the loop
Programming Foundations I
7
WHILE LOOPS
The C++ syntax of the while loop is:
// initialization statement
while ( logical expression )
{
// block of statements to be repeated
// update variables in logical expression
}
Programming Foundations I
8
The while statement
• A repetition statement specifies that a program
should repeat an action while some condition
remains true.
The while statement
A while loop is a “pre test” loop - the condition is
tested before the loop body is executed
while(condition)
statement;
while(condition)
{
statement(s);
}
The while statement - Example
Part of program
before iteration
TRUE
Part of program
Loop Body after iteration
13
COUNTING LOOPS
Often need to perform an operation fixed number of times
We can use a counting loops to do this operation
Programming Foundations I
14
COUNTING LOOPS
// Counting loop example
int Count = 0; con-var
while (Count < 10) // conditional variable
{
cout << Count << " squared=" << Count*Count << endl;//
independent
Count = Count + 1; // con-var
}
This while loop will count how many times Val can be
divided evenly by 3 (in this case Cnt=3 since 54=3*3*3*2)
This loop will execute over and over until the user enters a
Height value >= 42
This code could go in an infinite loop if the user types a
string like “height” instead an integer value
do {
statement(s);
} while(condition);
Part of program
before iteration
Loop Body
TRUE
Part of program
Loop Body after iteration
28
“do…while” - Example
int main()
{
int score1, score2, score3;
float average;
char again;
do {
cout << "Enter 3 scores and I will average them: ";
cin >> score1 >> score2 >> score3;
average = (score1 + score2 + score3) / 3.0;
cout << "The average is " << average << ".\n";
cout << "Do you want to average another set? (Y/N) ";
cin >> again;
} while (again == 'Y' || again == 'y');
} while ( ? );
return 0;
}
“do…while” - Example
#include <iostream>Output with a Sample Input
using namespace std;
Enter 3 scores and I will average them: 80 90 70 [Enter]
int main() The average is 80.
{ Do you want to average another set? (Y/N) y [Enter]
int score1, score2, score3;
float average;
Enter 3 scores and I will average them: 60 75 88 [Enter]
char again; The average is 74.333336.
Do you want to average another set? (Y/N) n [Enter]
do {
cout << "Enter 3 scores and I will average them: ";
cin >> score1 >> score2 >> score3;
average = (score1 + score2 + score3) / 3.0;
cout << "The average is " << average << ".\n";
cout << "Do you want to average another set? (Y/N) ";
cin >> again;
} while (again == 'Y' || again == 'y');
return 0;
}
“do…while” - Example
#include <iostream>
using namespace std; Output
int main ()
value of a: 10
{ value of a: 11
// Local variable declaration value of a: 12
int a = 10;
// do loop execution
do {
cout << "value of a: " << a
<< endl;
a = a + 1;
} while( a < 13 );
return 0;
}
“do … while …” vs “while …”
• Almost the same
• Prefer “do … while …” when we are
guaranteed to execute loop body at least once
• Prefer “while …” if loop body may not be
executed at all
• Programmer’s choice
33