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) True-block; else False-block; Statement-x In if-else statement, first the expression or condition is evaluated to 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. Test expression True False True Block False-Block Statement-X #include <iostream> using namespace std; int main() int num, rem; cout<< "\n Enter a number: "; cin>>num; rem = num % ; if (rem== ) cout<< "\n The given number" <<num<< " is Even"; else cout<< "\n The given number "<<num<< " is Odd"; return ; Output Enter number: The given number is Even Illustration . C++ program to find whether the given number is even number or odd number using if-else statement 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. Chapter Page - - . . Nested if An if statement which contains another if statement is called nested if.
The nested can have one of the following three forms. . If nested inside if part . If nested inside else part .
If nested inside both if part and else part The syntax of the nested if: if (expression- ) if (expression- ) ; else ; else body of else part; If nested inside if part If nested inside else part if (expression- ) body of true part; else if (expression- ) ; else ; if (expression) if (expression) ; else ; else if (expression) ; else ; If nested inside both if part and else part In the first syntax of the nested if