[C++ Entry Guide] (07) Judgment

insert image description here

Predicate constructs require the programmer to specify one or more conditions to be evaluated or tested, along with statements to execute if the conditions are true (required) and statements to execute if the conditions are false (optional).

1. The if statement

Function : Execute the statement that satisfies the condition.

Three forms of if statement:

  • Single-line format if statement
  • Multi-line format if statement
  • multi-conditional if statement

1. Single-line format if statement

Grammar :if(条件){条件满足执行的语句}

Flowchart :
insert image description here
Example :

#include <iostream>

using namespace std;


int main()
{
    
    
	int score = 0;
	cout << "请输入您的分数: " << endl;
	cin >> score;

	if (score > 500)
	{
    
    
		cout << "恭喜您,可以顺利进入大学" << endl;
	}

	return 0;
}

2. Multi-line format if statement

Grammar :if(条件){条件满足执行的语句}else{条件不满足执行的语句}

Flowchart :
insert image description here

Example : daffodil numbers 371, 153, 370, 407

#include <iostream>

using namespace std;


int main()
{
    
    
	int num = 0;
	int unit = 0;
	int ten = 0;
	int hundred = 0;
	cout << "请输入一个三位数: " << endl;
	cin >> num;

	unit = num % 10;
	ten = num % 100 / 10;
	hundred = num / 100;

	if (unit * unit * unit + ten * ten * ten + hundred * hundred * hundred == num)
	{
    
    
		cout << "您输入的三位数 " << num << " 为水仙花数噢" << endl;
	}
	else 
	{
    
    
		cout << "您输入的三位数 " << num << " 不是水仙花数" << endl;
	}

	return 0;
}

Second, the ternary operator

Function : Realize simple judgment through the ternary operator.

Grammar :表达式1 ? 表达式2 : 表达式3

Description :
If the value of expression 1 is true, execute expression 2 and return the result of expression 2;
if the value of expression 1 is false, execute expression 3 and return the result of expression 3.
The ternary operator in C++ returns a variable, which can continue to be assigned.

3. The switch statement

Function: Execute multiple conditional branch statements.

grammar:

switch(expression){
    
    
    case constant-expression  :
       statement(s);
       break; // 可选的
    case constant-expression  :
       statement(s);
       break; // 可选的
  
    // 您可以有任意数量的 case 语句
    default : // 可选的
       statement(s);
}

A switch statement must obey the following rules:

  • The expression in the switch statement must be an integral or enumeration type, or a class type where the class has a single conversion function to convert it to the integral or enumeration type.
  • There can be any number of case statements in a switch. Each case is followed by a value to compare and a colon.
  • The constant-expression of the case must have the same data type as the variable in the switch, and must be a constant or literal.
  • When the variable being tested is equal to the constant in the case, the statements following the case will be executed until a break statement is encountered.
  • When a break statement is encountered, the switch terminates, and the control flow will jump to the next line after the switch statement.
  • Not every case needs to contain a break. If the case statement does not contain a break, the control flow will continue with subsequent cases until a break is encountered.
  • A switch statement can have an optional default case that appears at the end of the switch. The default case can be used to perform a task when none of the above cases are true. The break statement in the default case is not required.

4. Algorithm questions

4.1 Piecewise functions

There is a function as follows, write a program, input x, output y value.
insert image description here

Round to two decimal places.

code :

#include <iostream>
#include <iomanip>

using namespace std;


int main()
{
    
    
	double x = 0.00;
	double y = 0.00;

	cin >> x;

	if (x < 1)
	{
    
    
		y = x;
	}
	else if (x >= 1 && x < 10)
	{
    
    
		y = 2 * x - 1;
	}
	else
	{
    
    
		y = 3 * x - 11;
	}

	cout << fixed << setprecision(2) << y << endl;

	return 0;
}

Guess you like

Origin blog.csdn.net/duoduo_11011/article/details/130637655