当前课程知识点:C Programming > Chapter 4 Selection Structure > 4.2 Switch Statement > 4.2 Switch Statement .mp4
大家好
我是云南大学信息学院丁海燕老师
欢迎走进C语言程序设计课堂
今天
我们讲解switch语句
下面来看例4.3
用switch语句来实现简单菜单程序
问题描述如下
运行程序
在屏幕上显示以下菜单
一
计算2个数的和
二
计算2个数的差
三
计算2个数的积
四
计算2个数的商
五
退出
用户根据菜单提示
输入一个整数
根据菜单编号
进行相应的计算
如果输入5
则退出程序
程序设计如下
一
数据描述
x,y为参加算术运算的两个数
为float型
z
为选择的菜单序号
为int型
二
算法描述
如图所示
首先
显示菜单
输入菜单编号z
判断z的值
和哪个整型常量匹配
如为1
输入x,y
输出x+y的值
如为2
输入x,y
输出x-y的值
如为3
输入x,y
输出x*y的值
如为4
输入x,y
输出x/y的值
z为其它值则退出程序
源程序如下
输入菜单项1
输入两个数3和5
输出和为8
switch语句的一般形式如下
switch (表达式)
{ case 常量表达式1
: 语句序列1
case 常量表达式2: 语句序列2
等等
case 常量表达式n: 语句序列n
default : 语句序列n+1
}
switch后面圆括号中
是运算结果为整型值
或字符型值的表达式
case后面必须是整型或字符型常量
或常量表达式
冒号后面为0个
或多个语句组成的语句序列
default子句可以有
也可以没有
其功能为
计算表达式的值
与常量表达式的值
比较
等于第i个值时
顺序执行语句序列i
i+1
n+1
若与所有常量表达式值都不相等
执行语句序列n+1
注意
case 常量表达式i
等价于语句标号
计算出的表达式值
等于哪个语句标号
就从哪个位置开始顺序向下执行
语句序列
break语句
可以改变case的语句标号作用
终止后续case语句序列的执行
所以
switch与break语句结合
才能实现程序的分支
允许switch嵌套使用
但同一个switch语句中
任意两个case的常量表达式值
不能相同
switch语句的N-S结构图
如图所示
例如
switch (a)
{ case 5: printf("&")
case 2: printf("#")
default:printf("$")
表示当a等于5
输出 $
当a等于2时
输出
#$
#$
表示当a是其他值时
输出$
又例如
switch (a)
{ case 5: printf("&"); break
当a等于5
此时输出&
case 2: printf("#"); break
当a等于2
则输出#
default: printf("$"); break
当a是其他值
则输出$}
可见switch与break语句
结合才能实现程序的分支
下面我们来看例4.4
将百分制成绩
转换成对应的等级制成绩
问题描述如下
输入一个百分制成绩
将其转换成对应的等级制成绩
90-100 分为A 级
80-89 分为B 级
70-79 分为C 级
60-69 分为D 级
0-59 分为E 级
一
数据描述
score
代表百分制成绩
int型
grade
表示成绩等级
char类型
temp
临时变量
int型
二
算法描述
分析如下
由于score为
[0,100]之间的整数
不能直接用score
作为switch中的表达式
必须对其进行处理
用score/10
就可将
[0,100]之间的若干多个数
转换成五个等级
就能利用switch语句
完成相应的分级工作
该算法流程图
如图所示
输入score
temp=score/10
temp=10或9吗
成立grade=‘A’
判断temp=8吗
成立grade=‘B’
判断temp=7吗
成立 grade=‘C’
判断temp=6吗
成立grade=‘D’
temp为其它值
grade=‘E’
输出grade
程序描述如下
{ int score,temp
定义score和temp两个整型变量
char grade
定义grade为字符变量
提示输入一个百分制成绩
输入成绩
temp=score/10;
switch(temp)
{ case 10:
case 9: grade='A';break;
case 8: grade='B';break;
case 7: grade='C';break;
case 6: grade='D';break;
default: grade='E';break;
}
最后输出score和grade的值
return 0;
}
运行程序
输入89
输出89分对应的等级为B
好了
以上就是switch语句的内容
下面
我们总结一下这一章的内容
if语句
需要掌握if语句的格式和功能
掌握if嵌套的概念和应用
if语句有单分支
双分支和多重分支3种格式
表达式
是选择判断和执行分支的依据
注意正确计算表达式的值
如果内嵌语句包含多条语句
必须用{}将其括起来
构成复合语句
否则会引起逻辑错误
if语句嵌套使用时
else
总是和离它最近的
尚未与其他else配对的if
配对使用
二
switch语句
掌握switch语句的格式和功能
注意switch语句的书写格式
当switch语句后面表达式的值
与某个case常量值相等时
执行相应的case分支
及后续语句
只有与break语句结合使用
switch语句才能实现程序的选择控制
同一个switch语句中
任意两个case的常量值不能相同
但是在switch嵌套或并列时
不同的switch语句的case常量值
不受此限制
好了
switch语句我们就学到这儿
同学们在课后一定要多加复习
我们下节课再见
-1.1 The development and characteristics of C language
--1.The development and characteristics of C language .mp4
--1.1 Self test questions
-1.2 A simple C Language program
--1.2 A simple C Language program.mp4
--【Source program】 Example 1.1 output a line of text Hello, world! "
--【Source program】 Example 1.2 program composed of multiple functions, find the larger of two integers
--1.2 Self test questions
-1.3 Program, Programming Language and C Program Running Steps
--1.3 Program, Programming Language and C Program Running Steps.mp4
--Running steps of C source program in CodeBlocks
--1.3 Self test questions
-chapter 1 Self-Test
-2.1 The Concept and Description of Algorithms
--2.1 The Concept and Description of Algorithms.mp4
--2.1 Self test questions
-2.2 Examples of Simple Algorithms, Computational Thinking and Structured Programming
--2.2 Examples of Simple Algorithms, Computational Thinking and Structured Programming.mp4
--2.2 Self test questions
-Chapter 2 Self test questions
-3.1 Simple Structure and Identifier of C Language Program
--3.1 Simple Structure and Identifier of C Language Program.mp4
--【Source program】 Example 3.1 input two integers and output the sum of two numbers.
--【Source program】 Example 3.2 input two integers and output the average value.
--【Source program】 Example 3.3 output the value of the character variable.
--3.1 Self test questions
-3.2 Constants, Variables and Assignments
--3.2 Constants, Variables and Assignments.mp4
--3.2 Self test questions
-3.3 Arithmetic, assignment, increment and decrement operators
--3.3 Arithmetic, assignment, increment and decrement operators.mp4
--3.3 Self test questions
-3.4 Conditions, commas, addresses, byte operators, and mixed operations among various numerical data
--3.4 Conditions, commas, addresses, byte operators, and mixed operations among various numerical data
--【Source program】 example of sizeof
--3.4 Self test questions
-3.5 Input and Output Examples and Character Input and Output
--3.5 Input and Output Examples and Character Input and Output.mp4
--【Source program】 Example. Find the root of quadratic equation of one variable.
--【Source program】 Example 1. Successively output three characters of BOY.
--【Source program】Example 2. Input 3 characters of BOY from the keyboard and output them to the screen
--3.5 Self test questions
-3.6 Formatted output printf function
--3.6 Formatted output printf function.mp4
--【Source program】 Example 3.4 output of integer data.
--【Source program】 Example 3.5 real data output.
--【Source program】 Example 3.6 character data output.
--【Source program】 Example 3.7 uses %s to output a string.
--3.6 Self test questions
-3.7 Formatted input scanf function
--3.7 Formatted input scanf function.mp4
--【Source program】 Example 3.8 Input and output integer data
--【Source program】Example 3.9 Input and output single precision and double precision real data.
--【Source program】Example 3.10 input and output character data.
--【Source program】Example 3.11 input and output string.
--3.7 Self test questions
-3.8 Basic Data Types of C Language
--3.8 Basic Data Types of C Language.mp4
--3.8 Self test questions
-Chapter 3 Self test questions
-Operator and expression self test questions
-4.1 Relational operators, logical operators, and if statements
--4.1 Relational operators, logical operators, and if statements.mp4
--【source example】Example 4.1 Find the root of the quadratic equation of one variable.
--【Source program】Example 4.2 Input two real numbers and output them from small to large.
--4.1 Self test questions
-4.2 Switch Statement
--【Source program】Example 4.3 uses switch statement to implement simple menu program
--【Source program】Example 4.4 converts centesimal score into the corresponding grade system score.
--4.2 Self test questions
-4.3 Examples of Selection Structural Programs
--4.3 Examples of Selection Structural Programs.mp4
--【Source program】 Example 4.5 determines whether a year is a leap year.
--【Source program】 Example 4.6 find the solution of quadratic equation of one variable.
--【Source program】Example 4.7 calculate the transportation cost for the user.
--4.3 Self test questions
-Chapter 4 Self test questions
-5.1 While and Do-While Statement
--5.1 While and Do…while statement.mp4
--【Source program】example 5.1 Use while statement to find 1 + 2 + 3 + +100
--【Source program】example 5.2 using do while statement to find 1 + 2 + 3 + +100
--5.1 Self test questions
-5.2 For Statement
--【Source program】example 5.3 Use for loop to find 1 + 2 + 3 +100
--5.2 Self test questions
-5.3 Change the State of Loop Execution and Nested Loop
--5.3 Change the state of loop execution and nested loop .mp4
--【Source program】example 5.4 collecting charity donations
--【Source program】example 5.5 output a number between 100 and 200 that cannot be divided by 3.
--【Source program】multiplication table
--【Source program】example 5.6 Output the following 4*5 matrix.
--5.3 Self test questions
-5.4 Loop Structure Program example 1
--5.4 Loop structure program example 1.mp4
--5.4 Self test questions
-5.5 Loop Structure Program example 2
--5.5 Loop structure program example 2.mp4
--【Source program】example 5. Output the following figure.
--【Source program】example6. The problem of 100 chickens and 100 coins
--【Source program】example 7. Find the approximate value of PI
--5.5 Self test questions
-Chapter 5 Self test questions
-6.1 Definition, Reference and Initialization of One-Dimensional Arrays
--6.1 Definition, Reference of One-Dimensional Arrays.mp4
--6.1 Self test questions
-6.2 One-Dimensional Array Programming
--6.2 One-Dimensional Array Programming.mp4
--【Source program】example 1. Fibonacci sequence
--【Source program】example 2. Find the maximum value and the minimum value.
--【Source program】example 3 exchanging array elements in reverse order.
--【Source program】example 4. Bubble sorting.
--6.2 Self test questions
-6.3 Definition, Reference and Initialization of Two-Dimensional Arrays
--6.3 Definition, Reference of Two-Dimensional Arrays.mp4
--6.3 Self test questions
-6.4 Two-Dimensional Array Programming
--6.4 Two-Dimensional Array Programming.mp4
--【Source program】example 1. Get the average score of each subject.
--【Source program】Example 2. The elements of row and column of a 2D array are interchanged
--6.4 Self test questions
-6.5 Definition, initialization and input and output of character arrays
--6.5 Definition, initialization and input and output of character arrays .mp4
--【Source program】Example 6.6 Output a known string.
--【Source program】Example 6.7 Output a diamond.
--【Source program】Example 6.10 Sorting of strings.
--6.5 Self test questions
-6.6 String Processing Function
--6.6 String Processing Function.mp4
--6.6 Self test questions
-6.7 Character Array Programming
--6.7 Character Array Programming.mp4
--6.7 Self test questions
-Chapter 6 Self test questions
-7.1 Function Concept and How to Define and Call Functions
--7.1 Function Concept and How to Define and Call Functions.mp4
--7.1 Self test questions
-7.2 Data Transfer in Function Call, Call Procedure and Function Return Value
--7.2 Data Transfer in Function Call, Call Procedure and Function Return Value.mp4
--7.2 Self test questions
-7.3 Declarations of called functions and nested calls to functions
--7.3 Declarations of called functions and nested calls to functions .mp4
--【Source program】Example 7.4 use a function to find the sum of t
--7.3 Self test questions
-7.4 Recursive call to function
--7.4 Recursive call to function.mp4
--【Source program】Example 7.6. How old is the fifth student?
--【Source program】Example 7.7 Use recursion method to find n!
--7.4 Self test questions
-7.5 Array as function parameter (1)
--7.5 Array as function parameter (1).mp4
--【Source program】Example 7.10
--7.5 Self test questions
-7.6 Array as function parameter (2)
--7.6 Array as function parameter (2).mp4
--【Source program】Selection sorting.
--【Source program】Example 7.13 find the maximum of a 3×4 matrix.
--7.6 Self test questions
-7.7 Local and global variables, internal and external functions
--7.7 Local and global variables, internal and external functions.mp4
--【Source program】Example 7.14
--7.7 Self test questions
-7.8 The Survival Period of Variables and the Storage Mode of Local Variables
--7.8 The Survival Period of Variables and the Storage Mode of Local Variables.mp4
--【Source program】Example 7.17
--7.8 Self test questions
-7.9 Storage Categories of Global Variables
--7.9 Storage Categories of Global Variables.mp4
--7.9 Self test questions
-Chapter 7 Self test questions
-8.1 Pointer Concept, Definition and Reference of Pointer Variables
--8.1 Pointer Concept, Definition and Reference of Pointer Variables.mp4
--8.1 Self test questions
-8.2 Pointer variables as function parameters
--8.2 Pointer variables as function parameters.mp4
--【Source program】Example 8.3. Exchange two data with function call.
--8.2 Self test questions
-8.3 Pointer of array element, operation of pointer and reference of array element by pointer
--8.3 Pointer of array element, operation of pointer and reference of array element by pointer.mp4
--【Source program】Point to array elements with pointer variables.
--8.3 Self test questions
-8.4 Using Array Name as Function Parameter
--8.4 Using Array Name as Function Parameter.mp4
--【Source program】Store array elements in reverse order.
--8.4 Self test questions
-8.5 Reference to multidimensional arrays by pointers
--8.5 Reference to multidimensional arrays by pointers.mp4
--【Source program】Pointer variable pointing to one-dimensional array.
--8.5 Self test questions
-8.6 Referencing strings through pointers
--8.6 Referencing strings through pointers.mp4
--【Source program】Reference string by pointer.
--8.6 Self test questions
-8.7 Character pointer as function parameter
--8.7 Character pointer as function parameter.mp4
--【Source program】Character pointer as function parameter.
--8.7 Self test questions
-8.8 Pointer pointing to function
--8.8 Pointer pointing to function.mp4
--【Source program】Pointer variable pointing to the function.
--8.8 Self test questions
-8.9 Functions that return pointer values
--8.9 Functions that return pointer values.mp4
--【Source program】Intercept substring.
--8.9 Self test questions
-8.10 Pointer arrays and multiple pointers
--8.10 Pointer arrays and multiple pointers.mp4
--【Source program】Sorting of strings.
--8.10 Self test questions
-8.11 Dynamic memory allocation and pointer variables pointing to it
--8.11 Dynamic memory allocation and pointer variables pointing to it.mp4
--【Source program】Dynamic memory allocation.
--8.11 Self test questions
-Chapter 8 Self test questions
-9.1 Define and use structural variables
--9.1 Define and use structural variables.mp4
--【Source program】Structure variable
--9.1 Self test questions
-9.2 Using structure arrays
--9.2 Using structure arrays.mp4
--【Source program】Structure array
--9.2 Self test questions
-9.3 Structure pointer
--【Source program】Structure pointer
--9.3 Self test questions
-Chapter 9 Self test questions
-CodeBlocks Baidu online disk download address
-Final Exam
--final exam