0% found this document useful (0 votes)
51 views10 pages

Loop Repetition Loop Int Main : Repetition Structure, Counter Controlled Repetition

Here is the program to calculate x to the power y: int main() { int x, y, total = 1; cout << "Enter the value of x: "; cin >> x; cout << "Enter the value of y: "; cin >> y; int c = 1; while(c <= y) { total = total * x; c = c + 1; } cout << x << " to the power of " << y << " is " << total; return 0; }

Uploaded by

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

Loop Repetition Loop Int Main : Repetition Structure, Counter Controlled Repetition

Here is the program to calculate x to the power y: int main() { int x, y, total = 1; cout << "Enter the value of x: "; cin >> x; cout << "Enter the value of y: "; cin >> y; int c = 1; while(c <= y) { total = total * x; c = c + 1; } cout << x << " to the power of " << y << " is " << total; return 0; }

Uploaded by

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

Repetition structure,counter controlled repetition

Loop = repetition
While loop
int main ()
{….
…..
int c = 1; // 1- counter initialization
while ( c < = 5) // 2- counter condition
{ cout << c << “ Hello\n” ;
c = c + 1; // 3- counter modification
}
cout << “Bye”;
cout << c ; // 6
return 0;
}
No: repetition but one time seletion : if ( c <=5)
cout << ”hello”;
Run:
Hello
Hello
Hello
Hello
Hello
Bye
6
cout << c <<“ Hello\n” ;
1 Hello
2 Hello
3 Hello
4 Hello
5 Hello
Bye
6
Infinite loop (no counter modification)
int c = 1 ; // 1- counter initialization
while ( c < = 5) // 2- counter condition
{ cout << c << “ Hello\n” ;
}
Run:
1 hello
1 hello
1 hello
1 hello
1 hello
1 hello
1 hello
……. to infinity
Counting downward
int c = 10 ; // 1- counter initialization
while ( c >= 1) // 2- counter condition
{ cout << c << “ Hello\n” ;
c = c + 1; // 3- counter modification
}
---------------------------
NB: We should make sure that the counter modification will lead to a false
condition in order to stop the repetition.

the below repetition will not stop theoretically (it will stop when the memory
reserved for the variable c will overflow)
int c = 10 ; // 1- counter initialization

while ( c >= 1) // 2- counter condition

{ cout << c << “ Hello\n” ;

c = c + 1; // 3- counter modification

}
Int total = 0
int c = 1; // counter initialization
while ( c < = 3) // counter condition
{ cout << “please enter a no”;
Take the grade;
Add the grade to the total
c = c + 1; // counter modification
}
Cout << “ the average is …….
int garde, total = 0;
int c = 1; // 1- counter initialization
while ( c < = 3) // 2- counter condition
{ cout << “Please enter a grade: ”;
cin >> grade;
total = total + grade; //0+ 75, 75+85=160, 160+80=240
c = c + 1; // 3- counter modification
}
Cout << “The average is” << total/3;

Ram: c  1+1+ 1 +1, grade  80 , Total  0+75 + 85 + 80


Output:
Please enter a grade: 75
Please enter a grade: 85
Please enter a grade: 80
The average is 80
Dangling if-else
Rule: the else pairs with the nearest if ( it will make a pair)

if (x < 10)

if (y > 10)

cout<<”***\n”;

else

cout<<”###\n”;

cout<<”$$$\n”;

same code written with indentation

if (x < 10)

if (y > 10) // this is the statement to be executed when the above if is true

cout<<”***\n”;

else

cout<<”###\n”;

cout<<”$$$\n”;

x is 9, y is 9 x is 9 , y is 11 x is 11 , y is 9 x is 11, y is 11

### *** $$$ $$$

$$$ $$$
if (x < 10)

if (y > 10)

cout<<”***\n”;

else

{ cout<<”###\n”;

cout<<”$$$\n”; }

x is 9, y is 9 x is 9 , y is 11 x is 1 1 , y is 9 x is 11, y is 11

### *** no output no output

$$$

if (x < 10)

{ if (y > 10)

cout<<”***\n”; }

else // the else will pair with if (x<10)

cout<<”###\n”;

cout<<”$$$\n”;

nb: else will pair with if (x<10)

x is 9, y is 9 x is 9 , y is 11 x is 11 , y is 9 x is 11, y is 11

$$$ *** ### ###

$$$ $$$ $$$


Write a program to take from the user 2 integers x and y and
calculate x to the power y
int main ()
{int x, y, total =1;
Cout<< “please enter the value of x : “
Cin >> x;
Cout<< “please enter the value of y : “
Cin >> y;
int c =1;
while (c <= y)
{
total = total*x;
c = c + 1;
}

You might also like