Conditional Statements in c

  Conditional statements in c programming are considered as the decision making statements. It helps us to make a decision on certain conditions in a program. These conditions are judged using the conditional statements in c programming. These conditional statements result a boolean value  true or false. 

 











 










 










 










 










 
Let us consider there are 50 deserving employees in your company. A certain task has to be given to the employees. You had applied a condition that the task has to be given only to those employees whose ages are below 35.  To apply those conditions we need conditional statements in  a  programming       language.
Depending upon the problem statement,  there may be any condition applied, and to solve the problem we use relational operators in C programming.
Now in C programming, there are some relational operators which are used for the comparison between two situations, we will say those situations as variables.
Those relational operators are

Relational operators
In words
x==y
x is equal to y
 x!=y
x is not equal to y
x<y
x is less than y
x>y
x is greater than y
x<=y
x is less than or equal to y
x>=y
x is greater than equal to y

There are five conditional statements

  1.      If-statement
  2.         If-else statement
  3.        Nested-if statement
  4.      If-else-if statement
  5.      Switch-statement
  1. 1.  If statement

If the situation is true then we represent that situation using if-statement in c programming.
This if-statement is the keyword in C which is used to control the decision, hence C uses the keyword if  to implement the decision control instruction.
Syntax ::
if(condition is true)
{
Execute the statement;
}

Let us understand if statement with the concept with the help of example:
Example:
#include<stdio.h>
int main()
{
 int num;
 printf("\nenter the number:");
 scanf("%d",&num);
 if(num%2==0)
 printf("\n even number\n");
 return 0;
}

Here in this program, num is the integer variable. Runtime input has been taken and the task is to print the statement “ even number” if the number is even.
Output:

if-statement-c
Flowchart:
flowchart-if-statement-c


 

As u can see the output, first we took the input as 12, and even number was printed, and when we took the input as 15 nothing was printed.
So when the if-condition is satisfied the statements under the if-condition will be printed. Otherwise, the cursor will move forward and nothing will be printed,
And curser will come out of the program.

Note: in conditional statements, if the statement under the body of any conditional statement is one line statement then there is no need for opening and closing curly braces for the statement body.
  1. 2.  If else statement

The if-statement itself will execute if the  condition is true, it does nothing when the condition is false.
Now one question arises, can we execute a group of the statement when the condition is true and another group of the statement when the condition is false?
The answer is yes, this the only purpose of the else- statement, when the condition becomes false the else-statement will be executed.

Syntax::
  if(the condition is true)                      
 {
 //when condition is true if-statement will be executed
 Execute this statement;
}
 else                                                     
 {
  // when condition is false else will be   executed
 Execute this statement;         
}      


Example::
#include<stdio.h>

int main()
{
  int num;
 printf("\n enter the number:");
 scanf("%d",&num);
 if(num%2==0)  
 printf("\n even number\n");
 else
 printf("\n odd number");
 return 0;
}
output:
 
if-else-statement-c











 










 










 










 


                      
  •  first output-output when, if condition satisfies, statement is true          
  •  second outputoutput when, if-statement is false and curser moves towards else-statement without entering under if-statement                                      
  1. flowchart-if-else-statement-c


    3.  Nested-if statement

There can be a group of the if-else statement under an if-statement or else-statement, or there can be multiple if statement under an if-statement or else statement.
Let us understand the concept with a simple example,
#include<stdio.h>
int main()
{
 int a,b,c;

 printf("\n enter the three numbers:");
 scanf("%d%d%d",&a,&b,&c);
 if(a>b)
 {
  if(a>c)
  printf("\n a is greater:");
  else
  printf("\n c is greater:");
 }
 else
 {
  if(b>c)
  printf("\n b is greater:");
  else
  printf("\n c is greater:");
 }
 return 0;
}

In this example, three variable inputs are taken, and we have to find the greatest  value among the three values. 
As you can see in the program, we have used an if-else statement under the body of if-
statement and else-statement to get the greatest number. This is an example of a nested-if-statement.

Output:
 
nested-if-statement-c

 for the first input, output is c 
 for the Second input, the output is c
for the third input, output is c
 
flowchart-nested-if-statement-c


  1. 4.  If-else-if statement

If-else-if statement includes three statements, if-statement, else-if statement and else-statement. If there are more than two options given in front of you and you have to choose only one option from all those options, in such a case you can use an if-else-if statement.
Let us understand with an example,
     If-else-if statement
     In the below program, there is a number taken as input, and there are four options which are, 
     to find the number is divided by 5 and 8,
     the number is divided by 5, 
     the number is divided by 8, 
     and divisible by none, 
     the output will be any one of these four.

Example:
#include<stdio.h>
int main()
{
 int a;
 printf("\nenter the  number");
 scanf("%d",&a);
 if(a%5==0 && a%8==0)
   printf("\n Divisible by 5 and 8");
 else if(a%5==0)
   printf("\n Divisible by 5");
 else if(a%8==0)
   printf("\n Divisible by  8");
 else
 printf("\n Divisible by none");
 return 0;
}                                                                                  














Output:


if-else-if-statement-c

 when input is 40, the number is divisible by 5 as well as 8, so the statement under if -statement will be printed and the cursor will not check the other three conditions. 

for second input 35, it is divisible for 5 and not 8, so if the condition will become false and the cursor will move to the next condition ‘’else if’’ if the condition becomes true it will print a statement under it and, the cursor will not check remaining conditions and the cursor will come out. 

In this way, the cursor will check for each condition until the condition gets true.


Also see:

Switch statement

Break statement in c