Conditional Statements in C Plus Plus

Switch Statement in C++

The switch statement is a special multiway decision maker that tests whether an expression matches one of the number of constant values accordingly. switch statement allows the user to choose a statement or a group of statements among several alternatives. The switch statement is useful when a variable is compared with different constants, and in case it is equal to a constant, a set of statements would be executed. The general form of the switch statement is as follows: switch (expression) { case constant 1: statement; case constant 2: statement; ………. ………. case constant n: statement; default: statement; } In this general form: 1) The expression, whose value is being compared, may be any valid expression but not a floating point expression 2) The The value that follows the keyword case may only be constants. They cannot be expressions. They may be an integer or characters, but not the floating...

If-else Statement in C++

if-else statement if statement is most commonly used with the following format: if(expression) { statement 1 } else { statement 2 } where else part is optional. In this case, either of the two statements are executed depending upon the value of the expression. If the expression has a non-zero value (i.e., if the expression is true), then statement1 will be executed. Otherwise (i.e., if expression is false), statement2 will be executed. Example: Given here is a program which reads a number and then prints whether the given number is even or odd. Note: If we divide a number by 2 and if the remainder is 0 then the number is EVEN else the number is ODD #include<iostream.h> void main() { int n; cout << “Enter a number :”; cin >> n; if(n%2 == 0) cout << “The number is EVEN”; else cout << “The number is ODD”; }

If Statement in C++

if statement: The if statement is used to express the conditional expressions. If the given condition is true then it will execute the statements otherwise it will execute the optional statements. The basic simple structure of the if statement is shown below: if (expression) { //set of statements } The expression must be placed in round brackets as shown above. In this form, the set of statements would be executed only if the expression has a non-zero value (i.e., if expression is true). If the expression has a value zero (i.e., if expression is false), then the set of statements would be ignored by C++ compiler. The set of statements are skipped and the execution continues with the next statements. Example: Given below is a program which reads two numbers and then prints greater one using if statement. #include <iostream.h> void main() { int a, b; cout << “Enter Two...