C if ... else Statement | C Programming Tutorials
Recommended Topics before You Continue :
Last we have seen if statement in c. If the condition in if statement becomes false (0) the statements in if does not execute and there is no output of code.If-else
Syntax:if(condition) { statement; } else { statement; }Note: If the condition evaluates to false (0) the statements in if part will not get executed but statements in else part will get. This means if else part evaluates if part will not and vice versa.
For Example:
Let us take example of even number again but this time with else part.
#include<stdio.h> void main() { int num; // Declared a variable to save integer printf("\n Enter any number:- "); scanf("%d",&num); // Accepting and saving integer in 'num' variable if(num%2==0) // Checking whether value in num is divisible by 2 printf("Given number is even"); // I have not used { } braces as we have to execute single statement. else printf("Given number is odd"); If condition evaluates to false (0) this part will get executed }
Explanation:
If 'num' holds value which is odd then the condition becomes false (0) and if part will be skipped and else will get executed and a message will be displayed on screen i.e. "Given number is odd".
Prev - If statement in C Next - Nested if-else