<< 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

what is phylogeny
Odigie Reply
evolutionary history and relationship of an organism or group of organisms
AI-Robot
ok
Deng
what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
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