COMPUTER PROGRAMMING
LECTURE # 4: CONTROL
STRUCTURE - A
BSE 1
Joddat Fatima
1
[email protected]
Department of C&SE
Bahria University Islamabad
CONTROL STRUCTURE
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.
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: { }:
{ statement1; statement2; statement3; }
2
THREE CONTROL STRUCTURES
Sequence structure
Programs executed sequentially by default
Selection structures
if, if/else, switch
Repetition structures
while, do/while, for
3
SELECTION STRUCTURE IF-ELSE
4
IF STRUCTURE
The if keyword is used to execute a statement or block only if a
condition is fulfilled. Its form is: 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.
5
CONDITIONAL -- IF
For example, the following code fragment prints x is 100 only if
the value stored in the x variable is indeed 100:
if (x == 100)
cout << "x is 100";
If we want more than a single statement to be executed in case
that the condition is true we can specify a block using braces { }:
if (x == 100)
{
cout << "x is ";
cout << x;
} 6
EXAMPLE IF SELECTION
7
IF AND ELSE STRUCTURE
We can additionally specify what we want to happen if the condition is not
fulfilled by using the keyword else.
if
Performs action if condition true
if/else
Different actions if conditions true or false
if (x == 100)
cout << "x is 100";
else
cout << "x is not 100";
Prints on the screen x is 100 if indeed x has a value of 100, but if it has not 8
-and only if not- it prints out x is not 100.
EXAMPLE IF AND ELSE
9
NESTED IF ELSE
The if + else structures can be concatenated with the intention of verifying a range of
values.
One inside another, test for multiple cases
Once condition met, other statements skipped
The following example shows its use telling if the value currently stored in x is
positive, negative or none of them (i.e. 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 that we want more than a single statement to be executed, we 10
must group them in a block by enclosing them in braces { }.
FLOW DIAGRAM IF-ELSE
11
EXAMPLE NESTED IF AND ELSE
12
SELECTION STRUCTURE SWITCH
13
SWITCH STRUCTURE
The syntax of the switch statement is a bit peculiar.
Its objective is to check several possible constant values for an expression.
Something similar to what we did at the beginning of this section with the
concatenation of several if and else if instructions.
14
SWITCH FLOW DIAGRAM
15
THE CONTROLLING STATEMENT
A switch statement's controlling statement must return one
of these types
A bool value
An enum constant
An integer type
A character
The value returned is compared to the constant values after
each "case"
When a match is found, the code for that case is used
16
THE BREAK STATEMENT
The break statement ends the switch-statement
Omitting the break statement will cause the code for the next
case to be executed!
Omitting a break statement allows the use of multiple case labels
for a section of code
17
THE DEFAULT STATEMENT
If no case label has a constant that matches the controlling
expression, the statements following the default label are
executed
If there is no default label, nothing happens when the switch
statement is executed
It is a good idea to include a default section
18
EXAMPLE SWITCH
19
EXAMPLE SHOW BOTH SELECTIVE STRUCTURES
20
EXAMPLES
21
EXAMPLE # 1
#include <iostream>
int main()
{
int number;
cout << "Enter Number\n";
cin >> number;
if(num%2==0)
cout <<“NUMBER IS EVEN”<<endl;
else
cout <<“NUMBER IS ODD”<<endl;
return 0;
}
22
EXAMPLE # 2
23
EXAMPLE # 3
int main()
{
double number1, number2, number3;
cout << "Enter three floating-point numbers: ";
cin >> number1 >> number2 >> number3;
double max = number1;
if (number2 > max ),
max = number2;
if (number3> max )
max = number3;
cout << "Maximum is: " << max<< endl;
return 0; 24
}
EXAMPLE # 4
25
EXAMPLE # 5
26
EXAMPLE # 6
27