Loops Manual of (For Loop, While Loop and Do-While Loop)
Loops Manual of (For Loop, While Loop and Do-While Loop)
Flow Chart:
Example 1: Write a program in C++ to find the first 10 natural
numbers.
#include <iostream>
using namespace std;
int main()
{
int i;
cout << "\n\n Find the first 10 natural numbers:\n";
cout << "---------------------------------------\n";
cout << " The natural numbers are: \n";
for (i = 1; i <= 10; i++)
{
cout << i << " ";
}
cout << endl;
return 0;
}
#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;
}
While Loop:
Loop: a control structure that causes a statement or statements to repeat
General format of the while loop:
while (expression)
statement;
statement; can also be a block of statements enclosed in { }
Flow chart:
Output:
#include <iostream>
using namespace std;
int main()
{
int n;
cout<< "Input a number to find the factorial:";
cin>>n;
int factorial = 1;
int i = 1;
while (i <= n)
{
factorial = factorial*i;
i++;
}
cout << factorial;
}
}
Formulating Algorithms (Sentinel-Controlled Repetition)
Example 5: Develop a class averaging program that will
process an arbitrary number of grades each time the program
is run.
# include <iostream>
# include <iomanip>
using namespace std;
int main()
{
int total, // sum of grades
gradeCounter, // number of grades entered
grade; // one grade
float average; // average of grades
// initialization phase
total = 0; // Clear total
gradeCounter = 0; // prepare to loop
// processing phase
cout << "Enter grade, type -1 to indicate the end outer: ";
cin >> grade; // input grade
while ( grade != -1) // loop until -1 is entered
{
total = total + grade; // add grade to total
gradeCounter = gradeCounter + 1; // increment counter
cout << "Enter grade, type -1 to indicate the end inner: ";
cin >> grade;
}
// termination phase
if ( gradeCounter != 0 )
{
average = static_cast< float >( total ) / gradeCounter;
cout << "Class average is " << setprecision( 2 ) <<fixed<< average << endl;
}
else
{
cout << "No grades were entered"<<endl;
}
return 0; //indicate program ended successfully
}
Example 6: A college has a list of test results (1 = pass, 2 = fail)
for 10 students. Write a program that analyzes the results. If
more than 8 students pass, print "Raise Tuition".
//Analysis of examination results
#include <iostream>
using namespace std;
int main()
{
// initialize variables in declarations
int passes = 0, // number of passes
failures = 0, // number of failures
studentCounter = 1, // student counter
result; // one exam result
// process 10 students; counter-controlled loop
while ( studentCounter <=10 )
{
cout << "Enter result (1=pass, 2=fail): ";
cin >> result;
if ( result == 1) // if/else nested in while
{
passes = passes + 1;
}
else
{
failures = failures + 1;
}
studentCounter = studentCounter + 1;
}
// termination phase
cout << "Passed " << passes << endl;
cout << "Failed " << failures << endl;
if ( passes > 8 )
{
cout<< "Raise tuition " << endl;
}
return 0;
// successful termination
}
Example 7: Write a program to Printing and count Even
and Odd Numbers using While Loop in C++.
#include<iostream>
using namespace std;
int main()
{
int i=1,n,even=0,odd=0;
cout<<"\nEnter the Ending value:";
cin>>n;
while(i<=n)
{
if(i%2==0)
{
cout<<"\nEven numbers:";
cout<<"\n"<<i;
even++;
}
else
{
cout<<"\nOdd numbers:";
cout<<"\n"<<i;
odd++;
}
i++;
}
cout<<"\nTotal even numbers:"<<even;
cout<<"\nTOtal odd numbers:"<<odd;
return 0;
}
OR
#include<iostream>
using namespace std;
int main()
{
int i=1,n,even=0,odd=0;
cout<<"\nEnter the Ending value:";
cin>>n;
cout<<"\nEven numbers:";
while(i<=n)
{
if(i%2==0)
{
cout<<"\n"<<i;
even++;
}
i++;
}
cout<<"\nOdd numbers:";
i=1;
while(i<=n)
{
if(i%2==1)
{
cout<<"\n"<<i;
odd++;
}
i++;
}
cout<<"\nTotal even numbers:"<<even;
cout<<"\nTOtal odd numbers:"<<odd;
return 0;
}
if( a%2 == 0 ){
cout << "Your number is even" << endl;
}
else{
cout << "Your number is odd" << endl;
}
cout << "Want to check more : 1 for yes and 0 for no" << endl;
cout << "I hope you checked all your numbers" << endl;
return 0;
}
Do while Loop:
do-while: a posttest loop – execute the loop, then test the expression
General Format:
do
statement; // or block in { }
while (expression);
Note that a semicolon is required after (expression)
Example:
Code:
int x = 1;
do
{
cout << x << endl;
} while(x < 0);
Although the test expression is false, this loop will execute one time because do-
while is a posttest loop.
int main()
{
int num, guess, tries = 0;
srand(time(0)); //seed random number generator
num = rand() % 100 + 1; // random number between 1 and 100
cout<<num;
cout << "Guess My Number Game\n\n";
do
{
cout << "Enter a guess between 1 and 100 : ";
cin >> guess;
tries++;
return 0;
}
Practice Questions links website link of
(for loop, while loop and do-while loop)