C if Statement – Part 1

Generally we take decisions by looking at the current circumstances.

Example:
If she says yes then I will dance.
If you like this tutorial then I will write the next one too.

So, mostly decisions depend on some kind of conditions. C language also uses decision control instructions. It has mainly three decision control instructions.

  • if
  • if-else
  • switch

What are decisions in programming?

Till now we have only used sequence control instruction. In that statements are executed in the order they appear. But during serious programming we often need to run one set of instructions under one condition and entirely different set of instructions if the condition fails.

So the selection of instructions on the basis of some condition is called decision in programming.

Now we will proceed further to understand the first decision control instruction i.e. if

C if Statement

Here if is a keyword. It is used to check conditions before executing a set of statements. Its general form is given below.

if(Condition)
{
Statement 1
Statement 2
…. And so on
}

The keyword if tells the compiler to follow decision control instruction. So the compiler checks the condition, if it turns out to be true then it execute the statements which are present in the body of if statement. But if the condition is false then compiler just skip body of if statement.

Now some questions may arise in your mind.

What are conditions? How can I show conditions?

In C, conditions are expressed using relational operators. By using relational operators we can compare two values – if it is greater than, less than, equal to and not equal to the other value.

Checkout below table to understand relational operators.

C Relational Operators

A flow chart of if statement is given below.

C if Statement Flow Chart
Flow Chart of if Statement – Source
/*Program of if statement */

#include<stdio.h>

void main( ) 
{  
int n;  
printf("Enter a number less than 10" );
scanf("%d",&n);
if (n<=10)
{
 printf ("Number is less than or equal to 10" ); 
}
}
Output

output


Lets try to understand this basic program.

Note: Now we will start analysing the program after main().

1. In the first statement we have declared an integer variable n.

2. In the second statement we just print message to enter a number less than 10 using printf() function.

3. In the third statement we are accepting value from the user and storing it in n variable.

4. Now in the fourth statement we have used a condition which is n<=10. It means the compiler will check whether the value in n variable is less than or equal to 10. In our test run we have entered 7, so the condition turns out to be true and a message is printed.

5. In case if you will enter value greater than 10 then it will skip the body of if statement and the program will end.

if statement is most frequently used in C programming. I hope you must have understand the basic concept of using if statement.

Now write some programs using if statement to familiarise with it. You can get some program on if statement here.

Read: C if Statement – Part 2

4 thoughts on “C if Statement – Part 1”

Leave a Comment

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