<< Chapter < Page Chapter >> Page >

statements;

}

The flow chart of the while statement is given below.

Flow chart of the while statement

Example

// this program computes the sum of 10 first integers starting from 1

#include<iostream.h>

int main()

{

const int N = 10

int sum = 0;

int count = 1; // initialize count

while (count<= N){

sum = sum + count;

count++; // increment count

}

cout<<“The sum is “<<sum<<endl;

return 0;

}

The output of the above program:

The sum is 55

In the above program, the loop incurs a counter-controlled repetition. Counter-controlled repetition requires:

  1. the name of a control variable (the variable count in this case);
  2. the initial value of the control variable ( count is initialized to 1 in this case)
  3. the condition that tests for the final value of the control variable (i.e., whether looping should continue) ;
  4. the increment (or decrement) by which the control variable is modified each time through the loop.

Example

#include<iostream.h>

int main()

{

int i;

i = 10;

while (i>= 1)

{

cout<<i<<" ";

i--; // subtract 1 from i

}

return 0;

}

The output of the above program:

  1. 9 8 7 6 5 4 3 2 1

Interactive while loops

Combining interactive data entry with the repetition capabilities of the while statement produces very adaptable and powerful programs.

Example

// Class average program with counter-controlled repetition

#include<iostream.h>

int main()

{

int total, // sum of grades

gradeCounter, // number of grades entered

grade, // one grade

average; // average of grades

// initialization phase

total = 0;

gradeCounter = 1; // prepare to loop

while ( gradeCounter<= 10 ) { // 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 / 10; // integer division

cout<<"Class average is "<<average<<endl;

return 0;

}

The following is a sample run of the above program:

Enter grade: 98

Enter grade: 76

Enter grade: 71

Enter grade: 87

Enter grade: 83

Enter grade: 90

Enter grade: 57

Enter grade: 79

Enter grade: 82

Enter grade: 94

Class average is 81

Sentinels

In programming, data values used to indicate either the start or end of a data series are called sentinels. The sentinel values must be selected so as not to conflict with legitimate data values.

Example

#include<iostream.h>

int main()

{

float grade, total;

grade = 0;

total = 0;

cout<<"\nTo stop entering grades, type in any number less than 0.\n\n";

cout<<"Enter a grade: ";

cin>>grade;

while (grade>= 0 )

{

total = total + grade;

cout<<"Enter a grade: ";

cin>>grade;

}

cout<<"\nThe total of the grades is "<<total<<endl;

return 0;

}

The following is a sample run of the above program:

To stop entering grades, type in any number less than 0.

Enter a grade: 95

Enter a grade: 100

Enter a grade: 82

Enter a grade: -2

The total of the grades is 277

Break statement

The break statement causes an exit from the innermost enclosing loop statement.

Example:

while( count<= 10)

{

cout<<“Enter a number: “:

cin>>num;

if (num>76){

cout<<“you lose!\n”;

break;

}

else

cout<<“Keep on trucking!\n”;

count++;

}

//break jumps to here

Questions & Answers

calculate molarity of NaOH solution when 25.0ml of NaOH titrated with 27.2ml of 0.2m H2SO4
Gasin Reply
what's Thermochemistry
rhoda Reply
the study of the heat energy which is associated with chemical reactions
Kaddija
How was CH4 and o2 was able to produce (Co2)and (H2o
Edafe Reply
explain please
Victory
First twenty elements with their valences
Martine Reply
what is chemistry
asue Reply
what is atom
asue
what is the best way to define periodic table for jamb
Damilola Reply
what is the change of matter from one state to another
Elijah Reply
what is isolation of organic compounds
IKyernum Reply
what is atomic radius
ThankGod Reply
Read Chapter 6, section 5
Dr
Read Chapter 6, section 5
Kareem
Atomic radius is the radius of the atom and is also called the orbital radius
Kareem
atomic radius is the distance between the nucleus of an atom and its valence shell
Amos
Read Chapter 6, section 5
paulino
Bohr's model of the theory atom
Ayom Reply
is there a question?
Dr
when a gas is compressed why it becomes hot?
ATOMIC
It has no oxygen then
Goldyei
read the chapter on thermochemistry...the sections on "PV" work and the First Law of Thermodynamics should help..
Dr
Which element react with water
Mukthar Reply
Mgo
Ibeh
an increase in the pressure of a gas results in the decrease of its
Valentina Reply
definition of the periodic table
Cosmos Reply
What is the lkenes
Da Reply
what were atoms composed of?
Moses Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Programming fundamentals in c++. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10788/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Programming fundamentals in c++' conversation and receive update notifications?

Ask