Page 45 - Modular_V2.0_C++_Flikpbook
P. 45
• if(x >= y) // Corrected logical condition
cout<< x << " is greater than or equal to " << y;
Let us take an example given below: Clickipedia
void main()
{ You can also write conditional
clrscr(); statements without the braces if
there is only one line of code.
int a;
cout<< "Enter any number: ";
cin>> a;
if(age > 100)
{
cout<< "The number "<<a<<" is greater than 100";
}
getch();
}
In the above code, if the value in a is greater than 100, the statement written inside the if
statement is executed. Otherwise, C++ skips the entire block and continues with the program.
Program1: To take a number from the user as input and print the cube of it. Show a message
"Too Large Number", if user enters a number greater than 20.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n, cu;
cout<< "Enter a number less than 21: ";
cin>> n; DOSBox 0.74, Cpu speed: max 100% cycles, Frames
if(n <= 20) Enter a number less than 21: 20
{ The cube of 20 is: 8000
cu = n * n * n;
cout<< "The cube of " << n << " is: " << cu;
}
if(n > 20)
DOSBox 0.74, Cpu speed: max 100% cycles, Frames
{ Enter a number less than 21: 25
cout<< "Too Large Number"; Too Large Number_
}
getch();
}
43
Conditional Statements

