Programming and Graphics

(Kiana) #1

3.3 Control structures 53


3.3 Controlstructures


Control structures are blocks of statements that implement short algorithms
and make logical decisions based on available options. An algorithm is a set of
instructions that achieves a goal through sequential or repetitive steps.


C++ employs control structures with single or multiple statements. The
former are simply stated, while the latter are enclosed by curly bracket delim-
iters,{}.



  • if statement:


Theifstatement implements conditional execution of one command or
a block of commands.

For example, we may write

if(a==10)
b=10;

or
if(a==10)
{
b=10;
}

If more than one statements is involved, the use of curly brackets is manda-
tory:

if(a!=10)
{
b=a+3;
c=20;
}

We highly recommend using the curly brackets even in the case of one
statement.


  • if/else structure:


Theif/elsestructure implements conditional execution based on two
options.

For example, we may write:

if(a!=10)
{
Free download pdf