[C Language] Branch statements and loop statements

[C Language] Branch statements and loop statements

C language statement classification

Statements in C language can be divided into the following five categories:

  1. expression statement
  2. function call statement
  3. control statement
  4. compound statement
  5. empty statement

Among them, control statements are used to control the execution flow of the program to realize various structural methods of the program . There are nine types of control statements in C language. Can be divided into the following three categories:

  1. Conditional judgment statements are also called branch statements: if statements, switch statements;
  2. Loop execution statements: do while statement, while statement, for statement;
  3. Turn statements: break statement, goto statement, continue statement, return statement.

C language is a structured programming language , including the following structures:

  • Sequential structure – execute the code in a predetermined order
  • Select structure – select a portion of code to execute
  • Loop structure – execute the same piece of code repeatedly

image-20230820102122014

C language uses branch statements to form selection structures and loop statements to form loop structures. Both branch statements and loop statements are control statements .

branch statement

if syntax structure

The if statement has the following three grammatical structures:

  1. single branch
语法结构:
if(表达式)
    语句;
  • If the expression is true, the statement is executed.
#include <stdio.h>

int main()
{
    
    
	int age = 16;
	if (age < 18) //表达式为真,输出未成年
		printf("未成年\n");
	return 0;
}
  1. double branch
if(表达式)
    语句1;
else
    语句2;
  • If the expression is true, statement 1 is executed.
  • If the expression is false, statement 2 is executed.
#include <stdio.h>

int main()
{
    
    
	int age = 20;
	if (age < 18) //表达式为假,输出成年
		printf("未成年\n"); 
	else
		printf("成年\n");
	return 0;
}
  1. multiple branches
if(表达式1)
    语句1;
else if(表达式2)
    语句2;
else
    语句3;
  • If expression 1 is true, statement 1 is executed.
  • If expression 2 is true, statement 2 is executed.
  • All expressions before else are false and statement 3 is executed.
  • Multiple can be used else ifto control multiple branches.
#include <stdio.h>

int main()
{
    
    
	int age = 59;
	if (age < 18)
		printf("未成年\n");
	else if (age >= 18 && age < 60) //该表达式为真,输出成年
		printf("成年\n");
	else
		printf("老年\n");
	return 0;
}

Replenish:

  • In C language, 0 means false and 1 means true.
  • No matter how many branches there are in the if syntax structure, only one branch can be selected for execution.
  • if, else if, else can only control one subsequent statement. If you want to control multiple statements, you need to use {} to turn the statement into a code block.

There can be multiple statements in a code block. The structure of using a code block is as follows:

#include <stdio.h>
int main()
{
    
    
    if(表达式)
   {
    
    
        语句列表1}
    else
   {
    
    
        语句列表2}
    return 0;
}

Else matching rules

else matches the nearest if.

#include <stdio.h>

int main()
{
    
    
    int a = 0;
    int b = 2;
    if (a == 1)
        if (b == 2)
            printf("hehe\n");
    else
        printf("haha\n");
    return 0; //没有输出
}

Since else matches the nearest if, else matches the if (b == 2) statement, so when the condition in if (a == 1) is not true, the print statement will not be executed. In order to improve the readability of the code, it is recommended to modify it as follows:

#include <stdio.h>

int main()
{
    
    
    int a = 0;
    int b = 2;
    if (a == 1)
    {
    
    
        if (b == 2)
            printf("hehe\n");
        else
            printf("haha\n");
    }
    return 0;
}

The functions of the above two pieces of code are the same, but the readability is different due to different writing methods.

switch statement

The switch statement is also a branch statement and is often used in multi-branch situations.

The main syntax structure of the switch statement is as follows:

switch(整型表达式)
{
    
    
    case 整形常量表达式:
    语句;
    //...
}
  • Integer expressions must be used as judgment objects in switch .
  • Integer constant expressions must be used as judgment conditions in case .
  • The integer expression in the switch and the integer constant expression in the case are compared and judged, and the selection is made to enter the corresponding case.
#include <stdio.h>

int main()
{
    
    
	int day = 0;
	scanf("%d", &day); //输入为4
	switch (day)
	{
    
    
	case 1:
		printf("星期一\n");
	case 2:
		printf("星期二\n");
	case 3:
		printf("星期三\n");
	case 4:
		printf("星期四\n");
	case 5:
		printf("星期五\n");
	case 6:
		printf("星期六\n");
	case 7:
		printf("星期天\n");
	}
	return 0;//输出为星期四 星期五 星期六 星期天
}

Although day selects case 4 in the above code, since there is no control end in this case, the switch statement will be executed sequentially, and case 5, case 6, and case 7 will also be executed.

break in switch statement

The break in the switch statement can divide the statement list into different branch parts.

#include <stdio.h>
int main()
{
    
    
	int day = 0;
	scanf("%d", &day);//输入为4
	switch (day)
	{
    
    
	case 1:
		printf("星期一\n");
		break;
	case 2:
		printf("星期二\n");
		break;
	case 3:
		printf("星期三\n");
		break;
	case 4:
		printf("星期四\n");
		break;
	case 5:
		printf("星期五\n");
		break;
	case 6:
		printf("星期六\n");
		break;
	case 7:
		printf("星期天\n");
		break;
	}
	return 0;//输出为星期四
}

After executing the statement in case 4, break controls the end of the situation, jumps out and ends the switch statement.

default in switch statement

default means all cases except the above case.

#include <stdio.h>
int main()
{
    
    
	int day = 0;
	scanf("%d", &day); //输入8
	switch (day)
	{
    
    
	case 1:
		printf("星期一\n");
		break;
	case 2:
		printf("星期二\n");
		break;
	case 3:
		printf("星期三\n");
		break;
	case 4:
		printf("星期四\n");
		break;
	case 5:
		printf("星期五\n");
		break;
	case 6:
		printf("星期六\n");
		break;
	case 7:
		printf("星期天\n");
		break;
	default:
		printf("输入错误\n");
		break;
	}
	return 0; //输出为输入错误
}

loop statement

while loop

The syntax structure of while loop:

while(表达式)
	循环语句;
  • The loop evaluates the expression. If the expression is true, the loop statement is executed.
  • The expression is false, ending the loop.
  • When the loop statement is multiple statements, {} needs to be used to turn it into a code block.
#include <stdio.h>

int main()
{
    
    
	int i = 0;
	while (i < 5)
	{
    
    
		printf("%d ", i);
		i++;
	}
	return 0; //输出 0 1 2 3 4
}

break and continue in while loop

break in while is used to terminate the loop permanently. As long as break is encountered in the loop, all subsequent loops will be stopped and the loop will be terminated directly.

#include <stdio.h>

int main()
{
    
    
	int i = 0;
	while (i < 5)
	{
    
    
		if (i == 3)
			break;
		printf("%d ", i);
		i++;
	}
	return 0; //输出为 0 1 2
}

When i=3, the while expression is judged to be true and the loop statement is executed, the if expression is judged to be true, and the break statement is executed to terminate the while loop.

continue is used to terminate this loop, that is, the code after continue in this loop will not be executed again, but will jump directly to the judgment part of the while statement. Make the entry judgment for the next cycle.

#include <stdio.h>

int main()
{
    
    
	int i = 0;
	while (i < 5)
	{
    
    
		i++;
		if (i == 3)
			continue;
		printf("%d ", i);
	}
	return 0; //输出为 1 2 4 5
}

When i=3, the while expression is judged to be true and the loop statement is executed. The if expression is judged to be true. The continue statement is executed to terminate this loop and enter the next loop.

Rough flow chart of while loop:

image-20230820133359713

for loop

The syntax structure of for loop:

for(表达式1; 表达式2; 表达式3)
 循环语句;
  • Expression 1 is the initialization part, used to initialize loop variables.
  • Expression 2 is the conditional judgment part, which is used to judge the termination of the loop.
  • Expression 3 is the adjustment part, which is used to adjust the loop conditions.
  • When the loop statement is multiple statements, {} needs to be used to turn it into a code block.
#include <stdio.h>

int main()
{
    
    
	int i = 0;
	for (i = 1; i < 10; i++)
	{
    
    
		printf("%d ", i);
	}
	return 0; //输出为 1 2 3 4 5 6 7 8 9
}

break and continue in for loop

break and continue can also appear in a for loop, and their meaning is the same as in a while loop.

#include <stdio.h>

int main()
{
    
    
	int i = 0;
	for (i = 1; i < 5; i++)
	{
    
    
		if (i == 3)
			break;
		printf("%d ", i);
	}
	return 0; //输出为 1 2
}

When i=3, the for expression is judged to be true and the loop statement is executed, the if expression is judged to be true, and the break statement is executed to terminate the for loop.

#include <stdio.h>

int main()
{
    
    
	int i = 0;
	for (i = 1; i < 5; i++)
	{
    
    
		if (i == 3)
			continue;
		printf("%d ", i);
	}
	return 0; //输出为 1 2 4
}

When i=3, the for expression is judged to be true and the loop statement is executed. The if expression is judged to be true. The continue statement is executed to terminate the current loop and enter the next loop.

Rough flow chart of the for loop:

image-20230820140402568

Variants of for loop

#include <stdio.h>

int main()
{
    
    
	for (;;)
	{
    
    
		printf("hehe\n");
	}
	return 0; //死循环输出 hehe
}

The initialization part, judgment part, and adjustment part in the for loop can be omitted. Omitting them all means that the loop condition will always be true.

#include <stdio.h>

int main()
{
    
    
    int i = 0;
    int j = 0;
    for (i = 0; i < 10; i++)
    {
    
    
        for (j = 0; j < 10; j++)
        {
    
    
            printf("hehe\n");
        }
    }
    return 0; //输出为100个hehe
}

The for loop is nested, the outer loop loops 10 times, the outer loop executes the inner loop once each time, and the inner loop executes 10 times, so the total is 100 times.

#include <stdio.h>

int main()
{
    
    
    int i = 0;
    int j = 0;
    for (; i < 10; i++)
    {
    
    
        for (; j < 10; j++)
        {
    
    
            printf("hehe\n");
        }
    }
    return 0; //输出为10个hehe
}

The for loop is nested, the outer loop loops 10 times, and the outer loop executes the inner loop once each time. After the first inner loop is executed 10 times, j is 10, and the next inner loop is not initialized. j is still 10, and the inner loop can no longer be executed, so the total is 10 times.

#include <stdio.h>

int main()
{
    
    
    int x, y;
    for (x = 0, y = 0; x < 2 && y < 5; ++x, y++)
    {
    
    
        printf("hehe\n");
    }
    return 0; //输出为2个hehe
}

The judgment condition of the loop is x < 2 && y < 5. After looping twice, x < 2 is no longer satisfied, so it can only be executed twice.

#include <stdio.h>

int main()
{
    
    
    int i = 0;
    int k = 0;
    for (i = 0, k = 0; k = 0; i++, k++)
        k++;
    return 0;
}

The judgment condition is k=0, which is an assignment statement. The result of the assignment statement is the assigned value, so the result of the statement is 0, and the loop is not executed once.

do while loop

Syntax structure of do while loop:

do
 循环语句;
while(表达式);
  • The loop statement is executed once before evaluating the expression, so the loop is executed at least once.
  • When the loop statement is multiple statements, {} needs to be used to turn it into a code block.
#include <stdio.h>

int main()
{
    
    
	int i = 1;
	do
	{
    
    
		printf("%d ", i);
		i++;
	} while (i <= 5);
	return 0; //输出 1 2 3 4 5
}

break and continue in do while loop

#include <stdio.h>
int main()
{
    
    
	int i = 1;
	do
	{
    
    
		if (3 == i)
			break;
		printf("%d ", i);
		i = i + 1;
	} while (i <= 5);
	return 0; //输出 1 2 
}

When i=3, the if expression is judged to be true, and the break statement is executed to terminate the loop.

#include <stdio.h>
int main()
{
    
    
    int i = 0;
    do
    {
    
    
        i = i + 1;
        if (3 == i)
            continue;
        printf("%d ", i);
    } while (i < 5);
    return 0;//输出 1 2 4 5
}

When i=3, the if expression is judged to be true, ending this loop and proceeding to the next loop.

Rough flow chart of do while loop:

image-20230820171738939

Summarize:

  • No matter what kind of loop statement, the function of break is to end the loop.
  • No matter what kind of loop statement, the function of continue is not to execute the code after continue and jump to the loop judgment. (The for loop will adjust the loop conditions before jumping to loop judgment)

goto statement

The goto statement in C language can jump the program to the marked position.

#include <stdio.h>

int main()
{
    
    
	printf("hello world\n");
	goto flag;
	printf("hello goto\n");
flag:
	printf("hello blog\n");
	return 0; //输出为 hello world hello blog
}

It is flag:used as a flag bit for the goto statement to jump. Because the goto statement jumps to the flag position, the intermediate code will not be executed.

  • The flag name can be arbitrary, you only need to use 标志名:the flag position to jump using the goto statement.
  • The goto statement is not commonly used because it reduces the readability and maintainability of the code.
  • The goto statement is suitable for jumping from the inside of a multi-level loop to the outside.

Guess you like

Origin blog.csdn.net/csdn_myhome/article/details/132406568