C if

In C, if is a keyword used to implement decision control instruction.
The condition following the keyword if is always enclosed within a pair of parentheses.
The general syntax of if statement looks like below:

Syntax of if statement:

Above statement or block of statements will be executed when a condition is true. Otherwise, these statements get skipped.

Flowchart of if statement:

if statement flowchart

First of all, a condition is check and then, depending on whether the condition is true (‘non zero’) or false (‘zero’), it transfers the control to a particular statement.
If the condition is true then the statement inside the if block gets executed otherwise statements get skipped.

Example:

Output:

Explanation:

In above program, if condition is true therefore the output is ‘b is greater than a’ and also ‘a is greater than b’ because statements execute after if statements.
Whereas, if a condition was false then the output will be only ‘a is greater than b’.

Examples:

Condition will be any of the following to print output ‘hi’:

Forms of if statements

There are many forms of if statements, check out Forms of if: page for more details. These variations are used in different ways.

Help others by sharing the content!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.