Presented to:
Sir Ihsan-ul-Ghafoor
Presented by:
19011509-020, 045, 047, 048,
049,054,055
Group:
”3”
Topic:
“5”
Do-while repetition structure , switch statement
Repetition structure
A control structure which executes a statement or set of statements
repeatedly for a specific number of times
Also called iteration or loop
Different types of loops are available in C++
 While loop
 Do-while loop
 For loop
Do-while loop:
Definition
Syntax
Explanation
Flowchart
Example
Difference between while and do-while
Do-while loop:
The do-while loop is a variant of while loop with one important
difference; the body of do-while loop is executed once before
the condition is checked.
Syntax:
do {
// code block to be executed
Statement 1;
statement 2;
:
:
statement N;
}
while (condition);
Code block
do It is the keyword that indicate the
beginning of loop.
In this loop; condition end with a
semicolon
Explanation
The body of the loop is executed at first. Then the condition is
evaluated.
If the condition evaluates to true, the body of the loop inside
the do statement is executed again.
The condition is evaluated once again.
If the condition evaluates to true, the body of the loop inside
the do statement is executed again.
This process continues until the condition evaluates to false.
Then the loop stops.
Flowchart:
Example in flowchart
Display Numbers from 1 to 5
// C++ Program to print numbers from 1 to 5
#include <iostream>
using namespace std;
int main() {
int i = 1;
do {
cout << i << " n";
++i;
}
while (i <= 5);
}
Output
Comparison
Difference between while and do-while
While Do while
It is entry controlled loop. It is exit controlled loop.
Loop execute the statement
after checking the condition is
true.
Loop execute the statement at
least once.
There is no semicolon at the
end of condition
There is a semicolon at the
end of condition
Switch statement
Switch statement
Switch statement is a type of selection structure. It
is a good alternative of nested if-else. It can be
used easily when there are many choices available
and only one should be executed. Nested if
becomes very difficult in such situation.
SYNTAX OF SWITCH STATEMENT
Switch(expression)
{
case value1:
statement1;
break;
case value2:
statement2;
break;
:
:
:
case value n:
statement n;
break;
default:
statements;
}
FLOWCHART OF SWITCH STATEMENT
WORKING OF SWITCH STATEMENT
Switch statement compares the result of a single expression
with multiple cases. Expression can be any valid expression that
results are in integer or character value. The expression is
evaluated at the top of switch statement and its result is
compared with different cases. If the result matches with any
case, the corresponding block of statement is executed.
DEFAULT STATEMENT
The default label appears at the end of all case
labels. It is executed only when the result of
expression does not match with any case label. Its
use is optional. The default label is not fixed.
It may be placed before the first case statement or
after the last one.
Write a program to inputs two numbers and
one operator. It implies arithmetic operation
on numbers on the basis of the operator by
using switch statement.
BREAK STATEMENT
The break statement in each case label is used to
exit from switch body. It is used at the end of each
case label. When the result of the expression
matches with a case label, the corresponding
statements are executed.
The break statement comes after these statements
and control exits from switch body.
If break is not used, all case blocks that come after
the matching case, will also be executed.
Write a program to input a character from user. Check it
vowel or consonant.
Output
EXAMPLE of switch statement
Write a program to input a number of weekdays
and display the name of the day.
Output:
Do While Repetition Structure

Do While Repetition Structure

  • 1.
    Presented to: Sir Ihsan-ul-Ghafoor Presentedby: 19011509-020, 045, 047, 048, 049,054,055 Group: ”3” Topic: “5” Do-while repetition structure , switch statement
  • 2.
    Repetition structure A controlstructure which executes a statement or set of statements repeatedly for a specific number of times Also called iteration or loop Different types of loops are available in C++  While loop  Do-while loop  For loop
  • 3.
  • 4.
    Do-while loop: The do-whileloop is a variant of while loop with one important difference; the body of do-while loop is executed once before the condition is checked.
  • 5.
    Syntax: do { // codeblock to be executed Statement 1; statement 2; : : statement N; } while (condition); Code block do It is the keyword that indicate the beginning of loop. In this loop; condition end with a semicolon
  • 6.
    Explanation The body ofthe loop is executed at first. Then the condition is evaluated. If the condition evaluates to true, the body of the loop inside the do statement is executed again. The condition is evaluated once again. If the condition evaluates to true, the body of the loop inside the do statement is executed again. This process continues until the condition evaluates to false. Then the loop stops.
  • 7.
  • 8.
  • 9.
    Display Numbers from1 to 5 // C++ Program to print numbers from 1 to 5 #include <iostream> using namespace std; int main() { int i = 1; do { cout << i << " n"; ++i; } while (i <= 5); }
  • 10.
  • 11.
  • 12.
    Difference between whileand do-while While Do while It is entry controlled loop. It is exit controlled loop. Loop execute the statement after checking the condition is true. Loop execute the statement at least once. There is no semicolon at the end of condition There is a semicolon at the end of condition
  • 13.
  • 14.
    Switch statement Switch statementis a type of selection structure. It is a good alternative of nested if-else. It can be used easily when there are many choices available and only one should be executed. Nested if becomes very difficult in such situation.
  • 15.
    SYNTAX OF SWITCHSTATEMENT Switch(expression) { case value1: statement1; break; case value2: statement2; break; : :
  • 16.
    : case value n: statementn; break; default: statements; }
  • 17.
  • 18.
    WORKING OF SWITCHSTATEMENT Switch statement compares the result of a single expression with multiple cases. Expression can be any valid expression that results are in integer or character value. The expression is evaluated at the top of switch statement and its result is compared with different cases. If the result matches with any case, the corresponding block of statement is executed.
  • 19.
    DEFAULT STATEMENT The defaultlabel appears at the end of all case labels. It is executed only when the result of expression does not match with any case label. Its use is optional. The default label is not fixed. It may be placed before the first case statement or after the last one.
  • 20.
    Write a programto inputs two numbers and one operator. It implies arithmetic operation on numbers on the basis of the operator by using switch statement.
  • 23.
    BREAK STATEMENT The breakstatement in each case label is used to exit from switch body. It is used at the end of each case label. When the result of the expression matches with a case label, the corresponding statements are executed. The break statement comes after these statements and control exits from switch body. If break is not used, all case blocks that come after the matching case, will also be executed.
  • 24.
    Write a programto input a character from user. Check it vowel or consonant.
  • 26.
  • 27.
    EXAMPLE of switchstatement Write a program to input a number of weekdays and display the name of the day.
  • 30.