Decision is needed to run any statement, depend on one or more condition.
For example, we want to decide whether a number we inserted is more than 60 or not.
The syntax is according o this format:
if (condition)
{statement}
Example:
if (x>60)
{cout<<”the number is more than 60”;}
We can add one more statement for the condition that opposite to the condition above, with else.
Example:
if (x>60)
{cout<<”the number is more than 60”;}
else
{cout<<”the number is not more than 60”;}
The complete syntax is shown by the example below:
#include "iostream.h"
#include "conio.h"
main()
{
int x;
cout<<"Insert a number: ";
cin>>x;
if (x>60)
{cout<<"the number is more than 60";}
else
{cout<<"the number is not more than 60";}
getch();
}
The result is looks like the picture below:
1. If we insert a number more than 60, for example 61, then the program will print: the number is more than 60, because the condition has TRUE value.
2. If we insert 60 or a number less than 60, for example 59, then the program will print: the number is not more than 60, because the condition has FALSE value.
0 comments:
Post a Comment