Professional Documents
Culture Documents
Chapter Three
Controls and Decisions
A program is usually not limited to a linear sequence of instructions. During its process it may
bifurcate, repeat code or take decisions. For that purpose, C++ provides control structures that
serve to specify what and how have to perform our program.
3.1 Decisions/ Selection (if, if…else, switch)
Using decision or selection statement, we select one block of code based on certain conditions.
if Statement
Syntax: if (condition)
{
//line of codes here
}
if condition is satisfied, code is executed, otherwise nothing done.
For example, the following code fragment prints out x is 100 only if the value stored in
variable x is indeed 100:
int x;
cout<<”Enter number”;
cin>>x;
if (x = = 100)
cout << "You Entered 100";
if there is only one statement which depends on the if clause, the braces are not important.
If we want more than a single instruction to be executed in case that condition is true we can
specify a block of instructions using curly brackets { }:
if (x == 100)
{
cout << "x is ";
cout << x;
}
if … else Statement
syntax: if (condition) statement1
else statement2
Example1: if (x == 100)
cout << "x is 100";
else
cout << "x is not 100"; /*prints out on the screen x is 100 if indeed x is worth 100,
but if it is not -and only if not- it prints out x is not 100. */
___________________________________________________________________________
Course Instructors chapter Three Page 1
Fundamental of Programming, C++ Notes
The if + else structures can be concatenated with the intention of verifying a range of values.
The following example shows its use telling if the present value stored in x is positive, negative
or none of the previous, that is to say, equal to zero.
if (x > 0)
cout << "x is positive";
else if (x < 0)
cout << "x is negative";
else
cout << "x is 0";
Remember that in case we want that more than a single instruction is executed we must group
them in a block of instructions by using curly brackets { }.
Switch Statement
Is used when multiple selection is required, and it is equivalent with the if else if statement.
Syntax: switch (expression) {
case constant1:
block of instructions 1
break;
case constant2:
block of instructions 2
break;
.
.
.
default:
default block of instructions
}
Using Switch
.
.
.
int x;
cout<<”Enter a number”;
cin>>x;
switch (x)
{
case1:cout<<”You Entered 1:”;break;
case2:cout<<”You Entered 2:”;break;
case3:cout<<”You Entered 3:”;break;
default: cout<<”Invalid Input:”; break;
}
___________________________________________________________________________
Course Instructors chapter Three Page 2
Fundamental of Programming, C++ Notes
Optionally, using the comma operator (,) we can specify more than one instruction in any of
the fields included in a for loop, like in initialization, for example. The comma operator (,) is
an instruction separator, it serves to separate more than one instruction where only one
___________________________________________________________________________
Course Instructors chapter Three Page 3
Fundamental of Programming, C++ Notes
instruction is generally expected. For example, suppose that we wanted to intialize more than
one variable in our loop:
All the process in the program above can be interpreted according to the following script:
beginning in main:
3. Execute statement:
cout << n << ", ";
--n;
( prints out n on screen and decreases n by 1).
4. End of block. Return automatically to step 2.
5. Continue the program after the block: print out FIRE! and end of program.
___________________________________________________________________________
Course Instructors chapter Three Page 4
Fundamental of Programming, C++ Notes
The do….while loop: is used when a block of code should be executed at least once, just
before the condition is checked.
Syntax: do statement while (condition);
Example
#include<iostream.h>
int main ( )
{
int counter = 1;
do
{
cout<<conter<<” “;
counter++;
}
while( counter < = 10);
return 0;
}
The output will be: 1 2 3 4 5 6 7 8 9 10
The do…while statement is similar to the while statement. The difference is –The condition is
checked first incase of while and later incase of do…while
The break statement: Using break we can leave a loop even if the condition for its end is not
fulfilled. It can be used to end an infinite loop, or to force it to end before its natural end.
Example:
#include<iostream.h>
int main ( )
{
int count;
for (count = 1; count<=10; count++)
{
if (count = =5)
break;
cout<<count<< “ “;
}
return 0;
}
The output will be: 1 2 3 4
___________________________________________________________________________
Course Instructors chapter Three Page 5
Fundamental of Programming, C++ Notes
___________________________________________________________________________
Course Instructors chapter Three Page 6