0% found this document useful (0 votes)
96 views41 pages

C++ Loop Examples and Outputs

The document contains code snippets demonstrating the use of various control flow statements in C++ like for, while, do-while loops and break, continue statements. It shows examples of counter-controlled repetition, summation using for loops, nested loops, and using break and continue in loops.

Uploaded by

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

C++ Loop Examples and Outputs

The document contains code snippets demonstrating the use of various control flow statements in C++ like for, while, do-while loops and break, continue statements. It shows examples of counter-controlled repetition, summation using for loops, nested loops, and using break and continue in loops.

Uploaded by

jenny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

What is the out put

#include<iostream>
using namespace std;
void main()
{
int i;
for(i=1;i<10;i++)
{
cout<<i<<endl;
}
cout<<endl<<i<<endl;
}
What is the out put
#include<iostream>
using namespace std;
void main()
{
int i;
for(i=1;i<10;++i)
{
cout<<i<<endl;
}
cout<<endl<<i<<endl;
}
What is the out put
#include<iostream>
using namespace std;
void main()
{
int i;
for(;i<10;++i)
{
cout<<i<<endl;
}
cout<<endl<<i<<endl;
}
Infinite loop
What is the out put
#include<iostream>
using namespace std;
void main()
{
int i;
for(i=1;;++i)
{
cout<<i<<endl;
}
cout<<endl<<i<<endl;
}
Infinite loop
What is the out put
#include<iostream>
using namespace std;
void main()
{
int i;
for(i=1;i<10;)
{
cout<<i<<endl;
}
cout<<endl<<i<<endl;
}
Infinite loop
What is the out put
#include<iostream>
using namespace std;
void main()
{
int i;
for(;;)
{
cout<<i<<endl;
}
cout<<endl<<i<<endl;
}
Infinite loop
What is the out put
#include<iostream>
using namespace std;
void main()
{
int i;
for(i=1;i<10;++i);
{
cout<<i<<endl;
}
cout<<endl<<i<<endl;
}
The out put
x to the y power
// raise x to the y power
#include <iostream>

using namespace std;


int main()
{
int x, y, i, power;
i = 1;
power = 1;
cout << "Enter base as an integer: ";
cin >> x;
cout << "Enter exponent as an integer: ";
cin >> y;
while ( i <= y ) {
power *= x;
++i;
}
cout << power << endl;
return 0;
}
// Class average program with counter-
controlled repetition (Example)
#include <iostream>
using namespace std;
int main()
{
int total, // sum of grades
gradeCounter, // number of grades entered
grade, // one grade
average; // average of grades
// initialization phase
total = 0; // clear total
gradeCounter = 1; // prepare to loop

// processing phase
while ( gradeCounter <= 5 ) { // loop 10 times
cout << "Enter grade: "; // prompt for input
cin >> grade; // input grade
total = total + grade; // add grade to total
gradeCounter = gradeCounter + 1; // increment counter
}
// termination phase
average = total / 5; // integer division
cout << "Class average is " << average << endl;
return 0; // indicate program ended successfully
}
Example
// Counter-controlled repetition
#include <iostream>

using namespace std;

int main()
{
int counter = 1; // initialization

while ( counter <= 10 ) { // repetition condition


cout << counter << endl;
++counter; // increment
}

return 0;
}
// Summation with for
example
#include <iostream>

using namespace std;

int main()
{
int sum = 0;

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


{ cout<<number<<endl;
sum += number;
}
cout <<endl<< "Sum is " << sum << endl<<endl;

return 0;
}
Summation with for
example
Using the break statement in a
for structure
// Using the break statement in a for structure
#include <iostream>
using namespace std;
int main()
{
// x declared here so it can be used after the loop
int x;

for ( x = 1; x <= 10; x++ ) {

if ( x == 5 )
break; // break loop only if x is 5
cout << x << " ";
}
cout << "\nBroke out of loop at x of " << x << endl;
return 0;
}
// Using the continue statement in a for
structure

// Using the continue statement in a for structure


#include <iostream>

using namespace std;

int main()
{
for ( int x = 1; x <= 10; x++ ) {

if ( x == 5 )
continue; // skip remaining code in loop
// only if x is 5

cout << x << " ";


}

cout << "\nUsed continue to skip printing the value 5"


<< endl;
return 0;
}
// Using the continue statement in a for
structure
example
#include <iostream>

using namespace std;


int main()
{
int count = 1;

while ( count <= 10 ) {


cout << (count % 2 ? "****" : "++++++++")
<< endl;
++count;
}

return 0;
}
Nested Control Structures
#include <iostream>

using namespace std;

int main ()
{
int i,j;
for (i = 1; i <= 5 ; i++)
{
for (j = 1; j <= i; j++)
cout << "*";
cout << endl;
}

return 0;
}
Nested Control Structures
Nested Control Structures
#include <iostream>

using namespace std;

int main ()
{
int i,j;
for (i = 1; i <= 5 ; i++)
{
for (j = i; j <= 5; j++)
cout << "*";
cout << endl;
}

return 0;
}
Nested Control Structures
Nested Control Structures
#include <iostream>

using namespace std;

int main ()
{
int i,j;
for (i = 1; i <= 5 ; i++)
{
for (j = 1; j <= 5; j++)
cout << "*";
cout << endl;
}

return 0;
}
Nested Control Structures
The dowhile Loop
#include <iostream>
using namespace std;
int main()
{

int i=1;

do
{
cout<<i<<endl;
i++;

}
while(i<=10);
cout<<endl<<i<<endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{

int i=1;

do
cout<<i;
{

cout<<endl;
i++;

}
while(i<=10);
cout<<endl<<i<<endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{

int i=1;

do
{
cout<<i<<endl;
i++;

}
while(i<=10)
cout<<endl<<i;
return 0;
}

You might also like