Chapter 7 C++ Program Control Structures
Chapter 7 C++ Program Control Structures
Objectives:
Control Strucuture
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 has to be done by our program, when and under which circumstances.
With the introduction of control structures we are going to have to introduce a new concept:
the compound-statement or block. A block is a group of statements which are separated by
semicolons (;) like all C++ statements, but grouped together in a block enclosed in braces: { }:
Most of the control structures that we will see in this section require a generic statement
as part of its syntax. A statement can be either a simple statement (a simple instruction ending
with a semicolon) or a compound statement (several instructions grouped in a block), like the one
just described. In the case that we want the statement to be a simple statement, we do not need
to enclose it in braces ({}). But in the case that we want the statement to be a compound statement
it must be enclosed between braces ({}), forming a block.
SEQUENCE STRUCTURE
The instructions are executed sequentially starting from the first instruction up to the last
instruction in the program.
Entry S1 S2 S3 S4
Page 1
CC02 MODULE FUNDAMENTALS OF PROGRAMMING
a. If Statement
The if keyword is used to execute a statement or block only if a condition is fulfilled. Its form is:
Syntax:
if (condition) statement
Where condition is the expression that is being evaluated. If this condition is true, statement is
executed. If it is false, statement is ignored (not executed) and the program continues right after
this conditional structure.
The
1 if (x following
== 100) code fragment prints x is
100
2 coutonly<<if"xthe value stored in the x variable is
is 100";
indeed 100:
1 if (x == 100) If we want more than a single statement to be executed in
2 { case that the condition is true we can specify a block using
3 cout << "x is "; braces { }:
4 cout << x;
5 }
b. If Else Statement
It executes two statements in a program. If the condition is satisfied the first statement will be
executed otherwise, it will go to another statement.
We can additionally specify what we want to happen if the condition is not fulfilled by using the
keyword else. Its form used in conjunction with if is:
Syntax: if (condition)
statement 1;
else
statement 2;
Prints on the screen x is 100 if
For example: indeed x has a value of 100, but if it has
not -and only if not- it prints out x is
1 if (x == 100) not 100.
2 cout << "x is 100";
3 else
4 cout << "x is not 100";
Page 2
CC02 MODULE FUNDAMENTALS OF PROGRAMMING
c. Else If Statement
Usually it is used to execute if you have three or more statement to be executed in the program.
The if + else structured can be concatenated with the intention of verifying a range of values. The
following example shows its use telling if the value currently stored in x is positive, negative or
none of them (i.e zero):
1 if (x > 0)
2 cout << "x is positive";
3 else if (x < 0)
4 cout << "x is negative";
5 else
6 cout << "x is 0";
note
Remember that in case that we want more than a single statement to be executed, we
must group them in a block by enclosing them in braces { }.
Its objective is to check several possible constant values for an expression. Its form is
the
Page 3
CC02 MODULE FUNDAMENTALS OF PROGRAMMING
switch(x) {
case If (x == 1)
cout<<”x is 1”; {
break; Cout<<”x is 1”;
}
case Else if (x == 2)
cout<<”x is 2”; {
break Cout<<”x is 2”;
}
default: Else
cout<<”value of x {
is unknown”; Cout<<”value of x
} is unknown”;
}
The switch case statement is a bit peculiar within C++ language because it uses labels
instead of blocks. This forces us to put break statements after the group of statements
that we want to be executed for a specific condition. Otherwise the remaining statements
– including those corresponding to other labels - will also be executed until the end of the
switch selective block or a break statement is reached.
➢ https://www.youtube.com/watch?v=0YS8mLNrqhw
➢ https://www.youtube.com/watch?v=usU9ULY7t8E
➢ https://www.youtube.com/watch?v=L_sXxIkd7VU
REFERENCES
❖ https://www.w3schools.com/cpp/cpp_conditions.asp
Page 4