do while Loop

do

stmt;

while (condition);

do

{

stmt1;

stmt2;

.

.

stmtn;

} while (condition);

Used when code has to be executed at least once.

Since the condition is tested at the end of the loop the code is executed at least one time.

Main purpose is for error trapping (testing for invalid input).

Ex.

do

{

cout << "Do you wish to format  another disk? Y/N";

cin >> ans;

}while (ans != 'Y' && ans != 'y' && ans != 'n' && ans != 'N');

For Loops

for (initialization expression;

loop repetition condition;

update expression)

stmt;

for (initialization expression;

loop repetition condition;

update expression)

{

stmt1;

stmt2;

.

.

stmtn;

}

Old Problem:  The weather service has contracted you to write the program that will convert their daily Fahrenheit temperature readings into Celsius readings.   Readings are taken every four hours.  At the end of each day, the average daily temperature is calculated.  Print the results of each conversion and the daily average.

count = 0;

while (count < 6)

{

cout << "Please enter a temperature: ";

cin >> temp;

averagetempsum += temp;

celsius = 5.0/9.0*(temp - 32);

cout << temp << " degrees fahrenheit is "

<< celsius << " degrees celsius." << endl << endl;

count ++;

}

averagedaily = averagetempsum / 6;

cout << "The average daily temperature is:  " << averagedaily << endl;

Using a for loop:

int i;

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

{

cout << "Please enter a temperature: ";

cin >> temp;

averagetempsum += temp;

celsius = 5.0/9.0*(temp - 32);

cout << temp << " degrees fahrenheit is "

<< celsius << " degrees celsius." << endl << endl;

}

averagedaily = averagetempsum / 6;

cout << "The average daily temperature is:  " << averagedaily << endl;

Problem:  Write a code fragment that prints all the even numbers between 0 and a given positive number.

#include <iostream>

using namespace std;

int

main()

{

int i, num;

cout << "Please enter a number: " ;

cin >> num;

for (i = 0; i <= num; i = i +2)

cout << i << endl;

return 0;

}

Problem:  Given an integer, print all the numbers from that number down to zero.

#include <iostream>

using namespace std;

int

main()

{

int i, num;

cout << "Please enter a number: " ;

cin >> num;

for (i = num; i > 0 ; i-- )

cout << i << endl;

return 0;

}

For loops should be used when the following conditions are present:

1) We have an initialization

2) The loop repeats dependent on some condition

3) There is an update.

Nested for loops

Problem:  We wish to process twelve numbers located in a data file.  We want to add 5 numbers and compare the sum to the sixth number.  If the sum is less than or equal to the sixth number, we add the sixth number to the sum and print.  Otherwise we print the sum of the five numbers.

#include <iostream>

#include <fstream>

#include <cstdlib>

using namespace std;

int

main()

{

int i, j, sum = 0, num;

ifstream fin;

fin.open("data2.txt");

if (fin.fail())

{

cerr << "Error opening file." << endl;

abort();

}

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

{

for (j = 0; j < 5; j++)

{

fin >> num;

cout << num << endl;

sum += num;

}

fin >> num;

cout << num << endl;

if (sum <= num) sum += num;

cout <<"The sum is: " <<  sum << endl;

sum = 0;

}

return 0;

}

When one loop is nested inside the other the innermost loop is executed to completion.  Execution proceeds from the innermost to the outermost loop.

Problem: Print an eight line right triangle using asterisks (*)

#include <iostream>

#include <iomanip>

using namespace std;

int

main()

{

int i, j;

int stars = 0;    //number of stars to print

//Print lines 2 through 7

for (i = 1; i < 8; ++i)

{

cout  << '*';

for (j = 0; j < stars; ++j)

cout << " *";

cout  <<endl;

stars ++;

}

//Print last line.

for (i = 0; i <= stars -1 ; ++i)

cout  << "**";

cout << '*' << endl;

return 0;

}

Problem:  Print an n line triangle.

#include <iostream>

#include <iomanip>

using namespace std;

int

main()

{

int i, j;

int space = 0;    //number of stars to print

int lines;  //number of lines in the triangle

//Print first line of triangle

cout  << "How many lines? ";

cin >> lines;

//Print lines 2 through 9

for (i = 1; i < lines; ++i)

{

cout  << '*';

for (j = 0; j < space; ++j)

cout << " *";

cout  <<endl;

space ++;

}

//Print last line.

for (i = 0; i <= space -1 ; ++i)

cout  << "**";

cout << '*' << endl;

return 0;

}