if else statement in C programming

In the C language decision making statement is executes if the given condition is true otherwise conditional block will never execute.

In C language non-zero and non-null values are consider as true, and zero or null values are consider  false.

if statement

Syntax of an if statement −

In a if statement if the expression result is non zero true, then the block of statement of the ‘if’ statement will be executed. If the expression result is zero, then the block of statement of the ‘if’ statement will not executed and control sent to the next line of if block.

Example: Write a program to demonstrate the ‘if’ statement.

OUTPUT

In the above program if(x>y) then the condition will be false and ‘if’ block will never execute.

OUTPUT

if-else  statement

Syntax of an ‘if-else’ statement −

if else c programming Examples

Example: Write a program to take two numbers from user and find the grater between them using ‘if-else’ statement.

OUTPUT

Nested If else

In a C language, we can use if statement inside another if statement(s).

Syntax of an ‘nested -if’ statement −

Example: Write a program to take two numbers from user if both number are between 1 to 9 then print “Good” using nested-if statement.

OUTPUT

Nested if else statement

In a C language we can use if or if-else statement inside another if or  if-else statement(s).

Syntax of an ‘if-else’ statement −

Example: Write a program to take three numbers from user and find the grater among them using ‘nested if-else’ statement.

OUTPUT

If else ladder

some times if can be nested inside else as below

above statements can be written as

OUTPUT