0% found this document useful (0 votes)
32 views16 pages

C++ Cha3

This document discusses various control statements in C++ including conditional statements like if/else, iteration statements like while, do-while and for loops, and the switch statement. It provides syntax examples and explanations of how each statement directs the flow of a program based on conditions or repetitions. Key control statements covered are if/else, while, do-while, for, break, continue, goto, return and switch.

Uploaded by

Bacha Tariku
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)
32 views16 pages

C++ Cha3

This document discusses various control statements in C++ including conditional statements like if/else, iteration statements like while, do-while and for loops, and the switch statement. It provides syntax examples and explanations of how each statement directs the flow of a program based on conditions or repetitions. Key control statements covered are if/else, while, do-while, for, break, continue, goto, return and switch.

Uploaded by

Bacha Tariku
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/ 16

Chapter Three

Control Statements
 A running program spends all of its time executing statements
 The order in which statements are executed is called flow
control
 Flow control in a program is typically sequential, from one
statement to the next
 Flow control is an important consideration because it
determines what is executed during a run and what is not
Conditional Statements
The if Statement
 The if statement provides a way of expressing this
 the general form of which is(Syntax):
if (expression)
statement;
 First expression is evaluated
 If the outcome is nonzero (true) then statement is executed
 Otherwise, nothing happens
Example:-
if (x == 100)
cout << "x is 100";
The if Statement else

We can additionally specify what we want to happen if the condition is not fulfilled
by using the keyword else
Its form used in conjunction with if is(Syntax):
if (condition) statement1 else statement2
Example 1:- Example 2:-
if (x == 100)
cout << "x is 100"; if (x > 0)
else cout << "x is positive";
cout << "x is not 100"; else if (x < 0)
cout << "x is negative";
else
cout << "x is 0";
Iteration structures (loops)

Loops have as purpose to repeat a statement a certain number of times


or while a condition is fulfilled
The while loop
The while statement (also called while loop) provides a way of repeating
a statement while a condition holds // custom countdown using while
#include <iostream>
Syntax:- while (expression) statement using namespace std;
int main ()
{
int n;
cout << "Enter the starting number > ";
cin >> n;
while (n>0) {
Enter the starting number > 8 cout << n << ", ";
8, 7, 6, 5, 4, 3, 2, 1, FIRE! Output --n;
}
cout << "FIRE!\n";
return 0;
}
Cont..

The whole process of the previous program can be interpreted


according to the following script (beginning in main)
1 User assigns a value to n
2. The while condition is checked (n>0). At this point there are two possibilities:
* condition is true: statement is executed (to step3)
* condition is false: ignore statement and continue after it (to step 5)
3. Execute statement:
cout << n << ", ";
--n;
(prints the value of non the screen and decreases n by 1)
4. End of block. Return automatically to step 2
5. Continue the program right after the block: print FIRE! and end program.
The do-while loop

Syntax
do statement while (condition);
Its functionality is exactly the same as the while loop, except that
condition in the do-while loop is evaluated after the execution of
statement instead of before
#include <iostream.h>
int main ()
{
int i;
i = 0;
do
{
cout << i;
i ++;
}
while ( i < 10 ) Output:
return 0; 0123456789
}
The for loop

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
an expression which is evaluated once at the end of each iteration
Its format is:
for (initialization; condition; increase) statement;
It works in the following way:
1. Initialization is executed. Generally it is an initial value setting for a counter
variable. This is executed only once.
2. Condition is checked. If it is true the loop continues, otherwise the loop ends
and statement is skipped (not executed).
3. Statement is executed. As usual, it can be either a single statement or a block
enclosed in braces { }.
4. finally, whatever is specified in the increase field is executed and the loop gets
back to step 2.
Cont..

Example:-

// countdown using a for loop


#include <iostream>
using namespace std;
Int main ()
{
for(int n=10; n>0; n--)
{
cout << n << ", ";
}
cout << "FIRE!\n";
Return 0;
} Output
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, FIRE!
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
For example, we are going to skip the number 5 in our countdown
// continue loop example
#include <iostream>
using namespace std;
Int main ()
{
Output 10, 9, 8, 7, 6, 4, 3, 2, 1, FIRE!
for(int n=10; n>0; n--) {
if(n==5) continue;
cout << n << ", ";
}
cout << "FIRE!\n";
return0;
}
The break statement
Using break we can leave a loop even if the condition for its end is not fulfilled.
 It can be used to end an infinite loop, or to force it to end before its natural end.
 For example, we are going to stop the count down before its natural end (maybe
because of an engine check failure?) // break loop example
#include <iostream>
using namespacestd;
intmain ()
{
intn;
for(n=10; n>0; n--)
{
10, 9, 8, 7, 6, 5, 4, 3, cout << n << ", ";
Output if(n==3)
countdown aborted!
{
cout << "countdown aborted!";
break;
}}
return0;
}
The ‘goto’ Statement

The goto statement provides the lowest-level of jumping. It has the


general form:
goto label;
where label is an identifier which marks the jump destination of goto.
The label should be followed by a colon and appear before a statement
within the same function as the goto statement itself

for (i = 0; i < attempts; ++i) {


cout << "Please enter your password: ";
cin >> password;
if (Verify(password)) // check password for correctness
goto out; // drop out of the loop
cout << "Incorrect!\n";
}
out:
The ‘return’ Statement

The return statement enables a function to return a value to its caller.


It has the general form:
return expression;
where expression denotes the value returned by the function
For a function whose return type is void, expression should be empty
return;
The only function we have discussed so far is main, whose return
type is always int
The selective structure: switch
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 constant 1:
statements; break;
...
case constant n:
statements; break;
default:
statements;
}
Cont..
#include<iostream.h>
Example:- int main( )
{
int day;
cout << "Enter the number of the day \n";
cin >> day;
switch (day)
{
case 1: cout << "Sunday"; break;
case 2:cout << "Monday";
break;
case 3: cout << "Tuesday"; break;
case 4: cout << "Wednesday"; break;
case 5: cout << "Thursday"; break;
case 6: cout << "Friday"; break;
case 7: cout << "Saturday"; break;
default: cout << "Invalid day number"; break;
return 0;
}
}
?

You might also like