



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Conditional Statements in C++ or if Statements. Conditional Statements in C++ or if Statements.
Typology: Assignments
1 / 5
This page cannot be seen from the preview
Don't miss anything!




Conditional statements , also known as selection statements, are used to make decisions based on a given condition. If the condition evaluates to True, a set of statements is executed, otherwise another set of statements is executed. The if Statement: The if statement selects and executes the statement(s) based on a given condition. If the condition evaluates to True then a given set of statement(s) is executed. However, if the condition evaluates to False, then the given set of statements is skipped and the program control passes to the statement following the if statement. The syntax of the if statement is 1 2 3 4 5 if (condition) { statement 1; statement 2; statement 3; } The if-else Statement: The if – else statement causes one of the two possible statement( s) to execute, depending upon the outcome of the condition. The syntax of the if-else statements is 1 2 3 4 5 6 if (condition) // if part { statement1; statement2; } else // else part statement3; Here, the if-else statement comprises two parts, namely, if and else. If the condition is True the if part is executed. However, if the condition is False, the else part is executed. To understand the concept of the if -else statement, consider this example. Example : A code segment to determine the greater of two numbers 1 2 3 4 if(x>y) cout<<"x is greater"; else cout<<"y is greater"; Nested if-else Statement: A nested if-else statement contains one or more if-else statements. The if else can be nested in three different ways, which are discussed here.
- The if – else statement is nested within the if part. The syntax is
if (condition1) { statement1; if(condition2) statement2; else statement3; } else statement4;
- The if-else statement is nested within the else part. The syntax is 1 2 3 4 5 6 7 8 9 10 if(condition1) statement1; else { statement4; if (condition2) statement2; else statement3; } statement5; - The if-else statement is nested within both the if and the else parts. The syntax is 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 if(condition1) { statement1; if (condition2) statement2; else statement3; } else { statement4; if (condition3) statement5; else statement6; } statement7; To understand the concept of nested if-else, consider this example. Example : A code segment to determine the largest of three numbers 1 2 if (a>b) { if (a>c) cout<<"a is largest";
The output of this program is Enter an alphabet: $ It is not an alphabet Conditional Operator as an Alternative: The conditional operator ‘? :’ selects one of the two values or expressions based on a given condition. Due to this decision-making nature of the conditional operator, it is sometimes used as an alternative to if-else statements. Note that the conditional operator selects one of the two values or expressions and not the statements as in the case of an if-else statement. In addition, it cannot select more than one value at a time, whereas if-else statement can select and execute more than one statement at a time. For example, consider this· statement. 1 max = (x>y? x : y) This statement assigns maximum of x and y to max The switch Statement: The switch statement selects a set of statements from the available sets of statements. The switch statement tests the value of an expression in a sequence and compares it with the list of integers or character constants. When a match is found, all the statements associated with that constant are executed. The syntax of the switch statement 1 2 3 4 5 6 7 8 9 10 switch(expression) { case
cin>>x; int x; switch(x) { case l: cout<<"Option1 is selected"; break; case 2: cout<<"Option2 is selected"; break; case 3: cout<<"Option3 is selected"; break; case 4: cout<<"Option4 is selected; break; default: cout<<"Invalid option!"; } In this example, depending upon the input, an appropriate message is displayed. That is, if 2 are entered, then the message Option 2 is selected is displayed. In case, 5 is entered, then the message Invalid option! is displayed. Similar to if and if-else statements, switch statements can also be nested within one another. A nested switch statement contains one or more switch statements within its case label or default label (if any).