C language system learning 2 branch and loop statement

1. Branching statement (choice structure)
There are many choices in life, and different choices will lead to different results. In programming, too, there are choices. In programming, branching corresponds to the selection structure.
SELECT STATEMENT CONCEPT MAP

#include<stdio.h>
int main()
{
	int choose = 0;
	printf("今天要学习吗?1.学 2.不学");
	scanf("%d", &choose);
	if (choose == 1)
	{
		printf("正在成为大佬的路上!");
	}
	else
	{
		printf("你是一个混子");
	}
	return 0;
}

In the selection structure, there are options, and the option is implemented in the code corresponding to the use of the if statement.
In this example, the brackets of the if statement correspond to the
grammatical structure of an expression if statement

语法结构
if(表达式)
	语句1;
else
	语句2;
//
if(表达式)
	语句1;
else if(表达式2)
	语句2;
else
	语句3;

The switch statement
The switch statement is also a branch statement, which is often used in multi-branch situations.
For example:
input 1, eat spicy hot pot,
input 2, eat pickled fish,
input 3, eat chicken stew,
input 4, eat dry pot
Structure

switch(整句表达式)
{
	语句项;
}

In the switch statement, we also need to use break and fault to use
break to end the selection after the selection is completed, and fault provides a new situation, that is, if the input result is consistent with all the established conditional expressions If it does not match, propose a solution in advance.
2. Loop statements
Loop is a situation that often occurs. For example, when we want to buy something in our life, we will repeat the act of saving money until we have enough money to buy something. Things you want to buy.
insert image description here
The while loop
For the if statement, if the conditional expression is satisfied, the statement will be executed once. If you want to satisfy the expression and execute it multiple times, you need to use the while loop

while(表达式)
	循环语句;

In the loop structure, very commonly used statements are break and continue, which are auxiliary tools for us to use the while loop. Using break will stop all loops after executing this statement command, and continue ends this loop and enters the next loop.
for loop
structure

for(表达式1;表达式2;表达式3)
	循环语句;

Expression 1 is the initialization part, expression 2 is the condition judgment part, which is used to judge when the loop is terminated, and expression 3 is the adjustment part, which will be adjusted during the loop process. The three expressions are used together to control the loop
do...while loop
structure

do
	循环语句;
while(表达式);

The feature will be performed at least once, and the conditional statement will be judged after the code is run once

Guess you like

Origin blog.csdn.net/qq_45742383/article/details/113408531