1. Nested if and if elseThe nested if and if else refers to including another if else statement within an if or else code block. This structure allows us to execute different code blocks based on multiple conditions.Framework 1:if (condition){ if(condition) { // Code Block 1 } else { // Code Block 2 }}else{ // Code Block 3}Framework 2:if (condition){ // Code Block 1}else{ if(condition) { // Code Block 2 } else { // Code Block 3 }}The difference between these two is that the nested if else is inside the if in one case and inside the else in the other.Assuming we need to determine whether an integer x is positive, negative, or zero, we can use the following logic:1. If x is greater than 0, output positive;2. If the above condition is not met, if x is less than 0, output negative; otherwise, output 0.Example code is shown in the image below:
2. if else nested if elseThe if else nested if else refers to nesting another if else statement within an if else statement. This allows for executing different code blocks based on more complex combinations of conditions.Example:
Assuming we want to determine whether a user qualifies for free shipping based on their total shopping cart price in an online store, we need to check the following conditions:1. If the total price of the shopping cart is greater than or equal to $100, the user qualifies for free shipping. If the total price is greater than or equal to $200, the user receives a 20% additional discount. Otherwise, the user receives a 10% additional discount.2. If the total price of the shopping cart is less than $100, the user does not qualify for free shipping. If the total price is greater than or equal to $50, the user receives a 5% additional discount. Otherwise, the user is advised to increase their shopping cart total to qualify for a discount.Example code:
If the total shopping price is $120, the program will output:Free shipping10% discount3. Logical operator &&In C++, the logical operator && represents logical AND. The entire expression is true only when both operands are true.For example:5>3 && 4<6Both sides of this expression are true, so the entire expression is true.&& operation rules: true and true is true, any false is false.
| Expression 1 | Expression 2 | Expression 1 && Expression 2 |
| true | true | true |
| true | false | false |
| false | true | false |
| false | false | false |
4. Example Problems1. Role-playing GameProblem Description: Xiao Ming is playing a role-playing game, and he needs to decide his next action based on his character’s health points (HP) and magic points (MP). If HP is greater than 50 and MP is greater than 30, he will attack; if MP is less than or equal to 30, he will use magic to recover; if HP is less than or equal to 50, he will use a potion.Input Description: Two integers representing HP and MP.Output Description: Based on the values of HP and MP, output Xiao Ming’s action.Thought Analysis:1) First check if HP is greater than 50;2) If HP is greater than 50, then check if MP is greater than 30;3) If MP is not greater than 30, Xiao Ming will use magic to recover;4) If HP is not greater than 50, Xiao Ming will use a potion.Specific example code is shown in the image below:
2. Travel PlanProblem Description: The “Non-Human Survival Strategy Guide” has established a strict standard for agents’ travel.If the agent’s task duration exceeds 7 days, they will choose to wear cloth shoes, output 1.If the agent’s task duration does not exceed 7 days (including 7 days), then further arrange the travel tool based on the task distance. If the distance exceeds 800 kilometers, they will choose airdrop, output 2; if not exceeding 800 kilometers (including 80), they will choose swimming ring, output 3.Input Description: One line, 2 integers, representing the task duration t (1<=t<=100) days, and the task distance d (1<=d<=10000) kilometers.Output Description: Based on the input data, output 1, 2, or 3.Complete code is shown in the image:
3. Bag SnatchingProblem Description: The “Non-Human Survival Strategy Guide” states that when an item is snatched, the following countermeasures can be taken.If the running speed exceeds 10 meters/second, further determine the handling method based on the strength value. If the strength value exceeds 100 kilograms, choose 1 (dragging him back to prevent him from running); if the strength value does not exceed 100 kilograms (including 100), choose 2 (running ahead to mock him).If the running speed does not exceed 10 meters/second (including 10), similarly determine the handling method based on the strength value. If the strength value exceeds 100 kilograms, choose 3 (snatch the bag from the passerby and exchange it); if the strength value does not exceed 100 kilograms (including 100), choose 4 (crying at him to persuade him).Input Description: One line, 2 integers, representing the running speed s (1<=s<=100) meters/second, and the strength value h (1<=h<=1000) kilograms.Output Description: Based on the input data, output 1, 2, 3, or 4.Example code is shown in the image:
4. Highest BidProblem Description: According to the money-making scheme in the “Non-Human Survival Strategy Guide”, the main speaker has sold a one-day usage right. At this time, a nouveau riche has thrown an olive branch to the speaker and offered three positions.
The first position is a haunted house tester, bidding a gold coin weight of a; the second position is a money transfer service, bidding b gold coin weight; the third position is a junk collector, bidding c gold coin weight.
To make more money, the speaker decides to weigh the highest bid among them. Which bid can win the speaker’s usage right?
Input Description:One line, 3 integers, representing the bids a, b, c (the problem guarantees that a, b, c are not equal).Output Description:One line, 1 integer, which is the largest of the three numbers.Example code is shown in the image:
5. Highest Bid 2Problem Description: To achieve the simplicity required by the nouveau riche, use logical operators to solve the following problem.
According to the money-making scheme in the “Non-Human Survival Strategy Guide”, the main speaker has sold a one-day usage right. At this time, a nouveau riche has thrown an olive branch to the speaker and offered three positions.
The first position is a haunted house tester, bidding a gold coin weight of a; the second position is a money transfer service, bidding b gold coin weight; the third position is a junk collector, bidding c gold coin weight.
To make more money, the speaker decides to weigh the highest bid among them. Which bid can win the speaker’s usage right?
Input Description:One line, 3 integers, representing the bids a, b, c (the problem guarantees that a, b, c are not equal).Output Description:One line, 1 integer, which is the largest of the three numbers.Example code is shown in the image:
6. Triangle JudgmentProblem Description: According to the “Non-Human Survival Strategy Guide”, the stability of a triangle is the strongest, and the speaker has decided to dig a triangular tunnel for escape.
It is known that a triangle in a plane has the following property: the sum of any two sides must be greater than the third side.
If the lengths of the three line segments satisfy this property, then these three line segments can form a triangle. Otherwise, they cannot.
Now given the lengths of three line segments a, b, c, determine whether they can form a triangle. If they can, output yes; if not, output no.
Input Description:One line, 3 integers, representing the lengths of the three line segments a, b, c.Output Description:One line, if they can form a triangle, output yes; if not, output no.Example code is shown in the image:
That’s all for today’s content!
Don’t forget to “follow me, I will keep updating!