C language learning #004_ branch statement (if, switch), loop statement (while, do while, for), goto statement

branch statement

In C language, all numbers other than 0 are true.

if statement

if

If the expression is true , execute the following statement

if (表达式) 
{
    
    
	语句
}

if else

If the expression in the if brackets is true , execute the statement below the if

Otherwise, if the expression in the if bracket is false , execute the statement below else


if (表达式)
{
    
    
	语句
}
else  
{
    
    
	语句
}

else if

If the expression 1 in the if brackets is true , execute the statement below if

If the expression 1 in the if brackets is false , then the expression 2 in the else if brackets is true , execute the statement in the else if

If the expression 1 in the if brackets and the expression 2 in the else if brackets are both false , execute the statement below else


if (表达式)
{
    
    
	语句
}
else if(表达式2)
{
    
    
	语句
}
else
{
    
    
	语句
}

switch

General switch use

switch (表达式)
{
    
    
    case 常量表达式1: 语句1
    case 常量表达式2: 语句2
    case 常量表达式n: 语句n
    default: 语句 n + 1
}

use of switch

#define _CRT_SECURE_NO_WARNINGS 1	

#include <stdio.h>

int main()
{
    
    
	int n = 1;

	scanf("%d", &n);

	// 判断输入的数字是多少
	// 1 的话就输出 星期一
	// 2 的话就输出 星期二
	// 以此类推
	// default 是指除了 case 的其他的数字(常量表达式)
	switch (n)
	{
    
    
		case 1:
			printf("星期一\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:
			printf("只有星期 1 - 7\n");
			break;
	}

	return 0;
}

loop statement

while loop

while (表达式) // 满足条件就继续循环,直到条件不满足为止
{
    
    
	语句; // 每次循环都会执行花括号内的语句
}

Use of while loop (print 1 - 10 numbers)

insert image description here

#define _CRT_SECURE_NO_WARNINGS 1	

#include <stdio.h>

// while循环
int main()
{
    
    
	int i = 1;
	// i 小于或等于 10,则满足条件
	while (i <= 10)
	{
    
    
		// 打印 i
		printf("%d ", i);
		// 每次执行循环的时候 i 都会加 1
		i++;
	}

	return 0;
}

Use of while loop (getchar and putchar)

insert image description here

#define _CRT_SECURE_NO_WARNINGS 1	

#include <stdio.h>

int main()
{
    
    
	int ch = 0;

	// getchar() 返回结果为 int 类型,是输入的字符的ACSII码值
	// 每次循环都让玩家输入一个字符
	// 如果不是文件结束符就继续循环
	while ((ch = getchar()) != EOF)
	{
    
    
		// 将每次输入的字符打印出来
		putchar(ch);
	}
		 
	return 0;
}

Use the while loop to clear the buffer so that getchar() will not get the wrong character

insert image description here

#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>

int main()
{
    
    
	char ch[20] = {
    
     0 };

	printf("请输入密码: ");
	scanf("%s", ch);

	// 清空缓存区中的内容
	int tmp = 0;
	while ((tmp = getchar()) != '\n')
	{
    
    
		;
	}

	printf("请确认密码(Y/N): ");
	int c = getchar();
	if (c == 'Y' || c == 'y')
	{
    
    
		printf("确认成功\n");
	}
	else
	{
    
    
		printf("确认失败\n");
	}

	return 0;
}

do while

for

break

break can jump out of the entire loop

insert image description here

#define _CRT_SECURE_NO_WARNINGS 1	

#include <stdio.h>

// break
int main()
{
    
    
	int i = 1;

	while (i <= 10)
	{
    
    
		// 当 i 等于 5 时, 跳出循环
		if (i == 5)
		{
    
    
			break;
		}
		
		printf("%d ", i);

		i++;
	}

	return 0;
}

continue

continue can skip the current cycle

insert image description here

#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>

// continue
int main()
{
    
    
	int i = 1;

	while (i <= 10)
	{
    
    
		// 当 i 等于 5 时, 跳过本次循环,不再继续执行代码
		// 则后面的 i++ 没有运行,所以这是死循环
		if (i == 5)
		{
    
    
			continue;
		}
		printf("%d ", i);

		i++;
	}

	return 0;
}

goto statement

Exercise 1, determine which stage the age is in (underage, youth, middle age, prime of life, old age)

Enter an age
Judgment requirements:
1. Determine whether it is a negative number (age cannot be negative)
2. If the age is less than 18 years old, print underage,
3. If the age is less than 26 years old, print youth,
4. If the age is less than 40 years old, print middle age,
5. If the age is less than 60 years old, print the middle age,
6. If the age is greater than 60, print the old age

#define _CRT_SECURE_NO_WARNINGS 1	

#include <stdio.h>

// 练习
// 输入一个年龄
// 判断要求:
// 1. 判断是否是负数(年龄不能为负)
// 2. 年龄小于18岁,打印未成年,
// 3. 年龄小于26岁,打印青年,
// 4. 年龄小于40岁,打印中年,
// 5. 年龄小于60岁,打印壮年,
// 6. 大于60,打印老年
int main()
{
    
    
	// 声明保存年龄的变量
	int age = 0;

	printf("请输入一个年龄: ");
	// 输入一个年龄
	scanf("%d", &age);

	// 判断
	if (age < 0) 
	{
    
    
		// 打印结果
		printf("输入的年龄不能为负数\n");
	}
	else if (age < 18)
	{
    
    
		// 打印结果
		printf("未成年\n");
	}
	else if (age < 26)
	{
    
    
		// 打印结果
		printf("青年\n");
	}
	else if (age < 40)
	{
    
    
		// 打印结果
		printf("中年\n");
	}
	else if (age < 60)
	{
    
    
		// 打印结果
		printf("壮年\n");
	}
	else
	{
    
    
		// 打印结果
		printf("老年\n");
	}

	return 0;
}

insert image description here

Exercise 2, judge whether a number is odd or not

Check if a number is odd

#define _CRT_SECURE_NO_WARNINGS 1	

#include <stdio.h>

// 打印 1 - 100 之间的 奇数
int main()
{
    
    
	int number = 0;

	printf("请输入一个数字判断是不是奇数: ");
	scanf("%d", &number);

	// 判断是不是被2整除,是的话就不是奇数,不是的话就是奇数
	if (number % 2)
	{
    
    
		printf("\n%d是奇数\n", number);
	}
	else
	{
    
    
		printf("\n%d不是奇数\n", number);
	}

	return 0;
}

Guess you like

Origin blog.csdn.net/My_csdnQMDX/article/details/127021970