# 2.4 Repetition statements, arrays and structured programming  (Page 2/8)

 Page 2 / 8

`statements;`

`}`

The flow chart of the while statement is given below.

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

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.

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`

Application of nanotechnology in medicine
what is variations in raman spectra for nanomaterials
I only see partial conversation and what's the question here!
what about nanotechnology for water purification
please someone correct me if I'm wrong but I think one can use nanoparticles, specially silver nanoparticles for water treatment.
Damian
yes that's correct
Professor
I think
Professor
what is the stm
is there industrial application of fullrenes. What is the method to prepare fullrene on large scale.?
Rafiq
industrial application...? mmm I think on the medical side as drug carrier, but you should go deeper on your research, I may be wrong
Damian
How we are making nano material?
what is a peer
What is meant by 'nano scale'?
What is STMs full form?
LITNING
scanning tunneling microscope
Sahil
how nano science is used for hydrophobicity
Santosh
Do u think that Graphene and Fullrene fiber can be used to make Air Plane body structure the lightest and strongest. Rafiq
Rafiq
what is differents between GO and RGO?
Mahi
what is simplest way to understand the applications of nano robots used to detect the cancer affected cell of human body.? How this robot is carried to required site of body cell.? what will be the carrier material and how can be detected that correct delivery of drug is done Rafiq
Rafiq
if virus is killing to make ARTIFICIAL DNA OF GRAPHENE FOR KILLED THE VIRUS .THIS IS OUR ASSUMPTION
Anam
analytical skills graphene is prepared to kill any type viruses .
Anam
what is Nano technology ?
write examples of Nano molecule?
Bob
The nanotechnology is as new science, to scale nanometric
brayan
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Damian
Is there any normative that regulates the use of silver nanoparticles?
what king of growth are you checking .?
Renato
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
why we need to study biomolecules, molecular biology in nanotechnology?
?
Kyle
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
why?
what school?
Kyle
biomolecules are e building blocks of every organics and inorganic materials.
Joe
anyone know any internet site where one can find nanotechnology papers?
research.net
kanaga
sciencedirect big data base
Ernesto
Introduction about quantum dots in nanotechnology
hi
Loga
what does nano mean?
nano basically means 10^(-9). nanometer is a unit to measure length.
Bharti
how did you get the value of 2000N.What calculations are needed to arrive at it
Privacy Information Security Software Version 1.1a
Good
Got questions? Join the online conversation and get instant answers! By George Turner By OpenStax By OpenStax By Anh Dao By OpenStax By Saylor Foundation By By Frank Levy By Jonathan Long By Rhodes