Programming and Graphics

(Kiana) #1

58 Introduction to C++ Programming and Graphics



  • do-while:


This is identical to thewhileloop, except that the veracity of the distin-
guishing condition is examinedafterthe first execution of the statements
enclosed by the curly brackets. Thus, at least one execution is granted
even if the distinguishing condition is never true.

For example, thedo-whileloop

int i=0;

do
{
i=i+1;
cout << i << " ";
}
while(i<10);

prints the integers: 1, 2, 3, ..., 9, 10.

Thedo-whileloop is favored when a variable in the distinguishing con-
dition is evaluated inside the loop itself, as in our example.


  • exit:


To stop the execution at any point, we issue the command:

exit(1);

The use of these control structures will be exemplified throughout this
book.


Problems


3.3.1.Assess whether the following two structures are equivalent:


if(i==5) cout << "i=5"; j=i;

if(i==5){cout << "i=5"; j=i;}
Free download pdf