Professional Documents
Culture Documents
CH 3 C++
CH 3 C++
CH 3 C++
Control Statements
Introduction
– The order in which statements are executed is called flow control (or control
flow)
– C++ provides different forms of statements
Declaration statements
Branching statements
used for specifying alternate paths of execution
Loop statements
used for specifying computations
Conditional Statements
The if Statement
desirable to make the execution of a statement dependent upon a condition being satisfied
The if statement provides a way of expressing
the general form of expression
if (expression)
statement;
Execution flow
First expression is evaluated
If (the outcome is nonzero )
then
statement is executed
Otherwise
nothing happens.
…
if-else statement
A variant form of the if statement allows us to specify two alternative statements
the general form
if (expression)
statement1;
else
statement2;
Execution flow
First expression is evaluated.
If (the outcome is nonzero )
then
statement1 is executed.
Otherwise,
statement2 is executed
– For example:
if (balance > 0)
balance += interest;
Else
}
Other form of if statement
if (callDuration <= 5)
else
kind = digit;
kind = capitalLetter;
kind = smallLetter;
else
kind = special;
…
Switch statement
– The switch statement provides a way of choosing between a set of alternatives, based on the value of an expression.
– The general form of the switch statement is:
switch (expression)
{
case constant1:
statements;
...
case constantnt2:
statements;
default:
statements;
}
…
switch (operator)
{
case '+':
result = operand1 + operand2;
break;
case '-':
result = operand1 - operand2;
break;
case '*':
result = operand1 * operand2;
break;
case '/':
result = operand1 / operand2;
break;
default:
cout << "unknown operator: " << ch << '\n';
break;
}
Looping Statements
statement;
First expression (called the loop condition) is evaluated.
If the outcome is nonzero then statement (called the loop body) is executed and the whole process is repeated
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;
…
Examples2: print all integers starting from 1 to 10
#include<iostream.h>
Int main()
{
Int i=1;
while (i<=10)
{
Cout<<i<<endl; Output
i++; 1
} 2
return 0; 3
} 4
5
6
7
8
9
10
…
First expression1 is evaluated /variable definition/. Each time round the loop, expression2 is
evaluated. If the outcome is nonzero then statement is executed and expression3 is evaluated.
The general for loop is equivalent to the following while loop:
expression1;
while (expression2)
statement;
expression3;
The following for loop, for example, calculates the sum of all integers from 1 to n.
sum = 0;
sum += i;
Examples: a program that will print the sum of numbers starting from
1 to 10.
#include<iostream.h>
Int main()
{
Int I, sum=0;
for(i=1; <=10; i++)
{
sum=sum+i;
}
Cout<<sum;
return 0;
}
…
loops can appear inside other loops. In other words, loops can be nested.
– For example,
for (int i = 1; i <= 3; ++I -------------------row
for (int j = 1; j <= 3; ++j) ------------------ column
cout << '(' << i << ',' << j << ")\n";
Write a program that will display the
following shapes
1 //Solution
#include<iostream.h>
12 int main()
123 {
for (int i=1; i<=5; i++)
1234 {
12345 for(int j=1; j<=I; j++)
{
cout<<j;
}
cout<<endl;
}
return 0;
}
….
statement;
while (expression);
Fore example
do
cin >> n;
while (n != 0);
Other Statements