Hello, C + + (19) "Teacher, did I pass the level four exam?" The--4.2 conditional selection statement

Source: Internet
Author: User


4.2 Item Selection Statements


"Teacher, did I pass the level four exam?" ”



If the teacher is asked this question, how will he answer? Yes, he will choose different answers according to different conditions:



If the test result is greater than or equal to 60, then answer: "Congratulations, you passed the exam";



Otherwise, answer, "I'm sorry you didn't pass the exam."



This is the conditional choice in the real world-making different moves based on different conditions. So, in C + + programs, how do we express this conditional choice?


4.2.1 If statement: If ... Just ...


In the real world, we always use the "if ...." sentence to express the conditions of choice, C + + also learn from us, provide if (if) keyword to form a conditional selection structure, to express the real-world conditions of choice, the syntax format is as follows:


if (conditional expression)
{
     Statement 1;
}
else
{
     Statement 2;
} 


When C + + executes an IF condition selection statement, the value of the conditional expression is evaluated first, and then the different code is executed according to the different selection of its value. If the value of the conditional expression is true, statement 1 is executed directly down, and vice versa, and the Else branch executes statement 2. By using conditional selection statements, you can change the path of the program execution according to the different values of the conditional expression, and you can implement different functions in statement 1 and statement 2 to achieve the purpose of performing different actions according to different conditions. The IF statement is executed as shown in procedure 4-1.



Now, you can use the IF statement to solve the above "level four exam has not been" the real problem:


// Subject to test results
int nScore = 0;
cout << "Please enter the test results:";
// enter test results
cin >> nScore;
// Calculate conditional expressions to determine whether the test results meet the conditions (60 or more)
// If the value of nScore is greater than or equal to 60, which meets the conditions, the value of the condition expression is true,
// directly enter the if branch for execution, and output a prompt to pass the exam
if (nScore> = 85)
{
     // perform a qualified action
     cout << "Congratulations, you passed the exam" << endl;
}
else // If the condition is not met, the condition expression is false, then the else branch is executed
{
     // perform unqualified actions
     cout << "Sorry, you did not pass this exam" << endl;
} 





Figure 4-1 Execution flow of a conditional selection structure



Here, first let the user enter the test results, and then in the IF statement in the conditional expression, it is compared with a standard value, that is, to determine whether the test results meet the criteria. If the test result is greater than or equal to 60, then the value of the conditional expression is true, which means that the program enters the if branch to execute and output the language of the prompt passed by the test. Conversely, if the test result is less than 60, the condition can not be satisfied, the value of the conditional expression is false, the program will go to the Else branch to execute, the output test did not pass the prompt. This allows the program to make different actions (output different prompts) based on different conditions (nscore greater than or equal to 60).



Although the form of the IF statement is simple, there are a few points to note in its use.


1. If it is not necessary, the Else branch in the IF statement can omit


Many times, we only care about conditions when the condition is true, only the conditions that meet the condition, we can omit the Else branch, only the if to judge the condition expression and subsequent statement 1 to meet the conditions of processing. For example, we only hint at the person passing the exam, and for those who fail the exam, they simply ignore it and express it with an IF statement:


// Omit the else branch, and only handle the conditions that meet the conditions
if (nScore> = 60)
{
     cout << "Congratulations, you passed the exam";
} 


2. If statements can be nested to express multiple levels of condition judgment



In an If statement, you can nest another if statement, which means to make further conditional judgments under a certain precondition, so as to express multiple levels of conditional judgment. For example, to compare the size of the input V1 and v2 two numbers, we need to first determine whether the two are equal, under unequal preconditions, and then continue to judge the size of the relationship between the two, using a nested if statement to express it is:


cout << "Please enter two integers:" << endl;
int v1, v2;
// Get the number entered by the user
cin >> v1 >> v2;
if (v1! = v2) // determine whether v1 and v2 are equal, if not, continue to determine the size
{
     // second-level if statement
     // If not equal, continue to determine whether v1 is greater than v2
     if (v1> v2) // greater than
     {
         cout << "v1> v2" << endl;
     }
     else // less than
     {
         cout << "v1 <v2" << endl;
     }
}
else // v1 and v2 are equal
{
     cout << "v1 == v2" << endl;
}


3. If statements can be parallel



If there are multiple conditions at the same level, you can use a side-by-side conditional selection statement to implement it. The syntax format is as follows:


if (conditional expression 1)
{
     Statement 1;
}
else if (conditional expression 2)
{
     Statement 2;
}
// ...
else if (conditional expression n)
{
     Statement n;
}
else
{
     Statement n + 1;
} 


At execution time, the value of the conditional expression 1 is evaluated first, if its value is true, then the execution of statement 1 is entered into its branch, then the entire side-by-side condition selection statement is executed, and if its value is false, the value of the conditional expression 2 continues to be evaluated downward, similarly, if its value is true, Executes statement 2 into its branch, then ends the entire statement, and if its value is false, continues the same calculation process downward. Until finally, if all the conditional branches are not satisfied, go to the last else branch to execute and end the entire statement. For example, the comparison between V1 and V2, which we implemented with nested IF statements, is actually three different cases: either greater than, or less than, or equal to. Therefore, it is also possible to use a juxtaposition of conditional structures:


if (v1> v2) // first determine if v1 is greater than v2
{
     cout << "v1> v2" << endl;
}
else if (v1 <v2) // If the first condition is not satisfied, then determine whether v1 is less than v2
{
     cout << "v1 <v2" << endl;
}
else // If v1 is neither greater than v2 nor less than v2, then it must be equal to v2
{
     cout << "v1 == v2" << endl;
} 


It is important to note that when a side-by-side condition statement is executed, the conditional expression of the side-by-side is evaluated one after the other, until a condition expression is true before it enters its branch and ends the entire statement. Therefore, we always put a higher probability of satisfying the conditions of judgment placed in the position of comparison, I hope that if the statement at the outset to meet the conditions of the branch, to avoid the difficult to meet the criteria to judge the useless calculation.



It is also important to note that a side-by-side conditional selection statement executes only one of these branches, and if more than one condition expression is true, only the branch that encounters the first condition expression true from top to bottom is executed. For example:


int nScore = 91;
if (nScore> = 60) // the first conditional expression is true, enter the execution and directly end the entire statement
{
     cout << "Congratulations, you passed the exam" << endl;
}
// Because the first branch has executed and ended the entire statement
// So even if the second conditional expression is true, it will not be executed
else if (nScore> = 85)
{
     cout << "Great, your grades are excellent" <<< endl;
} 


Therefore, when you use a side-by-side condition to select a statement, you should avoid overlapping of conditional ranges, and do not allow multiple conditional expressions to be true at the same time to avoid procedural logic confusion.



Learn more: Use the "?:" condition operator to express conditional judgments and simplify the code



The so-called conditional operator, which allows an expression to have different values based on different conditions. It is the only ternary operator in C + +, which has the following syntax format:


Conditional expression? Expression 1: Expression 2


Like a conditional statement, when executed, it evaluates the value of the conditional expression first and, if it is true, then evaluates the expression 1 and takes it as the final result value of the entire expression. Conversely, the value of expression 2 is evaluated, as well as the final result value of the entire expression.



By using the conditional operator, we can easily implement the function of the condition selection, which is simple (to deal with both the condition and the case, and the process is relatively simple), so as to simplify the code. For example, we want to choose the larger one from two numbers, using the IF condition statement for size comparison, which can be implemented as:


int a , b;  0;  if (a > b) {m = A;} else{m = b;}         


If you use conditional operators, the above criteria are selected with a single statement:


int a , b;  Enter a, b ... int m = (a>b)? a:b;   


When executing "(a>b) a:b", The Value of "(A>B)" is computed as well, and if the value of a is larger, that is, the value of the conditional expression is true, the second operand A is used as the value of the entire expression, and then the value is assigned to M. In this way, M becomes the larger of the two numbers. Conversely, if the value of B is larger, then the value of the conditional expression is false, and the third operand, B, is the value of the entire expression, and the last assignment is to M, when M is still the larger of the two numbers. Thus, a short statement of the original need for the entire if condition statement to achieve the function. Simplifying the code is the primary function of the conditional operator.






Original address: http://www.cnblogs.com/nihaoCPP/p/4091693.html



Hello, C + + (19) "Teacher, did I pass the level four exam?" The--4.2 conditional selection statement


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.