The if-else Statement:
- The if statement that you have been using executes a statement or block of statements if the condition specified is true.
- Program execution then continues with the next statement in sequence.
- Of course, you may want to execute one block of statements when the condition is true and another set when the condition is false.
- An extension of the if statement called an if-else statement allows this.
- if x is dividable by 2 then x is even number else x is odd
- Used for double selection.
Syntax of if-else statement:
Example if else Statement:
write a C++ Program to Check Odd and Even Numbers using if else statement
Output:
Coding:
//Write a C++ Program to Check Odd and Even Numbers
#include<iostream>
using namespace std;
int main(){
int number;
cout<<"Enter any Number: ";
cin>>number;
if(number%2==0){
cout<<"You enter Even Number"<<endl;
}
else {
cout<<"You enter Odd Number"<<endl;
}
return 0;
}
0 Comments