Branch and loop statements

Posted by Redapple on Mon, 24 Jan 2022 18:45:01 +0100

Branch statement

  if

  switch

Branch statement (select structure)

if statement

Syntax structure:

1 single branch structure

if (expression)

Statement;

If the expression is true, the statement will be executed; otherwise, the statement will not be executed.

For example:

#include<stdio.h>
//Enter two numbers to find the larger one
int main()
{
    int a = 0, b = 0;
    printf("Please enter two numbers:");
    scanf("%d %d",&a, &b);
    if(a < b)
    {
       a = b;
    }
    printf("The larger of the two numbers is %d", a);
    
    return 0;
}

Operation results:

2 double branch structure

if (expression)

Statement 1;

else 

Statement 2;

If the expression is true, execute statement 1; otherwise, execute statement 2

For example:

#include<stdio.h>
int main()
{
    int coding = 0;
    printf("Will you study programming well? (option 1)  or  Select (0):");
    scanf("%d", &coding);
    if(coding == 1)
    {
       printf("Stick to it and you'll have a good future offer\n");
    }
    else
    {
       printf("Give up, you will pay a high price!\n");
    }
    return 0;
}

Operation results:

3 multi branch structure

if (expression 1)

Statement 1;

else if (expression 2)

Statement 2;

else if (expression 3)

Statement 3;

else 

Statement 4;

If expression 1 is true, execute statement 1; if expression 2 is true, execute statement 2; and so on, execute only one statement.

For example:

#include<stdio.h>
int main()
{
    int age = 0;
    printf("Please enter your age to roughly judge your age group:");
    scanf("%d",&age);
    if(age <= 18)
    {
       printf("teenagers");
    }
    else if(age <= 65)
    {
       printf("adult");
    }
    else
    {
       printf("old age");
    }
    return 0;
}

Operation results:

matters needing attention:

1. The representation of true and false in C language: 0 means false, and non-0 means true.

2 if the condition holds, you need to use code blocks to execute multiple statements, otherwise you can only execute the first statement.

Usage example:

#include<stdio.h>
int main()
{
    if(expression)
    {
        Statement list;
    }
    else
    {
        Statement list;
    }
    return 0;
}

3. Suspend else. Else will only match its nearest unmatched if

For example:

#include<stdio.h>
int main()
{
    int a = 0;
    int b = 2;
    if(a == 1)
        if(b == 2)
            printf("hello\n");
    else
        printf("hi\n");
    return 0;
}

Operation results:

Nothing!

Reason: because of the format alignment, it is easy to think that else matches the first if, but in C language, else will match its nearest if

if()
    if()
        printf();
else
    printf();
//The above format is equivalent to
if()
{
    if()
        printf();
    else
        printf();
}

The first if does not judge success at all, and it is impossible to execute the following statement.

Correction:

//Using {} properly can make the logic of the code clearer
#include<stdio.h>
int main()
{
    int a = 0;
    int b = 0;
    if(a == 1)
    {
        if(b == 2)
        {
            printf("hello!\n");
        }
    }
    else 
    {
        printf("hi!\n");
    }
    return 0;
}

4. When the comparison between constant and variable is equal, the constant is placed to the left and the variable is placed to the right (reduce errors)

During comparison, constants are placed on the left and variables on the right. If "= =" is mistakenly written as "=", the compiler will automatically report an error.

2 switch statement

Switch statements are often used in the case of multiple branches. When we write if else if ... Since the form of else if is too complex, we will consider using the switch statement.

Structural form:

Switch (integer expression)

{

Statement item;

Statement items are case statements:

case shaping expression:

Statement;

The switch statement can really branch by using break in combination.

#include<stdio.h>
int main()
{
    int day = 0;
    scanf("%d",&day);
    switch(day)
    {
        case 1:
            printf("Monday\n");
            break;
        case 2:
            printf("Tuesday\n");
            break;
        case 3:
            printf("Wednesday\n");
            break;
        case 4:
            printf("Thursday\n");
            break;
        case 5:
            printf("Friday\n");
            break;
        case 6:
            printf("Saturday\n");
            break;
        case 7:
            printf("Sunday\n");
            break;
    }
    return 0;
}

Operation results:

Now change requirements:

1. Input 1-5 and output "working day"

2. Input 6-7 and output "rest day"

#include<stdio.h>
int main()
{
    int day = 0;
    scanf("%d",&day);
    switch(day)
    {
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
            printf("weekdays\n");
            break;
        case 6:
        case 7:
            printf("Rest Day\n");
            break;
    }

    return 0;
}

Operation results:

The actual effect of the break statement is to divide the statement list into different parts.

default clause (generally used for illegal input)

Default is written where any case tag can appear. When the value of the switch expression does not match the value of all case tags, the statement after the default clause will be executed.

practice:

#include<stdio.h>
//Find the values of operation results m and n
int main()
{
    int n = 1;
    int m = 2;
    switch(n)
    {
    case 1:
            m++;
    case 2:
            n++;
    case 3:
            switch(n)
            {
             case 1:
                    n++;
             case 2:
                    m++;
                    n++;
                    break;
            }
    case 4:
            m++;
             break;
    default:
             break;   
    }
    printf("m = %d, n = %d\n",m,n);
    return 0;
}

Operation results:

Process: