Branch statement and loop statement in C language (1)

Branch statement and loop statement in C language (1)

1. What is a sentence

C statements can be divided into the following five categories:

  1. expression statement
  2. function call statement
  3. control statement
  4. compound statement
  5. Empty Statements
    The following are control statements.
    Control statements are used to control the execution flow of the program to realize various structural modes of the program. They are composed of specific statement definers. C language has nine control statements.
    Can be divided into the following three categories:
  6. Conditional judgment statements are also called branch statements: if statement, switch statement;
  7. Loop execution statement: do while statement, while statement, for statement;
  8. Turn statement: break statement, goto statement, continue statement, return statement.

2. Branch statement (choice structure)

If you study hard, get a good offer during school recruitment, and reach the pinnacle of life.
If you don't study, graduation is equivalent to unemployment, go home and sell sweet potatoes.
This is choice!

1. if statement
What is the grammatical structure of the if statement?

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

Code example:

#include <stdio.h>
int main()
{
    
    
	int age = 0;
	scanf("%d", &age);
	if (age < 18)
	{
    
    
		printf("少年\n");
	}
	else if (age >= 18 && age < 30)
	{
    
    
		printf("青年\n");
	}
	else if (age >= 30 && age < 50)
	{
    
    
		printf("中年\n");
	}
	else if (age >= 50 && age < 80)
	{
    
    
		printf("老年\n");
	}
	else
	{
    
    
		printf("老寿星\n");
	}

}

To explain:
If the expression evaluates to true, the statement executes.
How to express true and false in C language? (0 means false, non-zero means true)

If the condition is true and multiple statements are to be executed, how should a code block be used.

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

A pair of { } here is a code block.
1.1 dangling else
when you wrote this code:

#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;
}

Appropriate use of {} can make the logic of the code clearer.
Code style is very important
Correction:

#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;
}

Else matching : else matches the nearest if.
1.2. Comparison of writing forms of if
Code 1:

if (condition) {
    
    
	return x;
}
return y;

Code 2:

if (condition)
{
    
    
	return x;
}
else
{
    
    
	return y;
}

Code 3:

int num = 1;
if (num == 5)
{
    
    
	printf("hehe\n");
}

Code 4:

int num = 1;
if (5 == num)
{
    
    
	printf("hehe\n");
}

Code 2 and Code 4 are better, the logic is clearer, and it is less prone to errors.
2. The switch statement
The switch statement is also a branch statement.
Often used in multi-branch situations.
for example:

输入1,输出星期一
输入2,输出星期二
输入3,输出星期三
输入4,输出星期四
输入5,输出星期五
输入6,输出星期六
输入7,输出星期日

The form of if...else if ...else if is too complicated, so we have to have a different grammatical form.
This is the switch statement.

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

And what is the statement item?
It is some case statements
as follows:

case 整形常量表达式:
  语句;

2.1 The break in the switch statement In
the switch statement, we can't directly realize the branch, and the real branch can be realized only by using the break.
for example:

#include <stdio.h>
int main()
{
    
    
	int day = 0;
	switch (day)
	{
    
    
		case 1printf("星期一\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;
}

Sometimes our needs change:

  1. Input 1-5, the output is "weekday"
  2. Input 6-7, output "weekend"
    so our code should be implemented like this:
#include <stdio.h>
int main()
{
    
    
	int day = 0;
	switch (day)
	{
    
    
		case 1case 2:
		case 3:
		case 4:
		case 5:
			printf("weekday\n");
			break;
		case 6:
		case 7:
			printf("weekend\n");
			break;
	}
	return 0;
}

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

Good programming habit : add a break statement after the last case statement. (It can avoid forgetting to add a break statement after the last case statement before)
2.2 default clause
What if the expressed value does not match the values ​​of all case labels?
In fact, it's nothing, the structure is that all the statements are skipped.
The program does not terminate, and no error is reported, because this situation is not considered an error in C.
But what if you don't want to ignore the value of an expression that doesn't match all tags?
You can add a default clause in the statement list, and write the following label
default :
in any position where a case label can appear.
When the value of the switch expression does not match the values ​​of all case labels, the statements following the default clause will be executed.
Therefore, only one default clause can appear in each switch statement.
But it can appear anywhere in the statement list, and the statement flow will execute the default clause as if it were a case label.
example code

#include <stdio.h>
int main()
{
    
    
	int n = 1;
	int m = 2;
	switch (n)
	{
    
    
	case 1:
		m++;
	case 2:
		n++;
	case 3:
		switch (n)
		{
    
    //switch允许嵌套使用
		case 1:
			n++;
		case 2:
			m++;
			n++;
			break;
		}
	case 4:
		m++;
		break;
	default:
		break;
	}
	printf("m = %d,n = %d\n", m, n);
	return 0;
}

Good programming habit : It is a good habit to put a default clause in each switch statement, and you can even add a break after it.
To be continued...

Guess you like

Origin blog.csdn.net/m0_68662723/article/details/131755320