0% found this document useful (0 votes)
3 views

Programming C++ Secations Sixth

The document provides an overview of control statements in C++, specifically focusing on repetition statements such as while, do-while, and for loops. It includes examples demonstrating how to calculate averages, sums, and factorials using these loops. Additionally, it covers nested loops and their applications in programming.

Uploaded by

moathjubouri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Programming C++ Secations Sixth

The document provides an overview of control statements in C++, specifically focusing on repetition statements such as while, do-while, and for loops. It includes examples demonstrating how to calculate averages, sums, and factorials using these loops. Additionally, it covers nested loops and their applications in programming.

Uploaded by

moathjubouri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

‫‪Programming C++‬‬

‫م‪.‬د‪ .‬حسين أحمد علي الويس‬


‫ﺟﺎمﻌﺔ كركوك‬

‫كليﺔ علوم الحﺎسبﺎت وتكنلوﺟيﺎ المﻌلومﺎت‬

‫قسم تكنلوﺟيﺎ المﻌلومﺎت‬

‫‪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.

• There are three methods by way of which we can repeat a part of a


program. They are:
– (a) Using a while statement
– (b) Using a do-while statement
– (C) Using a for statement

69
1- The While Loop
While ( continuation condition)
{

Action statement 1 ;
Action statement 2 ;
.
.
Action statement n ;
}

1- loop counter is any numeric variable ( int , float ,….).


2- when using a counter , the while loop is called
counter –controlled loop.
3- The initial value of the loop counter is up to you,
but you have to increment it to avoid endless loops.
70
1- The 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)

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.

int number=0 , sum=0;

while (number >= 0)


{
cout <<" Enter Positive numbers to sum \n";
cin>>number;
sum += number;
}
Cout <<“ sum = “ << sum <<“\n”;

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.

int number=0 , sum=0;


cout <<" Enter Positive numbers to sum \n";
cin>>number;

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.

int grade=0, counter =1, sum=0;


cout <<" Enter 5 grades or -1 To Exit \n";
while (counter <=5 && grade !=-1)
{
cin>>grade;
sum += grade;
counter ++;
}

cout <<"The sum of grades is " << sum <<"\n";

75
2- The do-while Loop

1- loop counter is any numeric variable ( int , float ,….).


2- The initial value of the loop counter is up to you,
but you have to increment it to avoid endless loops.
3- The Loop Body is Executed at least one Time.

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) ;

cout <<"Average Grade is " << sum/counter <<"\n";


78
2- The do-While Loop

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.

• There are three methods by way of which we can repeat a part of a


program. They are:
– (a) Using a while statement
– (b) Using a do-while statement
– (C) Using a for statement

81
3- The For Loop.
• For Loop is probably the most popular looping instruction.

• general form of for statement is

• 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

within the loop has been executed.


3- The For Loop.
• Example : Write a program that calculates and prints out the Average
grade for 6 students .

int grade=0, sum=0; int counter = 1;


int grade=0 , sum=0;
for (int counter =1 ; counter <=6 ; counter ++) while (counter <=6)
{ {
cout <<"Enter Grade \n"; cout <<"Enter grade for student \n“ ;
cin >>grade;
cin>>grade; sum += grade;
sum += grade; counter ++;
} }
cout <<"Average Grade is "
cout <<"The Average grades is “ << sum/6 <<"\n"; << sum/6 <<"\n";

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 numbers from 0 to 10 in


descending order;
for (int i = 10 ; i >=0 ; 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 ;

for ( ; i <= 10 ; i + + ) for ( i = 0 ; i++ < 10 ; )

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

int number, factorial=1;


cout <<"Enter a positive number\n";
cin >> number;
if (number < 0 )
cout <<" Enter Positive Numbers only\n";
else
for (int i= 1 ; I <=number ; i++)
factorial = factorial * i;
cout <<" Factorila = “ << factorial <<"\n";

87
Nested Loops
• Example : Write a program that calculates the Factorial for numbers from
1 to 10;

for ( int number=1; number<=10 ; number++)


{
for ( int i= 1 ; i <=number ; i++)
{
factorial = factorial * i ;
}
cout <<" Factorila of " << number <<"=" << factorial <<"\n";
}

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 << "*";

cout << endl;

89

You might also like