0% found this document useful (0 votes)
22 views14 pages

AU Chapter 3

The document discusses different control statements in C++ including conditional statements like if, if-else and switch statements as well as looping statements like while, for, do-while loops. It provides examples and explanations of how each statement works.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views14 pages

AU Chapter 3

The document discusses different control statements in C++ including conditional statements like if, if-else and switch statements as well as looping statements like while, for, do-while loops. It provides examples and explanations of how each statement works.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Chapter Three

Control Statements
 Conditional Statements
 Looping Statements
 Other Statements

1
1. Conditional Statements
The ‘if’ Statement
General form:
if (expression)
statement;
• First expression is evaluated
– If the outcome is nonzero (true) then statement is executed.
– Otherwise, nothing happens.
• Example: if (count != 0)
average = sum / count;
• To make multiple statements dependent on the same condition, we can use a
compound statement:
if (balance > 0) {
interest = balance * creditRate;
balance += interest;
}

2
‘If-else’ Statement
• A variant form of the if statement allows us to specify two alternative
statements
General form:
if (expression)
statement1;
else
statement2;
• First expression is evaluated
– If the outcome is nonzero (true) then statement1 is executed.
– Otherwise, statement2 is executed.
Example:
if (balance > 0)
interest = balance * creditRate;
else
interest = balance * debitRate;
balance += interest; 3
Nested if Statement

• Having an if statement appear inside another if statement


Example 1:
if (callHour > 6) {
if (callDuration <= 5)
charge = callDuration * tarrif1;
else
charge = 5 * tarrif1 + (callDuration - 5) * tarrif2;
} else
charge = flatFee;

Example 2:
if (ch >= '0' && ch <= '9')
kind = digit;
else {
if (ch >= 'A' && ch <= 'Z')
kind = upperLetter;
else {
if (ch >= 'a' && ch <= 'z')
kind = lowerLetter;
else
kind = special;
4
}
The ‘switch’ Statement
• Provides a way of choosing between a set of alternatives, based on the value of an
expression.
General form:
switch (expression) {
case constant1:
statements;
...
case constantn:
statements;
default:
statements;
}
• First expression (called the switch tag) is evaluated:
– And the outcome is compared to each of the numeric constants (called case
labels), in the order they appear, until a match is found.
– The statements following the matching case are then executed.
• Execution continues until either a break statement is encountered or all intervening
5
The ‘switch’ statement……cont’d
Example:
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;
}
• Switch statement can also be written as multiple if-else statements.
Example: if (operator == '+')
result = operand1 + operand2;
else if (operator == '-')
result = operand1 - operand2;
else if (operator == 'x' || operator == '*')
result = operand1 * operand2;
else if (operator == '/')
result = operand1 / operand2; 6
else
2. Looping Statement

The ‘while’ Statement


• Provides a way of repeating a statement while a condition holds.
General form:
while (expression)
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.
– Otherwise, the loop is terminated.
Example: suppose we wish to calculate the sum of all numbers from 1 to some integer
denoted by n.
i = 1;
sum = 0;
while (i <= n)
sum += i;
For n=5
7
The ‘for’ statement
• is similar to the while statement, but has two additional components:
– an expression which is evaluated only once before everything else, and
– an expression which is evaluated once at the end of each iteration.
General form:
for (expression1; expression2; expression3)
statement;
• First expression1 is evaluated
• Each time round the loop, expression2 is evaluated
– If the outcome is nonzero then statement is executed and expression3 is
evaluated
– Otherwise, the loop is terminated.
• The general for loop is equivalent to the following while loop:
expression1;
while (expression2) {
statement;
expression3;
8
}
The ‘for’ statement …….cont’d
• The most common use of for loops is for situations where a variable is incremented
or decremented with every iteration of the loop.
Example: The following for loop calculates the sum of all integers from 1 to n.
int i; int sum; int sum;
sum = 0; sum=0;

for (i = 1; i <= n; ++i) OR for (int i = 1; i <= n; ++i)


sum += i; sum += i;
• This is preferred to the while-loop version we saw earlier. In this example, i is
usually called the loop variable.
• Any of the three expressions in a for loop may be empty.
Example: for (; i != 0;) // is equivalent to: while (i != 0)
something; // something;
• Removing all the expressions gives us an infinite loop. This loop's condition is
assumed to be always true.
Example: for (;;) // infinite loop
something; 9
The ‘for’ statement …….cont’d
• ‘for’ loops with multiple loop variables are not unusual. In such cases, the comma
operator is used to separate their expressions:
Example: for (i = 0, j = 0; i + j < n; ++i, ++j)
something;
• loops can also be nested
Example: for (int i = 1; i <= 3; ++i)
for (int j = 1; j <= 3; ++j)
cout << '(' << i << ',' << j << ")\n";
produces the product of the set {1,2,3} with itself, giving the output:
(1,1)
(1,2)
(1,3)
(2,1)
(2,2)
(2,3)
(3,1)
(3,2)
(3,3) 10
The ‘do…while’ Statement

• is similar to the while statement, except that its body is executed first and then the
loop condition is examined.
• General form:
do
statement;
while (expression);
• First statement is executed and then expression is evaluated
– If the outcome of the latter is nonzero then the whole process is repeated.
– Otherwise, the loop is terminated.
• It is useful for situations where we need the loop body to be executed at least once,
regardless of the loop condition.
Example: repeatedly read a value and print its square, and stop when the value is zero.
do {
cin >> n;
cout << n * n << '\n';
} while (n != 0);
11
3. Other Statements
The ‘continue’ Statement
• terminates the current iteration of a loop and instead jumps to the next iteration.
• It applies to the loop immediately enclosing the continue statement.
• It is an error to use the continue statement outside a loop.
Example: a loop which repeatedly reads in a number, processes it but ignores negative
numbers, and terminates when the number is zero,
do {
cin >> num;
if (num < 0) continue;
cout<<num++;
} while (num != 0);
This is equivalent to:
do {
cin >> num;
if (num >= 0) {
cout<<num++;
} 12
The ‘break’ Statement
• May appear inside a loop (while, do, or for) or a switch statement.
• It causes a jump out of these constructs, and hence terminates them.
• Only applies to the loop or switch immediately enclosing it.
• It is an error to use the break statement outside a loop or a switch.

Example: suppose we wish to read in a user password, but would like to allow the user
a limited number of attempts.
for (int n = 0; n<=7; n++)
{ if(n == 5)
break;
cout<<n;
}
Output: 01234

13
14

You might also like