C - If Else Statement
![c-if-else-statement](https://wikitechy.com/cdn/tutorials/c-programming/c-if-else-statement.gif)
Learn C - C tutorial - C if else statement - C examples - C programs
C If Else Statement- Definition and Usage
- In these type of statements, group of statements are executed when condition is true.
- If condition is false, then else part statements will be executed.
![C If Else Statement C If Else Statement](https://wikitechy.com/cdn/tutorials/c-programming/c-if-else-statement.png)
C Syntax
if(condition is true)
{
//execute your code
}
else
{
//execute your code
}
Sample - C Code
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter a number \n");
scanf("%d",&a);
if(a>0)
{
printf("The Given Number is Positive");
}
else
{
printf("The Given Number is Negative");
}
getch();
}
C Code - Explanation :
![](https://wikitechy.com/cdn/tutorials/c-programming/if-else-code-explanation.png)
- Here we declare the variable “a” as integer .
- In this statement we get the value of the variable “a” using scanf statement.
- In this statement we check the condition that “a” value is greater than “0”, if condition satisfies it prints “The Given Number is Positive”.
- Here “else statement” works which prints “The Given Number is Negative”.
Sample Output - Programming Examples
![](https://wikitechy.com/cdn/tutorials/c-programming/if-else-sample-output.png)
- Here in this output we enter a value of the variable a= 3 so the given number is greater than “0” so the “if” section will be executed by displaying the output as “The Given Number is Positive”.
![](https://wikitechy.com/cdn/tutorials/c-programming/if-else-output.png)
- Here in this output we enter a value of the variable a=-1 so the given number is less than “0” so the “else” section will be executed by displaying the output as “The Given Number is Negative”.