Simple Example Program For If..Else : Example 2
Definition
- The if statement executes based test expression inside the braces.
- If statement expression is to true, If body statements are executed and Else body statements are skipped.
- If statement expression is to false If body statements are skipped and Else body statements are executed.
- Simply, Block will execute based on If the condition is true or not.
- IF conditional statement is a feature of this programming language which performs different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false. Apart from the case of branch prediction, this is always achieved by selectively altering the control flow based on some condition.
Syntax
if (expression) {
doSomething();
}
else {
doSomethingElse();
}
Syntax Example
for example, In c
if (i == 3) {
doSomething();
}
else {
doSomethingElse();
}
Syntax Explanation
Consider above example syntax,if (i == 3)
- which means the variable i contains a number that is equal to 3, the statements following the doSomething() block will be executed.
- Otherwise variable contains a number that is not equal to 3, else block doSomethingElse() will be executed.
Example Program for If Else : Voting Check Example : 2
Example Program For If ... Else Example : 2
/* Example program to check whether the given age is eligible for vote or not */
// Header Files
#include<stdio.h>
#include<conio.h>
//Main Function
int main()
{
// Declaring the variable age
int age;
// Reading the age
printf("Enter your age:");
scanf("%d",&age);
// Checking the condition with If-Else statement
if(age>=18)
{
// Printing the output if the condition is true
printf("You are eligible for vote");
}
else
{
// Printing the output if the condition is fails
printf("You are not eligible for vote");
}
// Output screen waits until get a character
getch();
// Returning to the main function
return 0;
}
Sample Output:
Enter your age:25
You are eligible for vote
Enter your age:15
You are not eligible for vote
Simple Example Programs In C Language
- Hello World C Language Example Program
- If Statement Example Program In C Programming Language
- If else Statement Example Program In C Programming Language
- Simple Example Program For If..Else : Example 2
- Switch Case Statement Example Program In C Programming Language
- Simple Example Program For Printf
- Simple C Program for Switch case to Find weekdays name with weekday number
- Simple Program to Print All ASCII Value Table in C Programming
Read More Articles
- Use of getch(),getche() and getchar() in C
- Switch Case Statement Example Program In C Programming Language
- C Character Set
- Convert a Floating-point value to an Integer in C
- Data Input and Output gets and puts Example Program In C
- Special Operators In C
- Pointer Representation and Pointer Example Programs
- C Data Input and Data Output
- Simple While Loop Example Program In C Programming Language
- Data Output printf and putchar Example Program In C
- C Introduction
- C Operators
- Storage Classes In C
- C Pointers
- File Management
- C Identifiers
- Loop Control Statements
- Hello World - Simple C Program
- C Array
- Single Character Output Function : putchar()
- C Reserve Words
- C Specific Properties and Implementation
- If else Statement Example Program In C Programming Language
- If Statement Example Program In C Programming Language
- Confusing Array in C ( Array Representation and Initialization )