<< 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 mutation
Janga Reply
what is a cell
Sifune Reply
how is urine form
Sifune
what is antagonism?
mahase Reply
classification of plants, gymnosperm features.
Linsy Reply
what is the features of gymnosperm
Linsy
how many types of solid did we have
Samuel Reply
what is an ionic bond
Samuel
What is Atoms
Daprince Reply
what is fallopian tube
Merolyn
what is bladder
Merolyn
what's bulbourethral gland
Eduek Reply
urine is formed in the nephron of the renal medulla in the kidney. It starts from filtration, then selective reabsorption and finally secretion
onuoha Reply
State the evolution relation and relevance between endoplasmic reticulum and cytoskeleton as it relates to cell.
Jeremiah
what is heart
Konadu Reply
how is urine formed in human
Konadu
how is urine formed in human
Rahma
what is the diference between a cavity and a canal
Pelagie Reply
what is the causative agent of malaria
Diamond
malaria is caused by an insect called mosquito.
Naomi
Malaria is cause by female anopheles mosquito
Isaac
Malaria is caused by plasmodium Female anopheles mosquitoe is d carrier
Olalekan
a canal is more needed in a root but a cavity is a bad effect
Commander
what are pathogens
Don Reply
In biology, a pathogen (Greek: πάθος pathos "suffering", "passion" and -γενής -genēs "producer of") in the oldest and broadest sense, is anything that can produce disease. A pathogen may also be referred to as an infectious agent, or simply a germ. The term pathogen came into use in the 1880s.[1][2
Zainab
A virus
Commander
Definition of respiration
Muhsin Reply
respiration is the process in which we breath in oxygen and breath out carbon dioxide
Achor
how are lungs work
Commander
where does digestion begins
Achiri Reply
in the mouth
EZEKIEL
what are the functions of follicle stimulating harmones?
Rashima Reply
stimulates the follicle to release the mature ovum into the oviduct
Davonte
what are the functions of Endocrine and pituitary gland
Chinaza
endocrine secrete hormone and regulate body process
Achor
while pituitary gland is an example of endocrine system and it's found in the Brain
Achor
what's biology?
Egbodo Reply
Biology is the study of living organisms, divided into many specialized field that cover their morphology, physiology,anatomy, behaviour,origin and distribution.
Lisah
biology is the study of life.
Alfreda
Biology is the study of how living organisms live and survive in a specific environment
Sifune
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