![if !IE]> <![endif]>
In the previous examples of if, you have seen so for allow you to execute a set of statement is a condition evaluates to true. What if there is another course of action to be followed if the condition evaluates to false. There is another form of if that allows for this kind of either or condition by providing an else clause. The syntax of the if-else statement is given below:
if ( expression)
In if-else statement, first the expression or condition is evaluated either true of false. If the result is true, then the statements inside true-block is executed and false-block is skipped. If the result is false, then the statement inside the false-block is executed i.e., the true-block is skipped.
using namespace std;
int num, rem;
cout<< "\n Enter a number: ";
rem = num % 2;
cout<< "\n The given number" <<num<< " is Even";
cout<< "\n The given number "<<num<< " is Odd";
Enter number: 10
The given number 10 is Even
In the above program, the remainder of the given number is stored in rem. If the value of rem is zero, the given number is inferred as an even number otherwise, it is inferred as on odd number.
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.