C if Statement Basic

Generally, we tend to make choices by staring at the present circumstances.

Example:

If she says affirmative then I’ll dance.
If you wish this tutorial then I’ll write consequent one too.

So, largely choices depend upon some reasonable conditions. C language conjointly uses call management directions. it’s in the main 3 call management directions.

if
if-else
switch

What are choices in programming?

Till currently we’ve got solely used sequence management instruction. therein statements are dead within the order they seem.

however throughout serious programming, we regularly ought to run one set of directions below one condition and a fully completely different set of instructions if the condition fails.

So the choice of directions on the idea of some condition is named call in programming.

Now we are going to proceed any to grasp the primary call management instruction i.e. if

C if Statement

Here if could be a keyword. it’s wont to check conditions before capital punishment a group of statements. Its general type is given below.

if(Condition)
one
Statement a pair of
…. And so on
}

The keyword if tells the compiler to follow call management instruction. that the compiler checks the condition, if it seems to be true then it executes the statements that are a gift within the body of if statement. however if the condition is fake then compiler simply skip the body of if statement.

Now some queries might arise in your mind.

What are the conditions? however am I able to show conditions?
In C, conditions are expressed by victimisation relative operators. By victimisation relative operators we will compare 2 values – if it’s larger than, less than, adequate and not equal to the opposite worth.

Check out below table to grasp relative operators.

if the statement

A flow chart of if the statement is given below.

if the statement

Let’s try and perceive this basic program.

Program

/*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

enter a number less than 10 7 
number is less than or equal to 10 

Note: currently we are going to begin analysing the program when main().

within the 1st statement, we’ve got declared associate degree number variable n.

within the second statement, we tend to simply print a message to enter variety but ten victimisation printf() operate.

within the third statement, we tend to are acceptive worth from the user and storing it in n variable.

currently, within the fourth statement, we’ve got used a condition that is n.

Leave a Comment

error: Alert: Content is protected!!