C语言基础02--条件语句

Catalogue
  1. 1. C语言基础02–条件语句
    1. 1.0.1. goto语句
    2. 1.0.2. if-else语句
    3. 1.0.3. switch语句
    4. 1.0.4. while语句
    5. 1.0.5. do-while语句
    6. 1.0.6. for语句

C语言基础02–条件语句

goto语句

类似于汇编中的JMP,尽量不要用goto,实在没办法再用

1
2
3
4
5
6
7
8
9
#include<stdio.h>

int main() {
goto loop;
printf("1");
loop:
printf("2");
return 0;
}

if-else语句

1
2
3
4
5
6
7
8
9
int main(){
if(true){
//条件为true,执行这里
}else{
//反之,执行这里
}
//cmp
//jnz
}

switch语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main(){
int n1=5;
switch(n1){
case 1:
printf("1");break;
case 2:
printf("2");break;
default:
printf("?");break;
}
return 0;
//cmp
//jz
//cmp
//jz
}

while语句

1
2
3
4
5
6
7
8
9
10
11
12
13
int main(){
int n=100;
while(n>1){//满足条件就开始循环
printf("n");
n--;
}
return 0;
// mov
// cmp
// call
// dec
// jmp
}

do-while语句

1
2
3
4
5
6
7
8
9
10
11
12
int main(){
int n=0;
do{ //先执行一遍
printf("1");
n++;
}while(n<100); //再判断条件进行循环
return 0;
}
// mov
// {}
// cmp
// jz

for语句

1
2
3
4
5
6
7
8
9
10
11
12
#include<stdio.h>
int main(){
for(int i=0;i<100;i++){//类似while,但是把定义判断循环整合在一起了,类似于while的简写
printf("1");
}
}
// mov
// cmp
// jz
// call
// jmp
// inc