Introduction to Other Selection Control Statements

Conditional Operator ?:

The conditional operator (?:) is another form of a double selection statement. It also allows for performing one action when a condition is true and another when the condition is false. The conditional operator is a ternary operator because it takes three operands. The first operand is a condition, the second is the action to be performed if the action is true and the third is the action to be performed if the action is false. A full statement of the conditional operator is known as the conditional expression. A conditional expression is written as follows condition ? Action when true : action when false;

Using again the example of our girl and talking the possibility that she is mature to get married at 21 plus and young below that age, we can use the conditional expression as follows; 

#include<iostream>
using namespace std;
void CurrentAge( int herAge)
{ int age=herAge;
cout<<"At "<<age<<", she is " <<((age>=21) ? "old enough to marry" : "able to marry if she gets permission of her parents ");
}
int main()
{
int Age=18;
CurrentAge(Age);
cout<<"\nThis is my opinion as her parent "<<endl;
}
Output: Age 25 At 25, she is old enough to marry This is my opinion as her parent

Output: Age 18 At 18, she is able to marry if she gets permission of her parents This is my opinion as her parent

Please take note of points where the ? and : are placed. Mind also about the use of the quotation marks in the statements to printed if the condition is right or false.

Switch Statement

The switch statement is used for multiple selections in C++. It performs many possible actions holding for a particular condition. With the switch you can perform different actions using a condition for each. The switch statement contains two elements, controlling expression, the case label and default case. switch compares the controlling statement (which can also be an expression) with the case values and then performs an action corresponding with a particular case. The switch statement is written as follows.  
switch(controlling expression) { case a: action; break; case b: action; break; . . . Default: Action; break; }

After every action a break statement is made to inform the compiler of the end of thecase in question. If the break statement is not including the program will consider all that falls a particular case to be actionable until reaches the end of the switch. 

A default case is included to provide for the alternative action in case the program is not able to find a case label that matches with a controlling expression. At times the values provided could fall outside the tested conditions and you want the user the get information relating such a value. In case there is no match and no default case the program will continue to the first statement after the switch without any action and this could lead to unintended outcome.

We use again the example of a girl. If she is 21 she is mature to marry, at 18 she requires the permission of her parents, at 17 she is approaching marriage age but not ready yet and at 15 she is so young to even consider marriage. Our default case is any age not included in our four areas we are testing. 

#include<iostream>
using namespace std;
void CurrentAge( int herAge)
{ int age=herAge;
switch (age)
{ case 21: cout<<"At "<<"age<<", she is old enough to get married "<<"endl;
break;
case 18:
cout<<"Yes at "<<"age<<", she can get married but with permission of her parents "<<"endl;
break;
case 17:
cout<<"Although at "<<age<<" she is approaching 18, still she is not ready for marriage married. ";
break;
case 15:
cout<<"No at "<<age<<", she is so young to get married "<<endl;
break;
default:
cout<<"The age of "<<age<<", cannot be used to determine the status of the girl. ";
break;
} }
int main()
{
int Age=18;
CurrentAge(Age);
cout<<"This is my opinion as her parent "<<endl;
}
Output: case 21 At 21, she is old enough to get married This is my opinion as her parent
Output: case 18 Yes at 18, she can get married but with permission of her parents This is my opinion as her parent
Output: case 17 Although at 17 she is approaching 18, still she is not ready for marriage married. This is my opinion as her parent
Output: case 15 No at 15, she is so young to get married This is my opinion as her parent
Output : Default The age of 10, cannot be used to determine the status of the girl. This is my opinion as her parent
You can include the values included in the outputs to test the program yourself.

PREV:Introduction to Control Statements


NEXT:Repetition Statements

No comments:

Post a Comment