<< Chapter < Page Chapter >> Page >

Example

#include<iostream.h>

int main()

{

const int MAXI = 5;

const int MAXJ = 4;

int i, j;

for(i = 1; i<= MAXI; i++) // start of outer loop

{

cout<<"\ni is now "<<i<<endl;

for(j = 1; j<= MAXJ; j++) // start of inner loop

cout<<" j = "<<j; // end of inner loop

} // end of outer loop

cout<<endl;

return 0;

}

The output of the above program:

i is now 1

j = 1 j = 2 j = 3 j = 4

i is now 2

j = 1 j = 2 j = 3 j = 4

i is now 3

j = 1 j = 2 j = 3 j = 4

i is now 4

j = 1 j = 2 j = 3 j = 4

i is now 5

j = 1 j = 2 j = 3 j = 4

Do-while loops

The do..while statement executes a statement or statements once, then repeats the execution as long as a given conditional expression evaluates to true.

The do..while statement is used to create post-test loops.

The syntax for the do..while statement:

do {

statements;

} while (conditional expression);

Example:

do {

cout<<“\nEnter an identification number:”;

cin>>idNum;

} while (idNum<1000|| idNum>1999);

Flow chart of the do...while statement

Here, a request for a new id-number is repeated until a valid number is entered.

do {

cout<<“\nEnter an identification number:”;

cin>>idNum;

if (idNum<1000 || idNum>1999)

{

cout<<“An invalid number was just entered\n”;

cout<<“Please reenter an ID number /n”;

}

else break;

} while (true);

Structured programming with c++

The goto statement

In C++, goto statement – an unconditional branch, is just a legacy code from C language. The result of the goto statement is a change in the flow of control of the program to the first statement after the label specified in the goto statement.

Example:

start: // label

if (cout>10) go to end;

go to start;

end: cout<<endl;

The goto statement can lead to programs that are more difficult to debug, maintain, and modify.

Structured programming

During the 1960s, it became clear that the indiscriminate use of transfers of control through goto statements was the root of much difficulty experienced by programmer groups. The notion of so-called structured programming became almost synonymous with “ goto elimination .”

Bohm and Jacopini’s work demonstrated that all programs could be written in terms of only three control structures:

  • sequence structure
  • selection structure
  • repetition structure

The sequence structure is built into C++. Unless directed otherwise, the computer executes C++ statements one after the other in the order in which they are written. Below is a sequence structure.

Sequence Structure

C++ provides three types of selection structures:

- if statement (single-selection structure)

- if-else statement (double-selection structure)

- switch statement. (multiple-selection structure)

C++ provides three types of repetition structures:

- while statement

- do-while statement

- for statement

So C++ has only seven control structures: sequence, three types of selection and three types of repetition. Each C++ program is formed by combining as many of each type of control structures as is appropriate for the algorithm the program implements.

We will see that each control structure has only one entry point and one exit point . These single-entry/single-exit control structures make it easy to build programs.

Questions & Answers

what is the stm
Brian Reply
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?
LITNING Reply
what is a peer
LITNING Reply
What is meant by 'nano scale'?
LITNING Reply
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
what is Nano technology ?
Bob Reply
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?
Damian Reply
what king of growth are you checking .?
Renato
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
?
Kyle
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
Adin
why?
Adin
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?
Damian Reply
research.net
kanaga
sciencedirect big data base
Ernesto
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
Bharti
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
Daniel
how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
Maciej
characteristics of micro business
Abigail
for teaching engĺish at school how nano technology help us
Anassong
How can I make nanorobot?
Lily
Do somebody tell me a best nano engineering book for beginners?
s. Reply
there is no specific books for beginners but there is book called principle of nanotechnology
NANO
how can I make nanorobot?
Lily
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
s.
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
Tarell
what is the actual application of fullerenes nowadays?
Damian
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
Tarell
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Good
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!





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