C Primer Plus Chapter 7

Posted by soto134 on Thu, 14 Oct 2021 09:35:32 +0200

        1, if statement

        General form

if(expression)
{
    statement; 
}

        If expression is true, execute the statement, otherwise skip the statement

          2, If else

        General form

if(expression)
{
    statement1;
}
else
{
    statement2;
}

        For double branch structure, if expression is true, execute statement1 ·, otherwise execute statement2.

        3, Else if

        General form

if(expression1)
{
    statement1;
}
else if(expression2)
{
    statement2;
}
else
{
    statement3;
}

        Multi branch structure. There can be multiple else if branches between the first if and the last else.

        4, else matches if

        Without curly bracket constraints, else matches the if closest to itself

if(x > 10)                   //If x > 10, the first output is executed    
{
    printf("I'm the first if\n");

    if(x < 20)                //If 10 < x < 20, the second output is performed
    {
        printf("I'm the second if\n");
    }
}
else                         //If x < = 10, done will be output! This else is paired with the first if
{
    printf("done!\n");
}

        In the above code, if the curly braces are missing, the process will change

if(x > 10)                   //If x > 10, the first output is executed 
   
    printf("I'm the first if\n");

    if(x < 20)                //The second output is executed as long as x < 20
    {
        printf("I'm the second if\n");
    }
else                         //If x > = 20, done will be output! This else is paired with the second if
{
    printf("done!\n");
}

        5, Character input / output functions: getchar() and putchar()

        getchar() takes no parameters, receives and returns the next character from the input, and the return value type is int

int ch;

ch = getchar();   //Assign the character received by getchar() to the variable ch.

//The variable ch is set as a bit integer to ensure that the assignment of getchar() can be accurately received. Because getchar() can receive any character in the input.
//If ch is set to char type, it belongs to assigning the return value of int type to char type variable. It is a demotion operation, which may cause the input characters to be truncated and the correct characters cannot be obtained.

        The putchar() function prints its parameters.

putchar(ch);        //ch is the variable declared in the above code snippet

printf("%d", ch);   //The two statements are equivalent

          6, Logical operator

Logical operatormeaning
&&And
||or
!wrong

        Priority:

        (1)! The operator has a very high priority, the same as the increment operator, only lower than the parentheses

        (2) The & & operator takes precedence over the | operator, but both take precedence over the relational operator and over the assignment operator

        (3) When using multiple operators in an expression, even if the priority is clear, it is best to use parentheses for easy reading

         Evaluation order

        Except that two operators share an operand, C usually does not guarantee which item in a complex expression is evaluated first.

         However, for logical operators is an exception!

        C guarantees that the evaluation order of logical expressions is from left to right&& And are sequence points, so all side effects will take effect before the program executes from one operand to the next.

        C guarantees that once an element is found to invalidate the entire expression, evaluation will stop immediately

while((ch = getchar()) != ' ' && ch != '\n')  //The order in which logical expressions are evaluated ensures that ch is assigned first

        7, Conditional operator

        C provides a conditional expression as a convenient way of if else. The expression uses conditional operators. The general form is:

//If expression is true, the value of the entire conditional expression is the same as that of expression1, otherwise it is the same as that of expression2
expression ? sexpression1 : expression2;

        8, Loop assist: continue and break

        continue

        continue and bireak statements can ignore part of the loop content or even end the loop according to the test results in the loop body

        The continue statement can be used for all three loops. When this statement is executed, the rest of this iteration will be skipped and the next iteration will be executed.

        If the continue statement is inside a nested loop, only the inner loop containing the statement will be affected

        continue can also be used as a placeholder

while(getchar() != '\n')
{
    continue;                //If the input is not carriage return, the input is discarded
}

//Because the loop body of this loop is an empty statement, nothing needs to be executed
//Therefore, continue here is just a reminder. Compared with empty statements, it can make people pay more attention to the loop body of the loop and improve the readability of the program

          For while and do while loops, the next action after executing the continue statement is to evaluate the test expression of the loop

         For the for loop, the next action to continue is to evaluate the update expression and then the test expression

         break

         When the program executes the bireak statement, it terminates the loop containing it and continues to execute the statements after the loop.

        For the for loop, break and continue are different. After the break statement is executed, the statement after the loop will be executed directly, and the update expression will not be evaluated

        Like continue, a break in a nested loop affects only the current loop that contains it

        9, switch and break

        Multiple choice: switch case, general form

switch(Integer expression)
{
    case Constant 1:                  //You can have multiple case labels to choose from
        statement1;               //You can use the break statement to jump out of switch
    case Constant 2:
        statement2;               
    default:                      //The default statement is optional, but it is usually used
        sataement;
}

          The test expression of switch in parentheses should be an integer value (including char type).

        The case label must be a constant of integer type (including char type) or an integer constant expression.

        switch statement with multiple labels: the program jumps to the case label according to the value of expression. Then, execute all the remaining statements, especially the break statement for redirection

Topics: C