C if Statement

In this tutorial you will learn about the C if Statement and its application with practical example.

C if Statement

The c If a statement is a decision-making statement. It allows a block of code to be executed only when a specified condition is true. If a statement evaluates a boolean expression followed by one or more statements. The given boolean expression results in a boolean value that can only be either true or false. If the statement is always used with a condition. The condition is evaluated first before executing any statement inside the body of If. The statements inside the “if” body only execute if the given condition returns true. If the condition returns false then the statements inside “if” body are skipped

Need of If Statement In C

When we want to execute a block of code only when a given condition is true then we use if statement. It is one of the most simple decision-making statements.

Types of if Statement In C

The if statement can be used in many forms depending on the situation and complexity. There are following four different types of if statements:

If Statement Flowchart

The flowchart image illustrates the working of if statement:

c-if-statement

if Statement Syntax

Below is the general syntax of if statement in c programming:

Syntax:-

Working Of if Statement

Here, the if statement evaluates the test condition inside the parenthesis (). The Condition is a Boolean expression that results in either True or False. If it results in True then statements inside if the body is executed. If it results in False then execution is skipped from if body.

if Statement Example

Below is a simple example to demonstrate the use of if in c programming:

Example:-

The condition (num > 50) specified in the “if” returns true for the value of num, so the statement inside the if the body is executed. Then we would see the following output.

Output:-

c_if_statement

Multiple Conditions In if Statement

Multiple conditions in an if statement can be added using either logical AND (&&) and/or OR(||) operators. If we are using logical AND (&&) operator then the if statement will be executed if both the conditions are true. And if we are using logical OR(||) operator then the if statement will be executed if either of those conditions is true.

Example 1:-

Here, if statement will be executed if both the conditions are true. But, if the first condition is false it will not test the second condition and directly execute the else statement if provided. It will test the second condition only when the first condition is true.

Example 2:-

Here, if statement will be executed if either of those conditions is true. But, if the first condition is true it will not test the second condition and execute the if part. It will test the second condition only when the first condition is false.

Relational Operators In if Statement

Relational operators can be used for making a decision and testing conditions, which returns true or false. C has a set of relational operators that can be used in test expression.

  • < less than
  • <= less than or equal to
  • > greater than
  • >= greater than or equal to
  • == equal to
  • != not equal to

Notice:- The equal to (==) is different from the assignment operator (=). The equal to (==) operator is used to test if two values are equal to each other.

In this tutorial we have learn about the C if Statement and its application with practical example. I hope you will like this tutorial.