if Statement in C Language

Conditional Statements / Decision Making Statements in C Language





What are conditional Statements in C Language?

Conditional statements in C language are used to make decisions based on the condition result. If condition gives the true result then what decision has to make and if condition is false then what decision has to make. The execution flow changes based on the result evaluated by the condition this process is called decision making in C Language. Decision making statement decide the flow of execution of program.


Decision Making Statements in C Language,

1. if Statement
2. if – else Statement
3. Nested if – else
4. if else – if ladder
5. Switch Case


Before starting conditional statements let us look at relational operators or comparison operators in C Language.

Less rhan < ,             5<6 = True

Greater than > ,.       5>6 = False

Less than or equals to <=, 

                                   5<=6 = True

Greater than or equals to >=,   

                                    5>=6 = False

Exactly equals to ==,               

                                5==6 = False

Not equals to !=,      

                              5!=6= True.

All above comparison operators are used in decision making or conditional statements in C


1. if Statement in C Language:


if statement is very simple decision making statement which help to a certain statement or a block of statements to be executed or not. i.e. if condition becomes true the set of statements or a block of statements written in if block will get executed. If condition becomes false the set of statements or block will not get executed.

Syntax:

if (test expression)

{

//if block

// statements to execute if condition becomes true

}

if keyword followed by pair of parenthesis which consist test condition. If statement accepts Boolean values True or False. If condition becomes true then the statements written in if block will executed otherwise not.

In C language if condition is written by another way without using curly braces(curly brackets { }).

Syntax:

if ( test condition)

statement1;

statemet2;

//statement1 immediate to if condition get execute if condition is true.

In the above syntax pair of curly brackets are not used. This is valid syntax. Compiler will consider the if block statement which is very first or immediate to the if condition and it get execute if condition becomes true.

Click below for flowchart of if  Statement

Flowchartfor if statement


Program to demonstrate if statement in C Language.

/****************************************
Simple C program to check number is positive
**********************************/
#include<stdio.h>
#include<conio.h>
void main()
{
int a= 20;
printf("\t\t**Output\n");
if(a>0) //test condition
{
//if condition becomes true the below statement will execute
printf("%d is positive number\n",a);
}
printf("End of program");
}

**Output

20 is positive number

End of program


In above program variable a store integer value 20. test condition is given with if statement to check a is greater than 0. If test condition becomes true then the statement written in if block get execute.


Another program without using curly brackets for if statement.

/****************************************
Simple C program to check number is positive
**********************************/
#include<stdio.h>
#include<conio.h>
void main()
{
int a= 20;
printf("\t\t**Output\n");
if(a>0) //test condition
//if condition becomes true the below statement will execute
printf("%d is positive number\n",a);
printf("End of program");
}

**Output

20 is positive number

End of program


In this program if statement does not followed by the par of curly brackets. Compiler will consider the if block statement very first statement written after the if statement or condition and if condition becomes true this statement will get execute.


/****************************************
Simple C program to check number is positive
**********************************/
#include<stdio.h>
#include<conio.h>
void main()
{
int a= -20;
printf("\t\t**Output\n");
if(a>0) //test condition
{
//if condition becomes true the below statement will execute
printf("%d is positive number\n",a);
}
printf("End of program");
}

**Output

End of program

Compiler will not execute the statement written in if block because the value of a is -20 and it is compare with 0. so it is less than -20. Condition result is false so if block will not execute.



/****************************************
Simple C program to check number is positive
**********************************/
#include<stdio.h>
#include<conio.h>
void main()
{
int a= -20;
printf("\t\t**Output\n");
if(a>0) //test condition

//if condition becomes true the below statement will execute
printf("%d is positive number\n",a);

printf("End of program");
}

**Output

End of program

In this program now value of a is -20. Compiler will check the value of a and compare with 0. Condition becomes false because -20 is smaller value than 0. So now compiler will not execute if block statement. Compiler will execute secondment statement because this statement is not the part of if block.


Program to check number is even number

/****************************************
Simple C program to check number is even number
**********************************/
#include<stdio.h>
#include<conio.h>
void main()
{
int number;
printf("\t\t**Output\n");
printf("Enter any number to check it is even\n");
scanf("%d",&number);
if( number%2==0) //test condition
{
//if condition becomes true the below statement will execute
printf("%d is even number\n",number);
}
printf("End of program");
}

**Output

Enter any number to check it is even

44

44 is even number

End of program

To check number is even the simple logic is if the given number is divisible by 2 then the number is even. To check it remainder operator (%) is used. This operator will help to get the remainder. If remainder of division is 0 then this number is even.


The above program again executed and entered number 33. When 33 is divided by 2 the remainder will be 0 so this number is not even. So compiler will not execute the statement written in if block.


**Output

Enter any number to check it is even

33

End of program 

टिप्पणी पोस्ट करा

0 टिप्पण्या