CH 3 C++

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 22

Chapter Three

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)

interest = balance * creditRate;

balance += interest;

Else

interest = balance * debitRate;


balance += interest;

}
Other form of if statement

 If statements may be nested by having an if statement appear inside another if


statement. For example:
if (callHour > 6)

if (callDuration <= 5)

charge = callDuration * tarrif1; Callhour (0-5]


else
Callhour (5-6)
charge = 5 * tarrif1 + (callDuration - 5) * tarrif2;

else

charge = flatFee; Callhour (>6)


 A frequently-used form of nested if statements involves the else part consisting of


another if-else statement.
 For example:
if (ch >= '0' && ch <= '9')

kind = digit;

else if (cha >= 'A' && ch <= 'Z')

kind = capitalLetter;

else if (ch >= 'a' && ch <= 'z')

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

 The ‘while’ Statement


– provides a way of repeating a statement while a condition holds.
– It is one of the three flavours of iteration in C++.
– The general form of the while statement is:
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

 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

 The ‘for’ Statement


 The for statement (also called for loop) 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.

 The general form of the for statement is:


for (expression1; expression2; expression3)
statement;

 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;

for (i = 1; i <= n; ++i) ////////// i is called loop variable

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;
}
….

 The ‘do…while’ Statement


 its body is executed first and then the loop condition is examined.
 The general form of the do statement is:
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.
 The do loop is less frequently used than the while loop.
 It is useful for situations where we need the loop body to be executed at least once, regardless of the loop
condition.

Fore example
do

cin >> n;

cout << n * n << '\n';

while (n != 0);
Other Statements

 The ‘continue’ Statement


 The continue statement terminates the current iteration of a loop and instead jumps to the next iteration
 It is an error to use the continue statement outside a loop.
Do

cin >> num;

if (num < 0) continue; //////// to ignores negative numbers

// process num here...

while (num != 0);


 The ‘break’ Statement


 A 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
for (i = 0; i < attempts; ++i)
{
cout << "Please enter your password: ";
cin >> password;
if (Verify(password)) // check password for correctness
break; // drop out of the loop
cout << "Incorrect!\n";
}
 reading assignment
 The ‘goto’ Statement
 The ‘return’ Statement
Lab exercise

Write a c++ program that will solve the following problem


1. using if statement
A. Printing Greater values from two numbers
B. calculate profit and loss of single goods
C. Logging program

2. Using for loop


A. Print A B C D . . . . Z
B. Print all even numbers from 50 ……15

3. using while loop


A. Displaying digits of integer in revers order

You might also like