Programming C++ Secations Sixth
Programming C++ Secations Sixth
68
Control Statements : Repetition statements
• Some times we need to repeat a specific course of actions either a
specified number of times or until a particular condition is being satisfied.
• For example :
– To calculate the Average grade for 10 students ,
– To calculate the bonus for 10 employees or
– To sum the input numbers from the user as long as he/she enters
positive numbers.
69
1- The While Loop
While ( continuation condition)
{
Action statement 1 ;
Action statement 2 ;
.
.
Action statement n ;
}
71
1- The While Loop
• Example : Write a program that calculates and prints out the Average
grade for 6 students .
int counter = 1;
int grade=0 , sum=0;
while (counter <=6)
{
cout <<"Enter grade for student no " << counter <<"\n";
cin >>grade;
sum += grade;
counter ++;
}
cout <<"Average Grade is " << sum/counter <<"\n";
72
1- The While Loop
• Example : Write a program To print out the sum of the numbers entered
from the user as long as he/she enters positive numbers.
73
1- The While Loop
• Example : Write a program To print out the sum of the numbers entered
from the user as long as he/she enters positive numbers.
while (1)
{
sum += number;
cout <<" Enter Positive numbers to sum \n";
cin>>number;
If (number < 0)
break;
}
74
1- The While Loop
• Example : Write a program that calculates and prints out the Average
grade for 5 students or ends the program by entering -1.
75
2- The do-while Loop
76
2- The do-while Loop
The condition being tested
may be relational, logical or
even numeric expression :
while ( i <= 10 )
while ( i >= 10 && j <= 15 )
While (3) , While (3 + 5)
77
2- The do-While Loop
• Example : Write a program that calculates and prints out the Average
grade for 6 students .
int counter = 1;
int grade=0 , sum=0;
do
{
cout <<"Enter grade for student no " << counter <<"\n";
cin >>grade;
sum += grade;
counter ++;
}
while (counter <=6) ;
79
Programming C++
80
Control Statements : Repetition statements
• Some times we need to repeat a specific course of actions either a
specified number of times or until a particular condition is being satisfied.
• For example :
– To calculate the Average grade for 10 students ,
– To calculate the bonus for 10 employees or
– To sum the input numbers from the user as long as he/she enters
positive numbers.
81
3- The For Loop.
• For Loop is probably the most popular looping instruction.
• The for allows us to specify three things about a loop in a single line:
(a) Setting a loop counter to an initial value.
(b) testing the loop counter to detect whether its value reached the number
of repetitions desired.
(c) increasing the value of loop counter each time the program segment
83
3- The For Loop.
• Example : Write a program that prints out numbers from 0 to 10;
for (int i= 0 ; i <=10 ; i++)
cout << i << "\n“;
• Example : Write a program that prints out the even numbers from 2 to 20;
for (int i = 2 ; i <=20 ; i+=2 )
cout << i << "\n“;
84
3- The For Loop.
• Example : Write a program that accepts 10 numbers from the user and
prints out the sum of even numbers and the sum of odd numbers.
85
3- The For Loop.
int i = 1 ; int i ;
cout<< i ; cout<< i ;
int i = 1 ; int i ;
for ( ; i <= 10 ; ) for ( i = 0 ; ++ i < 10 ; )
{ cout<< i ;
cout<< i ;
i ++;
}
86
3- The For Loop.
• Example : Write a program that calculates the Factorial for any given
positive number.
Ex : Factorial (5) = 5 * 4 * 3 * 2 * 1
87
Nested Loops
• Example : Write a program that calculates the Factorial for numbers from
1 to 10;
88
Nested Loops
• Example : Write a program that prints out the following pattern.
*
**
for (int i = 1; i <= 5; i++)
***
**** {
***** for (int j = 1; j <= i ; j++)
******
cout << "*";
89