Elementary C language - branch and loop statement (1) [detailed explanation]

Preface:
C language supports three structures: sequence structure, selection structure, loop structure

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

Branch structure (select statement)

if statement
switch statement

1.if statement

So what is the grammatical structure of the if statement?

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

code demo

#include <stdio.h>
//代码1,单分支
int main()
{
    
    
  int age = 0;
  //输入
  scanf("%d", &age);
  if(age<18)
    printf("未成年\n");
  return 0;
}
//代码2,双分支
#include <stdio.h>
int main()
{
    
    
  int age = 0;
  scanf("%d", &age);
  if(age<18)
  {
    
    
     printf("未成年\n");
  }
    else
  {
    
    
     printf("成年\n");
  }
  return 0;
}
//代码3,多分支
#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");
 }
 return 0;
}
//无论有多少条分支只有一条语句会被执行

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.
For example:

#include <stdio.h>
int main()
{
    
    
 int age = 0;
 scanf("%d", &age);
 if(age<18)
 {
    
    
    printf("未成年\n");
    printf("不可以吸烟、n");
 }
 return 0;
}

Explanation:
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.

1.1 Floating else

If you write a code like this

#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;
}
//没有结果输出

Amendment:

//适当的使用{}可以使代码的逻辑更加清楚。
//代码风格很重要
#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;
}

Note: Code style and typography are very important
Else matching : else matches the nearest if.

1.2 Comparison of if writing forms

//代码1
if (condition) {
    
    
   return x;
}
return y;
//代码2
if(condition)
{
    
    
   return x;
  //return表示条件成立从此处返回,不在执行后边的语句
}
else
{
    
      
   return y;
}
//代码3
int num = 1;
if(num == 5)
{
    
    
   printf("hehe\n");
}
//代码4
int num = 1;
if(5 == num)
//把常量放在左边更易于发现问题,如果少写一个等号,代码4不能运行而代码3可以
{
    
    
   printf("hehe\n");
}

Code 2 and code 4 in the above code are better, the logic is clearer, easy to understand and less error-prone.

1.3 Exercises

  1. Check if a number is odd
  2. Output odd numbers between 1-100
 //判断是否是奇数 - if
 //生成1~100之间的数 - while
  //判断是否是奇数
#include <stdio.h>
int main()
{
    
    
	int n = 0;
	scanf("%d", &n);
	//判断
	if (n % 2 == 1)
	printf("奇数\n");
	return 0;
}
    //输出1-100之间的奇数
#include <stdio.h>
int main()
{
    
    	
	int i = 1;
	while (i <= 100)
	{
    
    
	if (i % 2 == 1)
			printf("%d ", i);
		i++;
	}
	return 0;
}

2. switch statement

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

So what is the grammatical structure of the if statement?

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

//语句项是什么呢?

//是一些case语句:
//如下:
case 整形常量表达式:
语句;

The switch must be followed by an integer expression, and the case must be followed by an integer constant expression

2.1 break in switch statement

When we input 5, we will find that the program prints Friday, Saturday, and Sunday.
insert image description here
This is because in the switch statement, we cannot directly implement the branch, and the real branch can be realized by using it with break.
So the code is corrected to:

#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;
      default//如果case语句全部错误的情况下,代码就走default
        printf("选择错误\n");
        break;
    }
return 0;
}

When our needs change:

  1. Input 1-5, the output is "weekday";
  2. Input 6-7, output "weekend

Our code should look like this:

#include <stdio.h>
//switch代码演示
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;
}

good programming habits

Add a break statement after the last case statement.
(The reason for writing this way is to avoid forgetting to add a break statement after the last case statement).

2.2 default clause

When the value of the expression does not match the value of any case label, all statements of the structure will be 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 to the statement list by putting the following label

default:

write in anycaseWhere labels 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
after 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. good programming habits

It is a good habit to put a default clause in each switch statement, and you can even add a break after it.

2.3 Exercises

#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++};//case后有多条语句加上{}更好
        break;
      }
    case 4:
    m++;
    break;
    default:
    break;
}
  printf("m = %d, n = %d\n", m, n);
  //m=5,n=3
  return 0;
}

insert image description here

Guess you like

Origin blog.csdn.net/2201_75366661/article/details/128719195