Professional Documents
Culture Documents
Chapter 3
Chapter 3
Introduction
A running program spends all of its time executing
statements. The order in which statements are executed is
called flow control (or control flow).
This term reflect the fact that the currently executing
statement has the control of the CPU, which when
completed will be handed over (flow) to another
statement.
Flow control in a program is typically sequential, from one
statement to the next, but may be diverted to other paths
by branch statements.
Cont…..
Flow control is an important consideration because it
determines what is executed during a run and what is not,
therefore affecting the overall outcome of the program.
Like many other procedural languages, C++ provides
different forms of statements for different purposes.
Declaration statements are used for defining variables.
Assignment-like statements are used for simple, algebraic
computations. Branching statements are used for specifying
alternate paths of execution, depending on the outcome of a
logical condition.
Loop statements are used for specifying computations,
which need to be repeated until a certain logical condition
is satisfied.
Cont….
Flow control statements are used to divert the execution
path to another part of the program. We will discuss
these in turn.
Control Statements sometimes known as decision
making statements.
Decision making structures require that the
programmer specify one or more conditions to be
evaluated or tested by the program, along with a
statement or statements to be executed if the condition
is determined to be true, and optionally, other
statements to be executed if the condition is determined
to be false.
Cont…
along with a statement or statements to be executed if
the condition is determined to be true, and optionally,
other statements to be executed if the condition is
determined to be false.
Cont….
C++ programming language provides following types
of decision making statements. These are conditional
Statements, Looping Statements and others
Conditional Statements
1. The if Statement
An if statement consists of a Boolean expression
followed by one or more statements.
If Statement
Syntax:
The syntax of an if statement in C++ is:
Notice that the break statement is used inside each case block. This terminates
the switch statement.
If the break statement is not used, all cases after the correct case are executed.
Example:
#include <iostream>
using namespace std;
int main ()
{
int a = 100;
int b = 200;
switch(a) {
case 100:
cout <<"This is part of outer switch" <<endl;
switch(b)
{
case 200: cout <<"This is part of inner switch" <<endl;
}}
cout <<"Exact value of a is : " <<a <<endl;
cout <<"Exact value of b is : " <<b <<endl;
return 0;
}
Example 2
#include <iostream>
using namespace std;
int main() {
char oper;
float num1, num2;
cout << "Enter an operator (+, -, *, /): ";
cin >> oper;
cout << "Enter two numbers: " << endl;
cin >> num1 >> num2;
switch (oper) {
case '+': cout << num1 << " + " << num2 << " = " << num1 + num2;
break;
case '-': cout << num1 << " - " << num2 << " = " << num1 - num2;
break;
case '*': cout << num1 << " * " << num2 << " = " << num1 * num2;
break;
case '/': cout << num1 << " / " << num2 << " = " << num1 / num2;
break;
default: // operator is doesn't match any case constant (+, -, *, /)
cout << "Error! The operator is not correct"; break; }
return 0; }
Looping Statements
The ‘while’ Statement
The while statement (also called while loop) provides a way
of repeating a statement while a condition holds (satisfied).
The general form of the while statement is:
while (condition) {
// body of the loop
}
A while loop evaluates the condition
If the condition evaluates to true, the code inside
the while loop is executed.
The condition is evaluated again.
This process continues until the condition is false.
When the condition evaluates to false, the loop terminates
Cont…
Exmaple
// C++ Program to print numbers from 1 to 5
#include <iostream>
using namespace std;
int main() {
int i = 1;
// while loop from 1 to 5
while (i <= 5) {
cout << i << " ";
++i;
} return 0;
For example, suppose we wish to calculate the sum of all numbers from 1 to
some integer denoted by n.
This can be expressed as:
i = 1; sum = 0;
while (i <= n)
sum += i;
For n set to 5, Table provides a trace of the loop by listing the values of the
variables involved and the loop condition
Cont….