If Statement in C programming with Examples

 

 

Hello friends how are you, today in this blog i will teach you what is if statement, syntax of if , how if statement works and many programs using if statement in a very simple way.

Let's start

👉Syntax


1.If the condition is true its body execute otherwise does not execute.
2.In the case of if in the place of condition always zero and non-zero value is checked in which zero means condition false and non-zero means condition true

Example 1 

#include<stdio.h>
int main()
{
//Assigning value to the variable
int x=50,y=20;
//checking the condition
if(x>y)
{
printf("x is greater than y");
}
}
### Output ###
x is greater than y

Example 2 

#include<stdio.h>
int main()
{
//Assigning value to the variable
int x=50,y=20;
//checking the condition
if(x<y)
{
printf("x is greater than y");
}
}
/*
### Output ###
condition is false so there is no output
*/

Example 3

#include<stdio.h>
int main()
{
if(8)
{
printf("Hello");
}
}
/*
### Output ###
Hello
*/

In case of at the place of condition always zero and non-zero value is checked in which zero means condition false otherwise true. Here in above example 8 is a non-zero value so condition will be true and output will be Hello

Example 4

#include<stdio.h>
int main()
{
if(0)
{
printf("Hello");
}
}
/*
### Output ###

*/

0 means condition false so there will be no output here.

Example 5

#include<stdio.h>
int main()
{
if(8,9,6,0,0,7,8)
{
printf("Hello");
}
}
/*
### Output ###
Hello
*/

In this situation always last value is checked and at last there is 8 so condition is true because 8 is non-zero value.

Program: Greatest Value three numbers

#include<stdio.h>
int main()
{
int no1,no2,no3;
printf("Enter number1:");
scanf("%d",&no1);
printf("Enter number2:");
scanf("%d",&no2);
printf("Enter number3:");
scanf("%d",&no3);
if(no1>no2&&no1>no3)
printf("%d is greaest value",no1);
if(no2>no1&&no2>no3)
printf("%d is greaest value",no2);
if(no3>no2&&no3>no1)
printf("%d is greaest value",no3);

}
/*
### Output ###
Enter number1:84
Enter number2:95
Enter number3:67
95 is greatest value
*/

Here I am requesting if you found this post was helpful for you then let me know by your comment in below comment box and share it with your friend. Also you can contact me on Instagram @rajusunagar_

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

Thank you!!.......

Post a Comment

0 Comments