The if_else statement is a powerful decision making tool. It
allows the computer to evaluate the expression. Depending on whether the value
of expression is 'True' or 'False' certain group of statements are executed.
The
syntax of if_else statement is:
if
(condition is true)
statement 1;
else
statement 2;
The
condition following the keyword is always enclosed in parenthesis. If the
condition is true, statements in then part are executed, i.e., statement1,
otherwise statement2 in else part is executed. There may be a number of
statements in then and else parts.
Example:
/* magic number program * /
main( )
{
int magic = 223;
int guess;
cout<<"Enter
your guess for the number \n";
cin >>guess;
if (guess = = magic)
cout<<"\n
Congratulation ! Right guess";
else
cout<<
"\n wrong guess";
}
No comments:
Post a Comment