Tutorial references

c
c++
c#

Tutorial references

chapters

Conditional Statements in C

if()

if() is a conditional statement. It is used to make decisions.

if you want to compare two variable or values then you can use if() statement.

Syntax:

if(condition){

/*This is true body area */

}

If condition is true then true body area statements will be execute. If condition is not true then true body area will not be execute.

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int m;
 int n;
 clrscr();
 printf("\n This is an if() statement program.");
 printf("\n Enter value of m: ");
 scanf("%d",&m);
 printf("\n Enter value of n: ");
 scanf("%d",&n);
 if(m > n){
 printf("\n %d is greater than %d ",m,n);
 }
 printf("\n Thank you ");
 getch();
}

Run program

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int m;
 int n;
 int r;
 clrscr();
 printf("\n This is an if() statement program.");
 printf("\n Enter value of m: ");
 scanf("%d",&m);
 printf("\n Enter value of n: ");
 scanf("%d",&n);
 r = m > n;
 if(r){
 printf("\n %d is greater than %d ",m,n);
 }
 printf("\n Thank You");
 getch();
}

Run program

if() else

"if() else" is also conditional statements. It is also used to make decisions.

Syntax:

if(condition){

/* True body area*/

}
else{

/* False body area*/

}

If condition is true then it will execute true body area otherwise execute false body area.

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int m;
 int n;
 clrscr();
 printf("\n This is an if() else statement program.");
 printf("\n Enter value of m: ");
 scanf("%d",&m);
 printf("\n Enter value of n: ");
 scanf("%d",&n);
 if(m > n){
 printf("\n %d is greater than %d ",m,n);
 }
 else{
 printf("\n %d is not greater than %d ",m,n);
 }
 getch();
}

Run program

else if ladder ( if() else if() else )

If you have more than one conditions then you can use else if ladder.

If first condition is false then it goes to check next condition. That condition is true which true body area will be execute.After execution else if ladder does not go to check next condition.

If any condition does not match then else{ } will be execute.It means false body area will be execute.

Syntax:

if(condition 1){

/* True body area*/

}
else if(condition 2){

/* ture body area*/

}
else if(condition 3){

/* ture body area*/

}
else if(condition 4){

/* ture body area*/

}
-----------
-----------
else{

/*false body area*/

}

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int dayNum;
 clrscr();
 printf("\n This is a ladder if() statement program.");
 printf("\n Enter number for day: ");
 scanf("%d",&dayNum);
 if( dayNum == 0){
 printf("\n SUNDAY ");
 }
 else if(dayNum==1){
 printf("\n MONDAY ");
 }
 else if(dayNum == 2){
 printf("\n TUESDAY ");
 }
 else if(dayNum == 3){
 printf("\n WEDNESDAY ");
 }
 else if(dayNum == 4){
 printf("\n THURSDAY ");
 }
 else if(dayNum == 5){
 printf("\n FRIDAY ");
 }
 else if(dayNum == 6){
 printf("\n SATURDAY ");
 }
 else{
 printf("\n please enter value between 0 to 6 ");
 }
 getch();
}

Run program

Nested if() else statement

if you are using if(){} else{} statement inside of if(){} body. or inside of else{} body then it called nested if() else statement.

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int m;
 int n;
 int p;
 clrscr();
 printf("\n This is a nested if() else statement program.");
 printf("\n Enter value of m: ");
 scanf("%d",&m);
 printf("\n Enter value of n: ");
 scanf("%d",&n);
 printf("\n Enter value of p: ");
 scanf("%d",&p);
 if(m > 100){
  if(n > 75){
   if(p > 50){
   printf("\n HELLO WORLD ");
   }
  }
 }
 else{
 if(m < 100){
  if(n < 75){
   if(p < 50){
   printf("\n HELLO FRIENDS ");
   }
  }
 }
 }
 getch();
}

Run program

switch()

If you want test value of a variable result of an expression then you can use switch()

If first case value is not match with switch(variable) variable then it chech next case. That case match with switch(variable) variable which statement will be execute after execution switch statement will be terminate.

Syntax:

switch( variable/expression ){
case value-1:

/* code here*/

break;
case value-2:

/* code here*/

break;
case value-3:

/* code here*/

break;
case value-4:

/* code here*/

break;
---------------
---------------
default:

/* code here*/

break;
}

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int dayNum;
 clrscr();
 printf("\n This is a switch statement program.");
 printf("\n Enter number for day: ");
 scanf("%d",&dayNum);
 switch( dayNum ){
 case 0:
 printf("\n SUNDAY ");
 break;
 case 1:
 printf("\n MONDAY ");
 break;
 case 2:
 printf("\n TUESDAY ");
 break;
 case 3:
 printf("\n WEDNESDAY ");
 break;
 case 4:
 printf("\n THURSDAY ");
 break;
 case 5:
 printf("\n FRIDAY ");
 break;
 case 6:
 printf("\n SATURDAY ");
 break;
 default:
 printf("\n Enter value between 0 to 6 ");
 break;
 }
 getch();
}

Run program

Example

#include <stdio.h>
#include <conio.h>
void main(){
 int dayNum;
 clrscr();
 printf("\n This is a switch statement program.");
 printf("\n Enter number for day: ");
 scanf("%d",&dayNum);
 switch( dayNum ){
 case 1:
 case 2:
 case 3:
 case 4:
 case 5:
 printf("\n Regular Days ");
 break;
 case 0:
 case 6:
 printf("\n Week end Days ");
 break;
 default:
 printf("\n Enter value between 0 to 6 ");
 break;
 }
 getch();
}

Run program

About Us

This website(www.sabbla.com) is very useful for beginners and professional developers. Sabbla provides tutorial in simple language. It provides latest technology tutorial which very useful for developers.

See More...

Contact Us

+91 9971366710

+91 9717983128

info@sabbla.com

Buxar, Bihar, India, Pin No.- 802131

Services

Sabbla is providing free latest technology tutorials. All tutorials are free for beginner and professionals.


Terms of Use

sabbla.com is not responsible for any mistake. Users can inform to sabbla.com for any mistake. But sabbla.com always try for 0-0 mistake.

Privacy policy

Currently sabbla.com does not collect any data from users.

                   

copyright © 2017-2018