Conditional-Statements-in-C
Conditional-Statements-in-C
Topperworld.in
Conditional Statement
©Topperworld
C Programming
1. if
The if statement is the most simple decision-making statement. It is used
to decide whether a certain statement or block of statements will be
executed or not i.e if a certain condition is true then a block of statements
is executed otherwise not.
Syntax:
if
(condition)
{
// Statements to execute if
// condition is true
}
Here,
The condition after evaluation will be either true or false. C if statement
accepts boolean values – if the value is true then it will execute the block
of statements below it otherwise not.
If we do not provide the curly braces ‘{‘ and ‘}’ after if(condition) then by
default if statement will consider the first immediately below statement
to be inside its block.
©Topperworld
C Programming
Example:
#include <stdio.h>
int main()
{
int gfg = 9;
// if statement with true condition
if (gfg < 10) {
printf("%d is less than 10", gfg);
}
Output
9 is less than 10
2. if-else
©Topperworld
C Programming
Syntax
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
Example:
#include <stdio.h>
int main()
{
int i = 20;
if (i < 15) {
printf("i is smaller than 15");
}
else {
printf("i is greater than 15");
}
return 0;
}
©Topperworld
C Programming
3. Nested if-else
A nested if in C is an if statement that is the target of another if statement.
Nested if statements mean an if statement inside another if statement.
Yes, both C and C++ allow us to nested if statements within if statements,
i.e, we can place an if statement inside another if statement.
Syntax
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
else
{
// Executes when condition2 is false
}
Example:
©Topperworld
C Programming
int main()
{
int i = 10;
if (i == 10) {
// First if statement
if (i < 15)
printf("i is smaller than 15\n");
// Nested - if statement
// Will only be executed if statement above
// is true
if (i < 12)
printf("i is smaller than 12 too\n");
else
printf("i is greater than 15");
}
return 0;
}
Output:
i is smaller than 15
i is smaller than 12 too
4. if-else-if Ladder
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.
©Topperworld
C Programming
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;
Example
©Topperworld
C Programming
if (i == 10)
printf("i is 10");
else if (i == 15)
printf("i is 15");
else if (i == 20)
printf("i is 20");
else
printf("i is not present");
}
Output: i is 20
5. Switch Statement
The switch case statement is an alternative to the if else if ladder that can
be used to execute the conditional code based on the value of the variable
specified in the switch statement.
The switch block consists of cases to be executed based on the value of
the switch variable.
Syntax
switch (expression) {
case value1:
statements;
case value2:
statements;
....
....
....
default:
statements;
©Topperworld
C Programming
Example
©Topperworld
C Programming
default:
printf("Default Case is executed");
break;
}
return 0;
}
6. Conditional Operator
The conditional operator is used to add conditional code in our program. It
is like the if-else statement.
It is also known as the ternary operator as it works on three operands.
©Topperworld
C Programming
Example
#include <stdio.h>
// driver code
int main()
{
int var;
int flag = 0;
// using conditional operator to assign the value to var
// according to the value of flag
var = flag == 0 ? 25 : -25;
printf("Value of var when flag is 0: %d\n", var);
// changing the value of flag
flag = 1;
// again assigning the value to var using same statement
var = flag == 0 ? 25 : -25;
printf("Value of var when flag is NOT 0: %d", var);
return 0;
}
Output:
Value of var when flag is 0: 25
Value of var when flag is NOT 0: -25
©Topperworld