In this lesson we are going to study about:
1. If condition
2. If-else condition
3. If-else-if condition
4. Nested-if
5. Switch Statement
These conditional statements help us to navigate the program flow based on certain conditions.
Above mentioned are the control statements that C language supports.
1. If condition:
If statement is used, when we need to execute set of statements if the condition is true. If it become false, we shall start to execute next statements.
The condition expression should be a Boolean expression, and should result in only true or false.
Syntax of simple if statement:
if(condition_expression)
{
//execute statements if condition is true
}
Example: Simple C program to check if pin is valid or not:
#include<stdio.h>
int main()
{
int pin;
printf("Enter pin number \n");
scanf("%d", &pin);
if (pin == 8525)
{
printf("Pin is valid \n");
}
printf("Thank You \n");
return 0;
}
2. if else statement
if else statement is used when there is need to execute set of statements if condition is true or execute another set of statements if the condition is false.
For example, if you entered correct password, the system should allow to login, else the system should throw an error message.
Syntax:
———————————–
if(condition_expression)
{
//execute statements if condition is true
}
else
{
//execute statements if condition is false
}
Example: C Program to check if login password is valid or not:
#include<stdio.h>
int main()
{
int password;
printf("Enter Login password \n");
scanf("%d", &password);
if (password == 8525)
{
printf("Password is valid \n");
}
else
{
printf("Password is in-valid \n");
}
printf("Thank You \n");
return 0;
}
3. If-else-if statement
Consider the following example:
If today is Saturday I need to go to movie
else if today is Sunday I need to buy grocery
else I have to go to office as this is weekday.
In these type of situations we need to use if..else..if statements.
Below is the syntax
if (condition1)
{
//Statements 1
}
else if (condition2)
{
//statements 2
}
else
{
//statements 3
}
From the above flowchart, we can understand that, if the condition1 is true statements1 will be executed and shall be out of the if..else ladder. If condition1 is false, it will go and check for condition2 if true statements2 will be executed and come out of if..else ladder. If condition2 is also false, then default statements3 will be executed.
Example: C program to give grades to a student.
#include<stdio.h>
int main()
{
int marks;
printf("Enter marks of a student \n");
scanf("%d", &marks);
if (marks >= 80)
{
printf("Student has got A grade \n");
}
else if (marks >= 80)
{
printf("Student has got B grade \n");
}
else
{
printf("Student has got C grade \n");
}
printf("Thank You \n");
return 0;
}
4. Nested if statement.
There can be an if statement inside an if block. Then it is called as nested if.
Consider the below example: While searching a store, we look if the product is available or not. If the product is available, then we shall check the price of the product and keep it the shopping cart.
Syntax:
if (condition1)
{
//statements1
if(condition2)
{
//statements2
}
}
else
{
//statements3
}
from the above we can see that if condition 1 is true, all the statements under condition 1 will be executed. Then condition2 will be checked, if true all the statements under condition 2 will be executed.
Program example:
#include<stdio.h>
int main()
{
int num_1 = 1;
int num_2 = 2;
if (num_1 == 1)
{
printf("Inside if condition \"num_1 ==1\" \n");
if (num_2 == 2) // Nested if statement
{
printf("Inside if condition \"num_2 == 2\" \n");
}
}
else
{
printf(" In else part");
}
return 0;
}
Go ahead, change the values of “num_1” and “num_2” to get different output.
5. Switch statement
Instead of using if…else..if ladder, we can use switch statement. Switch statement can be useful in situations where a particular code is need to be executed based on multiple choices like menu.
Syntax:
switch(value)
{
case value1:
//statements1
break;
case value2:
//statements2
break;
case value3:
//statements3
break;
default:
//statements4
}
Note:
1. In switch statement, break is necessary. If you miss break statement, then the next case will be executed.
2. Default statement will be executed when none of the cases matches.
3. The case in the switch statement should always be a number or an alphabet. You cannot include Boolean expression there.
Example for switch statement
#include<stdio.h>
int main()
{
int num = 4;
switch(num)
{
case 1:
printf("The number is one\n");
break;
case 2:
printf("The number is two\n");
break;
case 3:
printf("The number is three\n");
break;
case 4:
printf("The number is four\n");
break;
case 5: // we have missed the break statement. Check what happens when the number is 5
printf("The number is five\n");
case 6:
printf("The number is six\n");
break;
default:
printf("This is default case\n");
}
return 0;
}
Output:
When the number is 4.
The number is four
When the number is 5, the output is:
The number is five
The number is six
Note:
1. In the condition expression, 0 is considered as false all other value are considered as true including negative numbers.
Example 1:
if (0)
printf("prodevelopertutorial.com\n");
In this case " prodevelopertutorial.com" will not be printed
Example 2:
if (10)
printf("prodevelopertutorial.com\n");
In this case " prodevelopertutorial.com" will be printed
2. As seen in above example, if there is only one statement following if condition, there is no need to be enclosed in braces.
Example 1: No need of flower brace
if (45)
printf("prodevelopertutorial.com\n");
3. If you forget to put braces for multiple statements to be executed, only first statement is executed according to the condition other statements after that will be executed normally.
Example 1: We forgot to include multiple statements in braces
if (0)
printf("prodevelopertutorial.com\n");
printf("Forgot to include in brace\n");
printf("This statement will also get executed\n");
Output:
As if(0) is false, it will not execute the next statement, but executes other statements.
Forgot to include in brace
This statement will also get executed
4. Multiple conditional statements can be included using relational operators.
Example 1: We need to check a number is greater than 0 and is equal to 55
if ((num > 0)&&( num == 55))
5. Be careful while using equal to operator “==”, if you miss one equal sign, it will be an assignment operator “=” and the result will always true.