c + + basic to advanced selection statement

preface

Sometimes, when we write a topic, we see that a is equal to 1 when > = 0 and - 1 when < 0. How should we write it

Select statement

Select the word as its name

Basic structure

if(Judgment conditions){
	If the judgment condition is true, the operation to be performed
}else{
	If the above conditions are not true, the operation to be performed( else Not necessarily)
}

As we said in the preface, the title is:

if(a>=0){
	a=1;
}else{
	a=-1;
}

Topic practice

Buy batteries in the supermarket

Yasi supermarket sells batteries, 2 per battery.5 Yuan, 20% off if the quantity reaches 10. Write a program to enter the number of batteries
ļ¼ŒOutput how much it costs to buy the battery (2 digits after the decimal point).

Input format:
One line: an integer indicating the number of batteries.

Output format:
One line: a real data, indicating how much it costs to buy the battery, with 2 digits after the decimal point.

limit:
Space limit: 128 MByte
 Time limit: 1 second

Sample:

Input:
15

Output:
30.00

AC Code:

#include <iostream>
using namespace std;
int main(){
	double ans; 
	int n;	//Number of batteries
	scanf("%d",&n);	//input
	if(n>=10){	//If the number of batteries is greater than 10 
		ans=n*2.5*0.8;	//20% off 
	}else{	//If there are no more than 10 
		ans=2.5*n;	//The answer remains the same 
	}
	printf("%.2lf",ans);	//Keep two decimal places
	return 0; 
}

Examples of key points

Now, you can use the if statement to solve the real problem of "have you passed the exam":

// Subject to examination results
int nScore = 0;
cout<<"Please enter the test result:";// Enter test scores
cin>>nScore;
// Calculate the conditional expression to judge whether the test score meets the conditions (greater than or equal to 60)
// If the value of nScore is greater than or equal to 60, the condition is met, and the value of the condition expression is true,
// Enter the if branch directly and output the prompt of passing the exam
if(nScore >= 60)
{
    // Execute qualified actions
    cout<<"Congratulations, you passed the exam"<<endl;
}else// If the condition is not met and the value of the condition expression is false, enter the else branch for execution
{
    // Perform actions that do not meet the conditions
    cout<<"It's a pity that you didn't pass the exam"<<endl;
}
 

Although the form of if statement is simple, there are several points to pay attention to in its use.

  1. If not necessary, the else branch in the if statement can be omitted

    Most of the time, we only care about the case when the condition is true and only deal with the case that meets the condition. At this time, we can omit the else branch and only keep the if to judge the conditional expression and the subsequent statement 1 to deal with the case that meets the condition. For example, we only prompt those who have passed the exam, but ignore those who have not passed the exam. The if statement is:

// Omit the else branch and process only the qualified cases
if( nScore >= 60 )
{
    cout<<"Congratulations, you passed the exam";
}

2.if statements can be nested to express multi-level conditional judgment

Another if statement can be nested in an IF statement to make further conditional judgment under a certain premise, so as to express multi-level conditional judgment. For example, to compare the size relationship between the input v1 and v2 numbers, we need to first judge whether they are equal, and then continue to judge the size relationship between them under the premise of inequality. We use nested if statements to express it, which is:

cout << "Please enter two integers:" << endl;
int v1, v2;
// Gets the number entered by the user
cin >> v1 >> v2;
if( v1 != v2 )
// Judge whether v1 and v2 are equal. If not, continue to judge the size
{
    // Second level if statement
    // If not, continue to judge 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;
}
  1. if statements can be juxtaposed

    If there are multiple conditions at the same level, you can use parallel condition selection statements to implement them. The syntax format is as follows:

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

During execution, the value of conditional expression 1 will be calculated first. If its value is true, it will enter its branch execution statement 1, and then end the execution of the whole parallel conditional selection statement; If the value is false, the value of conditional expression 2 will continue to be calculated downward. Similarly, if the value is true, enter its branch execution statement 2, and then end the whole statement. If the value is false, the same calculation process will continue downward. Until the end, if all conditional branches cannot be met, enter the last else branch to execute and end the whole statement.

For example, the size comparison of v1 and v2 that we implemented earlier with nested if statements is actually three parallel situations: either greater than, less than, or equal to. Therefore, it can also be realized by parallel conditional structure:

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

It should be noted here that when parallel conditional statements are executed, the parallel conditional expressions will be calculated down one by one until a conditional expression is true, and then enter its branch to execute and end the whole statement. Therefore, we always put the condition judgment with high probability of satisfaction in the front position. We hope to encounter the branch satisfying the condition at the beginning of the if statement, so as to avoid useless calculation of the condition judgment that is not easy to meet.

In addition, it should be noted that the parallel conditional selection statement will only execute one branch. If multiple conditional expressions are true, only the branch with the first conditional expression true from top to bottom will be executed.

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

Tags: C C++

Posted on Fri, 15 Oct 2021 16:55:55 -0400 by mvd7793