Advance C++


if..else Statement tutorials
-
if..else checks the condition, and if result returns true, it execute statements for true block, if result turn out to be false, then it will execute statements given after else block.
Syntax:If ( Conditions ){Statements if condition is true;….}else{Statements if condition is false;….} |
|
C++ Program to check ASCII value of 'A' and 'a' |
|
|
// program to check ASCII value of 'A' and 'a'# include <iostream.h>void main(){if('A'<'a')cout<<"ASCII value of A is smaller than a";elsecout<<"ASCII value of A is BIGGER than a";} |
Output |
ASCII value of A is smaller than a |
Note: In this program we are comaring capital letter 'A' with lowercase 'a'. for your information, ASCII value of upper case 'A' is 65, while lower case 'a' is 97. So this program will display ASCII value of A is smaller than a.
C++ Program to Enter a number a print remark whether no is Positive no, or Negative no or Zero using if…else statements |
|
|
// program to check no and print +ve,-ve or zero remark using If..Else# include <iostream.h>void main(){int no;cout<<"Enter any no ";cin>>no;if(no>0)cout<<no<<" is positive no";else{if (no<0)cout<<no<<" is negative no";elsecout<<"Entered no is zero";}} |
Output |
Enter any no : 1010 is positive no.Enter any no : -7-7 is negative noEnter any no : 0Entered no is zero |
C++ Program to enter a no and print Odd or Even no remark |
|
|
// program will enter a no and print Odd or Even no remark# include <iostream.h>void main(){int no;cout<<"Enter any no : ";cin>>no;if(no%2 == 0)cout<<no<<" is even no";elsecout<<no<<" is odd no";} |
Output |
Enter any no : 1010 is even noEnter any no : 1515 is odd no |