Conditional Statements in C
Conditional Statements in C
Topperworld.in
Conditional Statements
There come situations in real life when we need to make some decisions and
based on these decisions, we decide what should we do next.
Similar situations arise in programming also where we need to make some
decisions and based on these decisions we will execute the next block of code.
1. if in C++
Syntax:
if(condition)
{
// Statements to execute if // condition is true
}
©Topperworld
C++ Programming
Flowchart:
©Topperworld
C++ Programming
Example:
int main()
{
int i = 10;
if (i > 15) {
cout <<
"10 is
greater
than
15";
}
Output:return 0;
}
I am Not in if
2. if-else in C++
©Topperworld
C++ Programming
Here comes the C++ else statement. We can use the else statement with
the if statement to execute a block of code when the condition is false.
The if-else statement consists of two blocks, one for false expression
and one for true expression.
Syntax:
if (condition)
{
// Executes this block if // condition is true
}else
{
// Executes this block if // condition is false
}
Flowchart:
©Topperworld
C++ Programming
Example:
#include <iostream>
using namespace std;
int main()
{
int i = 20;
if (i < 15)
cout <<
"i is
smaller
than
15";
else
cout <<
"i is
greater
Output than
15";
i is greater than 15
return 0;
The block
} of code following the else statement is executed as the condition
present in the if statement is false.
©Topperworld
C++ Programming
Syntax:
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
else
{
// Executes when condition2 is false
}
Flowchart:
©Topperworld
C++ Programming
Example:
int main()
{
int i = 10;
if (i == 10) {
// First if statement
if (i < 15)
cout << "i is
smaller than
15\n";
if (i < 12)
cout << "i is
smaller than
12 too\n";
else
cout << "i is
greater than
15";
}
Output:
return 0;
i }is smaller than 15
i is smaller than 12 too
©Topperworld
C++ Programming
The if else if statements are used when the user has to decide among
multiple options.
The C++ if statements are executed from the top down. As soon as one
of the conditions controlling the if is true, the statement associated
with that if is executed, and the rest of the C++ else-if ladder is
bypassed.
If none of the conditions is true, then the final else statement will be
executed. if-else-if ladder is similar to the switch statement.
Syntax:
if (condition)
statement;else if (condition)
statement;
.
.
else
statement;
Flowchart:
©Topperworld
C++ Programming
Example:
if (i == 10)
cout << "i is 10";
else if (i == 15)
cout << "i is 15";
else if (i == 20)
cout << "i
is 20";
else
cout << "i is not present";
return 0;
}
Output
i is 20
©Topperworld
C++ Programming
Syntax :
switch (expression) {
case value1:
statements;
case value2:
statements;
....
....
....
default:
statements;
}
©Topperworld
C++ Programming
Flowchart:
Example:
#include <iostream>
using namespace std;
// driver code
int main()
{
switch (var) {
case 1:
cout << "Case 1 is executed";
break;
©Topperworld
C++ Programming
case 2:
cout << "Case 2 is executed";
break;
default:
cout << "Default Case is executed";
break;
}
return 0;
Output
Case 2 is executed
©Topperworld