选择语句 循环语句 scanf getchar

选择语句分为if语句与switch语句。

if语句

#include <stdio.h>
int main()
{    
    int a = 0;
    if(判断条件为真)
        语句;
    else//判断条件为假
        语句;
    return 0;
}

可以嵌套使用 !!!else 与最近的 if 搭配除非写成代码块形式。

若多分枝可以

if ()
    语句;
else if ()
    语句;
else
    语句;

switch语句

#include <stdio.h>
int main()
{
    int a = 0;
	switch (整形表达式)
	{
		case (整形常量表达式):
		{
			break;//跳出选择语句
		}
		.......//可以有多个语句;
		default://case情况都不匹配到才执行的语句
		{
			break;
		}
	}
	return 0;
}

switch语句也可以嵌套使用!!!

循环语句分为for循环while循环(do while 以后补充)

for循环语句

#include <stdio.h>
int main()
{
	int a = 0;
	for (表达式1; 表达式2; 表达式3)
		//初始化循环变量;   判断;  调整;
	{
		循环语句;
	}
	return 0;
}

可以嵌套使用

for循环里的continue与break

continue是跳过本次循环里的代码进入下一次循环,进入下一次循环时会进行调整

break是结束循环语句

while循环语句

#include <stdio.h>
int main()
{
	int a = 0;//初始化变量
	while ()//判断
	{
		a++;//调整
	}
	return 0;
}

 也可以嵌套使用!!!

while里面的continue与break

continue跳过本次循环代码进入下一次循环,因为调整语句在被跳过的代码里所以不会进行调整

break与for循环一致。

 

scanf与getchar

scanf与getchar与键盘之间有一个过渡区叫缓冲区

当读到\n时会等待输入也就是回车键   可以利用getchar来清空缓冲区

int main()
{
	int temp = 0;
	while ((temp = getchar()) != \n)
	{
		;
	}
	return 0;
}

おすすめ

転載: blog.csdn.net/m0_65140642/article/details/122518487