Conditional Statements
Conditional Statements
statement;
else if (condition)
statement;
else
statement;
Nested If statement:
Nested conditional
statements involve
placing one or more
conditional statements
inside another.
This allows for more
complex decision-making
by evaluating multiple
conditions in a
hierarchical manner.
We use this when we
want to implement
multilayer conditions.
SYNTAX EXAMPLE:
if (condition 1 )
{ .
// Executes when condition 1
is true
if (condition 2 )
{
// Executes when condition 2
is true
}
else
{
// Executes when condition 2
is false
}
Switch Statement:
A switch statement is a control
flow statement that allows you
to select one of many code
blocks to be executed based on
the value of a given expression.
The break statement is used to
exit the switch block. Without
break, the control falls
through to the next case,
which may not be the
intended behavior.
#include <iostream>
.
EXAMPLE: using namespace std;
int main() {
int month;
cout << "Enter a month (1-12): ";
cin >> month;
switch (month) {
case 1:
cout << "January";
break;
case 2:
cout << "February";
break;
case 3:
cout << "March";
break;
// ... (continue for the rest of the months)
case 12:
cout << "December";
break;
default:
cout << "Invalid month";
}
return 0;
}
Break:
This loop control statement is
used to terminate the loop. As
soon as the break statement is .
encountered from within a
loop, the loop iterations stop
there, and control returns from
the loop immediately to the
first statement after the loop.
Basically, break statements are
used in situations when we are
not sure about the actual
number of iterations for the
loop or we want to terminate
the loop based on some
condition.
Continue:
The continue statement is
opposite to that of the
break statement, instead of
terminating the loop, it
forces to execute the next
iteration of the loop.
When the continue
statement is executed in
the loop, the code inside
the loop following the
continue statement will be
skipped and the next
iteration of the loop will
APPLICATIONS OF CONDITIONAL
STATEMENTS
Grading system could be done through if-else if
statements.
A basic calculator could be formed by using
switch statements.
Classifies individuals into different age
categories.
Simulates a traffic light scenario and provides
instructions based on the entered signal.
Temperature conversion could also be done
through the decision making statements.