TutorialLab
condition in c language
Home » Courses » C Language If , If else , Nested If else Statement with examples

C Language If , If else , Nested If else Statement with examples

C Language Decisions Control Statement:

In decision control statements (C if else and nested if), group of statements are executed when condition is true.

If condition is false, then else part statements are executed.

There are 3 types of decision making control statements in C language. They are,

  1. If statements
  2. If else statements
  3. Nested if statements

“If”, “else” and “nested if” decision control statements in C:

Syntax for each C Language decision control statements are given below table with description.

 

DecisionsSyntaxDescription
ifif (condition-here) {
statement;
}
In these type of statements, if condition is true,
{ statement;}
then respective block (statement) of code is executed.
if_elseif (condition-here) {
statement-1;
statement-2;
}
else {
statement-3;
}
In these type of statements, group of statements are
{ statement-1; statement-2}
executed when condition is true. If condition is false, then
{ statement-3;}
else part (statement-3) statements are executed.

nested ifif (condition-1) {
Statement-1;
}
else_if (condition-2) {
Statement-2;
}
else_if (condition-3) {
Statement-3;
}
else {
default-statement;
}
if condition-1 is false, then condition-2 is checked and statements are executed if it is true. If condition 2 also gets failure, then else part is executed.

 

C if Statement :

In “if” control statement, respective block of code is executed when condition is true.

In above program we use two number and we use also condition if m and n are equal then simply print “m and n are equal”.

OUTPUT : m and n are equal

 

Example 2 :program for if statement in C:

* Write a C program to print the number entered by user only if the number entered is negative.

OUTPUT 1 : enter a number to check:

INPUT 1 : -5

OUTPUT 2 : number is=-5

OUTPUT 2 :the if statement is easy in c programming.

EXPLANATION :

When user enters -5 then, the test expression (num<0) becomes true.

Hence, Number = -5 is displayed in the screen

 

C if_else Statement:

The if_else statement is used if the programmer wants to execute some statement/s when the test expression is true and execute some other statement/s if the test expression is false.

Example 1:  program for if else statement in C:

OUTPUT :

m and n are not equal

Example 2:  program for if else statement in C:

* Write a C program to check whether a number entered by user is even or odd.

OUTPUT 1: Enter a number you want to check.

INPUT 1 : 25

OUTPUT 2 : 25 is odd.

Another output

OUTPUT 1 : Enter a number you want to check.

INPUT 1 : 2

OUTPUT 2 : 2 is even.

 

Nested if_else statement (if_else-if_else Statement):

The nested if…else statement is used when program requires more than one test expression.

Example 1:  program for nested if statement in C:

 In “nested if” control statement, if condition 1 is false, then condition 2 is checked and statements are executed if it is true.

If condition 2 also gets failure, then else part is executed.

OUTPUT : m is greater then n

Example 2:   Program for nested if else statement:

* Write a C program to relate two integers entered by user using = or > or < sign.

OUTPUT 1 : Enter two integers to check.

INPUT 1 : 5

INPUT 2: 3

OUTPUT 2 : Result: 5 > 3

 

Second Output Example :

OUTPUT 1 : Enter two integers to check.

INPUT 1 : 4

INPUT 2 : 4

OUTPUT 2 : Result: -4 = -4

hassan

A Freelancer web developer, graphic designer and own this blog to spread knowledge.My hobby is computer problem's solving and i love to help other in this field.

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.