The document discusses do-while loops and switch statements in C++. It provides explanations of the syntax and flow of do-while loops, including that the code block is executed at least once before the condition is checked. It also explains switch statements, including that they provide an alternative to nested if-else statements when there are multiple choices and only one should be executed. Examples are provided of using do-while loops to display numbers and switch statements to perform arithmetic operations based on an operator.
2. 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
4. 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.
5. 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
6. 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.
9. 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);
}
12. 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
14. 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.
15. SYNTAX OF SWITCH STATEMENT
Switch(expression)
{
case value1:
statement1;
break;
case value2:
statement2;
break;
:
:
18. 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.
19. 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.
20. 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.
21.
22.
23. 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.
24. Write a program to input a character from user. Check it
vowel or consonant.