Write a program that requires the input value of x and outputs the value of y. Use (1) non-nested if statement (2) nested if statement (3) if-else statement (4) switch statement respectively.

Write a program that requires the input value of x and outputs the value of y. Use (1) non-nested if statement (2) nested if statement (3) if-else statement (4) switch statement respectively.

The selection structure is a control structure commonly used in programming languages ​​and is used to execute different code blocks based on different conditions. In C language, we usually use if statement, switch statement, etc. to implement the selection structure. This article will introduce the application of four commonly used selection structures in C language, namely non-nested if statement, nested if statement, statement. if-else statement and switch
topic

1. Unnested if statements

The non-nestedif statement is one of the simplest selection structures and is suitable for mutually exclusive conditions. The following is an example of calculating the corresponding y value based on the input x value:

cCopy code
#include <stdio.h>

int main() {
    float x, y;

    // 输入x的值
    printf("请输入x的值:");
    scanf("%f", &x);

    // 不嵌套的if语句
    if (-5 < x && x < 0)
        y = x;
    else if (x == 0)
        y = x - 1;
    else if (0 < x && x < 10)
        y = x + 1;
    else
        printf("x的值不在定义范围内\n");

    // 输出y的值
    printf("(1) 不嵌套的if语句:y = %.2f\n", y);

    return 0;
}

In this example, based on the input x value, the program determines the interval where x is located through the non-nested if statement and calculates the corresponding y value.

2. Nested if statements

Nestedif statements can handle more complex conditional logic, but it can easily lead to unclear code structure. The following is an example of using nestedif statements:

cCopy code
// 程序代码同上,只是第二个例子的部分代码发生了变化

// 嵌套的if语句
if (-5 < x && x < 0)
    y = x;
else {
    if (x == 0)
        y = x - 1;
    else {
        if (0 < x && x < 10)
            y = x + 1;
        else
            printf("x的值不在定义范围内\n");
    }
}

// 输出y的值
printf("(2) 嵌套的if语句:y = %.2f\n", y);

In this example, the nestedif statement is used to calculate the corresponding y value based on the input x value.

3. if-else statement

if-elseThe statement is a more concise way of handling mutually exclusive conditions and is suitable for situations where each condition is mutually exclusive. The following is an example of using the if-else statement:

cCopy code
// 程序代码同上,只是第三个例子的部分代码发生了变化

// if-else语句
if (-5 < x && x < 0)
    y = x;
else if (x == 0)
    y = x - 1;
else if (0 < x && x < 10)
    y = x + 1;
else
    printf("x的值不在定义范围内\n");

// 输出y的值
printf("(3) if-else语句:y = %.2f\n", y);

In this example, the if-else statement is used to calculate the corresponding y value based on the input x value.

4. switch statement

switchThe statement is suitable for different processing of different values ​​​​of a variable. However, it should be noted that the conditional expression in the switch statement can only be of integer type or character type. The following is an example of using the switch statement:

cCopy code
// 程序代码同上,只是第四个例子的部分代码发生了变化

// switch语句
switch ((int)x) {
    case -5 ... -1:
        y = x;
        break;
    case 0:
        y = x - 1;
        break;
    case 1 ... 9:
        y = x + 1;
        break;
    default:
        printf("x的值不在定义范围内\n");
        break;
}

// 输出y的值
printf("(4) switch语句:y = %.2f\n", y);

In this example, the switch statement is used to calculate the corresponding y value based on the input x value.

Notes and extensions

  • Pay attention to using logical operators in conditional expressions to ensure the correctness of the conditions.
  • For the processing of floating point numbers, accuracy issues need to be taken into consideration and direct comparison of floating point numbers for equality needs to be avoided.
  • switchThe conditional expression in the statement can only be an integer type or character type, not a floating point number.
  • The clarity of code structure is an important consideration when choosing a structural design. Excessive nesting may make the code difficult to understand and maintain.

Selection structures are a key part of the control flow in a program, executing different code paths based on different conditions. In actual programming, choosing an appropriate selection structure according to specific situations is an important part of improving code readability and maintainability.

Guess you like

Origin blog.csdn.net/qq_54000767/article/details/134484709