Basic understanding of c language: if and switch sentence structure

Table of contents

one .if

The following figure shows the specific application of else if:

Two.switch

1. Structural Grammar of Choice

2. The following are specific examples

3. The difference between #include<> and "" (problems found during the learning process, make up)

1. We create a new project xx, which only contains a file main.cppEdit

2. Add a header file stdio.h to the xx project, and use #include "stdio.h"

3. Add #include to the stdio.h file


There are 5 major expression sentence patterns in C language:

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

This time it is described as the if switch of the loop branch statement in the control statement  

one .if

1.if (if true)

execute statement;

2.if(age<18)

          printf("Underage"); 

   else

          printf("Adult");                      

3. If you have multiple conditions, you can use else if

The following figure shows the specific application of else if:

#define _CRT_SECURE_NO_WARNINGS 
#include <stdio.h>
int main()
	int grad = 65;

	if (grad >= 90)
    {                                     //成绩大于等于90的为非常优秀 
		printf("%s", "非常优秀");         //在大于等于60且小于90为优秀
	}
	else if(grad >= 60 && grad < 90) //这里不能写成60<=grad<90,因为若grad=20,则会出现0<90
//命题为真则还会打印优秀,所以不能写成这种形式(下图有解释)
	{
		printf("%s", "优秀");
	}

Two. switch

1. Structural Grammar of Choice

switch selection structure
        syntax structure:
            switch(expression) {              case constant 1:                  code block 1;                 break;               case constant 2:                code block 2;                break;                ......                case constant n:                code block n;                 break;                default:                  code block m;                   break;              }













2. The following are specific examples

#include<stdio.h>
#define _CRT_SECURE_NO_WARNINGS 
int n = 1;
    int m = 2;
	switch (n)
	{
	case 1:m++;
	case 2:n++;
	case 3:
		switch (n)
		{            //swich可嵌套使用
		case 1:n++;
		case 2:m++; n++;
		    break;
		}
	case 4:
		m++;
		break;
	default:
	    break;
	}
	printf("%d %d",m,n);   //最后输出结果为5  3
	return 0;
}                                            //执行规律:
   根据switch中()里表达式的值,来匹配case后面的常量值,匹配上哪一个常量值,就执行对应的代码块,执行完代码块后。
 执行break,退出整个switch选择结构,如果表达式的值与所有的case常量值都不匹配,则执行default语句中的代码块,然后执行break结束整个switch选择结构。

注意:
       1)表达式的值可以是int、short、byte、char、枚举类型、String
       2)case常量值不能重复,后面只能是字面 量,不能是自变量。
       3)break的作用是结束整个switch结构,如果省略了,代码会继续向下执行,知道遇到break,或者执行到了default语句后结束整个switch选择结构
这个现象称之为“case穿透”,这个case穿透可以合理使用。
       4)default语句是在表达式的与所有的case常量值不匹配的时候执行,可以省略。
default语句可以写在switch选择结构中的任意位置,一般写在switch选择结构的末尾。

3. The difference between #include<> and "" (problems found during the learning process, make up)


There are two ways to write header files in C language: #include <stdio.h> and #include "stdio" (no space after #include). The difference between the two ways is as follows:

#include <header file> : The compiler will only look for header files from the library environment configured by the system, and will not search the current folder. Typically used to refer to standard library header files.
#include "header file" : The compiler will first look for the header file from the current folder, and if it cannot find it, it will go to the system default library environment to find it. It is generally used to refer to header files defined by users themselves.

1. We create a new project xx, which only contains a main.cpp file

 It can be seen that both #include "stdio.h" and #include<stdio.h> programs can run normally

Analysis: 1) When using #include<stdio.h> to add a header file, the compiler searches the standard library header file, finds the stdio.h header file, and copies it to the current line; 2) uses #include "stdio. h" when adding a header file, the compiler first searches for the stdio.h header file in the folder where the current project is located, and finds that there is no such header file, so the compiler searches for the stdio.h header file in the system default library environment, and finds the stdio.h header file. copy it to the current

2. Add a header file stdio.h to the xx project, and use #include "stdio.h"

The compiler reports an error because printf is not declared.

Analysis: When using #include "stdio.h" to add a header file, the compiler first searches for the stdio.h header file in the folder where the current project is located, and finds the user-defined stdio.h header file, so the compiler copies it to the current line. The header file expanded here is a user-defined header file. The user-defined stdio.h header file does not declare the printf function, so of course an error will be reported.


3. Add #include<stdio.h> to the stdio.h file

The program will run normally. 


! ! ! ! ! ! ! Pay attention to the role of #ifndef, #define, and #endif: to prevent header files from being repeatedly quoted and repeatedly compiled.

(Click to see details)

#ifndef It is shorthand for if not define, a kind of macro definition, to be precise, one of the three preprocessing functions (macro definition, file inclusion, conditional compilation) - conditional compilation.

Using #ifndef can avoid the following errors: If global variables are defined in the .h file, and a C file includes the .h file multiple times, if the #ifndef macro definition is not added, the variable definition error will appear repeatedly; if # ifndef will not cause this error.
 

Guess you like

Origin blog.csdn.net/fcccfffg/article/details/132086510