c if...else statement

C if else Statements

c if...else statement

C if else Statements

Table of Contents

Conditional statements are often used in programming to make decisions. In this chapter you will learn how to make decisions using C programming if…else and nested if…else statements.

C if else Statement

if…else statements are used to execute some code if the condition is True and some code if it is False.

if...else syntax

				
					if (testExpression) {
 // If true, this code will be executed.
}
else {
 // If false, this code will be executed.
}
				
			

If the test expression is True, if the code block of the statement will be executed and else the code block of the statement will be skipped.

If the test expression is false, else the code block of the statement will be executed and if the code block of the statement will be skipped.

Example 1: C if...else Statement

				
					#include<stdio.h>
#include<conio.h>

int main()
{
 int time=16;
 clrscr();
 if(time<12)
 {
 printf("Good morning."); // Print if time is less than 12 o'clock.
 }
 else
 {
 printf("Good after noon");// Print if time is more than 12 o'clock.
 }
 getch();
}
				
			
output
				
					Good afternoon
				
			

Example 2: C if...else Statement

				
					// Program to check whether integer number input by user is odd or even.

#include <stdio.h>
int main()
{
 int testNumber;
 printf("Enter an integer: ");
 scanf("%d",&testNumber);

 // True if last quotient is zero(0).
 if(testNumber%2 == 0 ){
 printf("%d is an even integer.",testNumber);
 }
 else{
 printf("%d is an odd integer.",testNumber);
 }

 return 0;
}
				
			
output
				
					Enter an integer: 9
9 is an odd integer.
				
			

When the user enters 9, (testNumber%2 == 0) the value of the expression is false. So the statement else inside the statement printf(“%d is an odd integer”); is executed and if the statement inside it is skipped.

Nested if...else statements

if…else Statements are used when we want to execute the code i.e. take a decision whether the condition is True/False. But sometimes decision making may depend on more than two conditions.

Nested if…else statements are used to execute code by testing more than two expressions.

Nested if...else Syntax

				
					if (testExpression1)
{
 //This code will be executed if testExpression1 is True.
}
else if(testExpression2)
{
 // This code block will execute if testExpression1 is false and testExpression2 is True.
}
else if (testExpression3)
{
 //This code block will be executed if testExpression1 and testExpression2 are false and testExpression3 is True.
}
.
.
else
{
 // This code block will execute if all testExpressions are false.
}
				
			
c programming continue statement

Example 1: C nested if...else statement

				
					
#include<stdio.h>  
#include<conio.h>  
int main(){  
    int number=0;  
    clrscr();  
  
    printf("enter a number:");  
    scanf("%d",&number);  
  
    if(number==10){  
    printf("number is equals to 10");  
    }  
    else if(number==50){  
    printf("number is equal to 20");  
    }  
    else if(number==100){  
    printf("number is equal to 30");  
    }  
    else{  
    printf("number is not equal to 10, 20 or 30"); 
    } 
  
    getch();
}
				
			
output
				
					enter a number:5
number is not equal to 10, 20 or 30
				
			
				
					enter a number:100
number is equal to 30
				
			

Example 2: C nested if...else statement

				
					// Create a relationship between two integers using =, > or <

#include <stdio.h>
int main()
{
 int testNumber1, testNumber2;
 printf("Enter two integers: ");
 scanf("%d %d", &testNumber1, &testNumber2);

 //Checks if two integers are equal.
 if(testNumber1 ==testNumber2)
 {
 printf("Result: %d = %d",testNumber1, testNumber2);
 }

 //Checks whether testNumber1 is greater than testNumber2
 else if (testNumber1 > testNumber2)
 {
 printf("Result: %d > %d",testNumber1, testNumber2);
 }

 // if both expressions are false
 else
 {
 printf("Result: %d < %d",testNumber1, testNumber2);
 }

 return 0;
}
				
			
output
				
					Enter two integers: 15
225
Result: 15 < 25
				
			

You can also use switch statements to make decisions against multiple value.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *