Chapter 3
Chapter 3
ØIF Statement
ØSwitch Statement
ü Looping Statements
Ø The ‘While’ Statement
Ø The ‘for’ Statement
Ø The ‘do….While’ Statement
ü Other Statements
ØThe ‘Continue’ Statement
ØThe ‘break’ Statement
ØThe ‘goto’ Statement
ØThe ‘Return’ Statement
ü The following example demonstrates that correctly specifying the
order in which the actions execute is important.
ü Consider the "rise-and-shine algorithm" followed by one executive
for getting out of bed and going to work:
1) get out of bed; 4) get dressed;
2) take off pyjamas; 5) eat breakfast;
3) take a shower; 6) go to work.
ü Like many other procedural languages, C++ provides different forms of
statements for different purposes.
ØDeclaration statements are used for defining variables.
ØAssignment 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.
ØFlow control statements are used to divert the execution path to another
part of the program.
ü Specifying the order in which statements (actions) execute in a
program is called program control.
ü Program control or control statement can be :
ØConditional (Selection) Statement (if, if…else, switch)
ØLooping (Repetition )Statement(while, do…while, for-loop)
ØBranching Statement(break, continue, return)
ü If Statement:
Ø It is sometimes desirable to make the execution of a statement
dependent upon a condition being satisfied.
Ø Syntax:
if (expression)
statement;
Ø For example:
if (count != 0)
average = sum / count;
ØTo make multiple statements dependent on the same condition,
we can use a compound statement:
Ø For Example :
if (balance > 0)
{
interest = balance * creditRate;
balance += interest;
}
ü The if-else statement
ØSyntax:
if (expression)
statement1;
else
statement2;
Ø Because loops are statements, they can appear inside other loops. In
other words, loops can be nested. For example,
for (int i = 1; i <= 3; ++i)
for (int j = 1; j <= 3; ++j)
cout << ‘(' << i << ',' << j << ")\n“;
ü The ‘do…while’ Statement:
Ø The do statement (also called do loop) is similar to the while
statement, except that its body is executed first and then the
loop condition is examined.
ØSyntax:
do or do{
statement; statements;
while (expression); }while(expression);
Ø For example, suppose we wish to repeatedly read a value and
print its square, and stop when the value is zero.
Ø This can be expressed as the following loop:
do {
cin >> n;
cout << n * n << '\n';
} while (n != 0);
1. Write a program is to display 1 up to 10 horizontally(i.e
separated by tab ‘\t’ as well as vertically use the endline ‘\n’
or endl
2. Write a program is to find the factorial of 10!.
3. Write a program is to display the reverse of a given number
for example if a user inters 123 the program should display
321.
4. Write a program that print sum of the digits. For instance if a
user enters 123 the program should display 6 i.e 1+2+3=6
Branching Statements
ü The ‘continue’ Statement:
Ø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.
Branching Statements
ü The ‘continue’ Statement:
ØFor example, a loop which repeatedly reads in a number,
processes it but ignores negative numbers, and terminates when
the number is zero, may be expressed as:
do {
cin >> num;
if (num < 0) continue;
// process num here...
} while (num != 0);
Branching Statements
ü 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.
ØLike the continue statement, a break statement 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.
Branching Statements
ü The ‘break’ Statement:
ØFor example, suppose we wish to read in a user password, but
would like to allow the user a limited number of attempts:
for (i = 0; i < attempts; ++i) {
cout << "Please enter your password: ";
cin >> password;
if (password==123) // check password for correctness
break; // drop out of the loop
cout << "Incorrect!\n";
}
Branching Statements
ü The ‘goto’ Statement:
ØThe goto statement provides the lowest-level of jumping.
ØIt has the general form:
goto label;