Tle Group 2 Switch Statements
Tle Group 2 Switch Statements
STATEMENTS
GROUP 2
What is the Switch Statement?
The switch statement in C++ is a potent control structure that
enables you to run several code segments based on the result of
an expression. It offers a sophisticated and effective substitute
for utilizing a succession of if-else-if statements when you have
to make a decision between several possibilities.
It is also a flexible construct that makes it easier for programs to
handle a variety of scenarios. Its explicit case labels and
succinct syntax make code easier to comprehend and maintain,
especially in situations when there are many possible outcomes.
The switch statement improves the organization of program
logic by offering a direct mapping between cases and actions.
The main feature of the switch statement in C++ is:
• The fall-through behavior of the C++ switch
statement is one of its key features. The control
will fall through to the next case if a break
statement is not used to stop a case block. After
that, subsequent cases will be processed until a
break is encountered or the end of the switch
block is reached. This capability may be
purposely used to share common code across
several scenarios.
The BREAK keyword
• Break stop the execution of more
code and case testing inside the
block.
The DEFAULT keyword
• Specifies some code to run if
there is no case match.
The
SWITCH STATEMENT SYNTAX :
switch (exp
(expreession)
ssion) {
case constant1:
// code to be executed if
// expression is equal to constant1;
break;
case constant2:
// code to be executed if
// expression is equal to constant2;
break;
default:
// code to be executed if
// expression doesn't match any constant
}
EXAMPLES:
1. Without using default.
EXAMPLES:
2. Using default.
EXAMPLES:
3. Using both break and default.
NESTED SWITCH
Nested switch statement in C++
Switch-case statements: These are a substitute
for long if statements that compare a variable to
several integral values
The switch statement is a multiway branch
statement. It provides an easy way to dispatch
execution to different parts of code based on the
value of the expression.
Switch is a control statement that allows a value
to change control of execution.
Syntax
switch(n)
{
case 1: // code to be executed if n = 1;
break;
case 2: // code to be executed if n = 2;
break;
default: // code to be executed if
// n doesn't match any cases
}
Points to remember while using Switch Case
The expression used in a switch statement
must have an integral or character type, or be
of a class type in which the class has a single
conversion function to an integral or
character type. There can be any number of
case statements within a switch. Each case is
followed by the value to be compared to and
after that a colon.
Points to remember while using Switch
Case
Switch cases require an integral or
character type expression or a class type
with a single conversion function. They can
have multiple case statements, followed by
the value to be compared and a colon. When
a case is equal to the variable, subsequent
statements execute until a break statement
is reached. The flow of control jumps to the
next line. An optional default case can be
used when none of the cases is true.
Example 1
Example 2
Example 3
BREAK
STATEMENT
Break Statement in C++
The break statement is used with the conditional switch
statement and with the do , for , and while loop statements. In
a switch statement, the break statement causes the program
to execute the next statement outside the switch statement.
The Syntax in Break Statement is:
break;
Before you learn about the break statement, make sure you
know about:
• C++ for loop
• C++ if...else
• C++ while loop
Example 1: break with for loop
In a for loop, the break command ends the loop and
moves control to the next command outside the loop.
In the above program, the for loop is used to print the
value of i in each iteration.
if (I ==3) {
break;
}
This means, when i is equal to 3, the break statement
terminates the loop. Hence, the output doesn't include values
greater than or equal to 3.
Note: The break statement is usually used with decision-making statements.
EXAMPLE 1:
Example 2: break with while loop
In a while loop, To break out of a while loop, you can use
the endloop, continue, resume, or return statement.
In the above program, the user enters a number. The
while loop is used to print the total sum of numbers
entered by the user.
if(number < 0) {
break;
}
This means, when the user enters a negative number,
the break statement terminates the loop and codes
outside the loop are executed.
EXAMPLE 2:
Example 3: break with nested loop
When break is used with nested loops,
break terminates the inner loop.
In the above program, the break statement
is executed when i == 2. It terminates the
inner loop, and the control flow of the
program moves to the outer loop.
Hence, the value of i = 2 is never displayed in
the output.
EXAMPLE 3:
CONTINUE
STATEMENT
C++ continue Statement
In computer programming, the continue statement is
used to skip the current iteration of the loop and the
control of the program goes to the next iteration.
The syntax of the continue statement is:
continue;
Before you learn about the continue statement, make
sure you know about,
C++ for loop
C++ if...else
C++ while loop
Example 1: continue with for loop
In a for loop, continue skips the current iteration and the control
flow jumps to the update expression.
In the above program, we have used the the for loop to print the
value of i in each iteration. Here, notice the code,
if (i == 3) {
continue;
}
This means
- When i is equal to 3, the continue statement skips the current
iteration and starts the next iteration
- Then, i becomes 4, and the condition is evaluated again.
- Hence, 4 and 5 are printed in the next two iterations.
Note: The continue statement is almost always used with
decision-making statements.
EXAMPLE 1
Example 2: continue with while loop
In a while loop, continue skips the current iteration and control flow of the
program jumps back to the while condition.
In the above program, the user enters a number. The while loop is used to print
the total sum of positive numbers entered by the user, as long as the numbers
entered are not greater than 50.
Notice the use of the continue statement.
if (number > 50){
continue;
}
When the user enters a number greater than 50, the continue statement skips
the current iteration. Then the control flow of the program goes to the condition
of while loop.
When the user enters a number less than 0, the loop terminates.
Note: The continue statement works in the same way for the do...while loops.
EXAMPLE 2
Example 3: continue with Nested loop
When continue is used with nested loops, it skips the
current iteration of the inner loop. For example,
In the above program, when the continue statement
executes, it skips the current iteration in the inner loop.
And the control of the program moves to the update
expression of the inner loop.
Hence, the value of j = 2 is never displayed in the output.
Note: The break statement terminates the loop entirely.
However, the continue statement only skips the current
iteration.
EXAMPLE 3
Thank You