Control statements in C language (3)

switch


In C language, `switch` is a control statement used for multi-branch conditional selection. It allows different code blocks to be selectively executed based on the value of an expression. The general syntax of the `switch` statement is as follows:

switch (expression) {

    case value1:

        //The code here is executed when the value of the expression is equal to value1

        break;

    case value2:

        //The code here is executed when the value of the expression is equal to value2

        break;

    // More case statements

    default:

        //Execute the code here if no case matches

}

- `expression` is an expression whose value will be compared with the value in each `case` tag.

- The `case` tag defines each possible expression value. When the value of `expression` is equal to the value in a certain `case` tag, the code block associated with the `case` tag will be executed.

- The `break` statement is used to break out of the `switch` block and end the execution of the `switch` statement. If there is no `break` statement, the program will continue to execute the code in subsequent `case` labels until it encounters a `break` or `switch` statement.

- The `default` tag is optional and is used to handle situations when the value of `expression` does not match any `case` tag. If there is no matching `case` tag, the code block associated with the `default` tag will be executed.

The following is an example demonstrating the use of the `switch` statement:

#include <stdio.h>

int main() {

    int choice;

    printf("Please select a number (1-3):");

    scanf("%d", &choice);

    switch (choice) {

        case 1:

            printf("You selected the number 1.\n");

            break;

        case 2:

            printf("You selected the number 2.\n");

            break;

        case 3:

            printf("You selected the number 3.\n");

            break;

        default:

            printf("Invalid selection.\n");

    }

    return 0;

}


 

In this example, the `switch` statement chooses to execute different blocks of code based on the value entered by the user. If the user enters 1, 2 or 3, the corresponding code block will be executed respectively. If any other value is entered, the code block in the `default` tag will be executed.

It should be noted that the `switch` statement can only be used to compare values ​​of integer and character types. In some compilers, enumeration types can also be used. If you need to compare values ​​of other data types, you usually need to use a series of `if...else if...else` statements.

return


In C language, `return` is a keyword used to return a value from a function. It allows the function to return a specific value to the caller after completion of execution, or to end the execution of the function early without returning a value. The usage of the `return` statement is as follows:


return expression;
 

- `expression` is an expression that represents the value to be returned to the caller. The data type of `expression` must be compatible with the return type of the function. If the function has no return value (return type is `void`), `expression` can be omitted.

Here are some examples demonstrating the use of `return`:

1. Return an integer value:


int add(int a, int b) {     int result = a + b;     return result; // Return the result to the caller }



 

2. Return a floating point value:


float divide(float x, float y) {     if (y == 0.0) {         printf("The divisor cannot be zero.\n");         return 0.0; // Return 0.0 as a special case result     }     return x / y; / / Return the division result to the caller }






 

3. Return no value (`void` function):


void greet() {     printf("Hello, world!\n");     return; // can be omitted because the void function does not return a value }



 

In a function, `return` is not only used to return a value, but also to end the execution of the function early. For example:


int findFirstPositive(int array[], int length) {     for (int i = 0; i < length; i++) {         if (array[i] > 0) {             return i; // End the function early and return the index position         }     }     return -1; // If no positive number is found, return -1 }







 

In the above example, if the first positive number in the array is found, the function immediately returns the index position of that positive number. If no positive number is found, the function returns -1 after the loop ends.

The `return` statement is part of the control flow, which determines the execution flow and return value of the function. The `return` statement can be used anywhere in the function, but once `return` is executed, the function will end immediately and subsequent code will not continue to be executed. Therefore, be careful to use `return` when appropriate to return the correct value or end the execution of the function early.

Guess you like

Origin blog.csdn.net/qq_50942093/article/details/132913444