当前课程知识点:C/C++:从基础语法到优化策略 >  Final Exam >  Final exam >  3.7 switch statement

返回《C/C++:从基础语法到优化策略》慕课在线视频课程列表

3.7 switch statement在线视频

返回《C/C++:从基础语法到优化策略》慕课在线视频列表

3.7 switch statement课程教案、知识点、字幕

我们再来说一说switch语句

switch语句的话 他是告诉计算机来选

哪一段代码去执行的 来执行

那么它的语法是这个样子 switch后面跟着一个表达式

首先这个表达式的值必须是整数

然后根据这个表达式的不同的值

然后如果它的值是1 label 1 就是这个值是 1 那就执行这段代码

如果是 2 就执行这段 严格来讲 它其实是这样的

如果是这个值 他就先测试一下 如果满足这个条件

那就执行statements这个以及往下执行

除非碰到break

如果这个不满足 那就看第二个

如果第二个满足的话 那就会从第二个开始往下执行这些statement

那如果不满足 就继续再往下找

最后都没有的话 那就是执行default指定的这个命令

如果没有default那就什么都不做就退出 退出了

所以这里面要特别注意的是

我们一般用switch的时候一定要用break

当然也不是说一定要 但是基本上都要

但break的作用不要弄错

我们来看这个例子 ok

在这个例子里面 我们用户输入一个choice

从标准输入输入 这是一个整数

然后如果它不等于五这个循环就不停的进行

然后每进行一次 就showmenu显示菜单并又再读一个数值

就是说他让我们不停的去输入

如果输入一那就说 就打印出来 here is alarm

如果输入二那就report 大家看 这边都后面都跟着break

如果三 那就是The boss was in all day

四的话 comfort 就调这个函数 然后break

如果前面这些都不是 就default就会输出That`s not a choice

我们来看这个代码 看这个代码 我们去运行一下

好 我们运行他会

他说please enter 1, 2, 3, 4, or 5 那么

这里面是通过这个showmenu()打印出来的

前面应该显示一个showmenu() showmenu是什么 就是这

他啥都不做 他就打印了一段话给我们看

他打印一段话给我们看

然后那我们现在执行了程序 执行了这一步了 我们做一个选择 选几呢 选择一

选个一如果一回车1≠5然后就会进入switch 我回车

好一的话 他立刻就打印了这句话 Here is a alarm

那就是一那就cout<

打印之后呢 然后break break很重要 break之后

然后就跳出来执行showmenu() showmenu()

然后就是下面打印出这一段

才来这 然后再等我们再输入

现在其实程序已经运行到这了 那我们再输个什么呢 输个二

输个二 输个二的话 刚才做什么 他说

It`s been an excellent week for business

Sales are up 120%. Expenses are down 35%

那这个就是一个report 打印出来的report在干嘛

我们看看函数report就是打这句话的 啥也没做 然后又把menu打印了一遍

所以说就这样去执行 如果我们输入一个值 输个八他会说 That`s not a choice

这里面需要注意的是 如果我把这些break都去掉了

都去掉了那会怎么样 那会怎么样

我都去掉之后或者说我把这个简单一点

一就说Pressed 1.

这样这样 这个地方 2 3 4

pressed unknow

好了 假如是这样的话 我们来看

他会怎么样 我们这个程序五 quit 那退出 重新编译一下

然后运行ok 他让我们去输12345如果我们输给 输个九

他会说Pressed unknow

如果输个五就退出了 我们先输个四 输个四

不对呀 我输了四来看Pressed 4 Pressed unknow.

他把这两个都打印出来了 那我们再输个三看看

输个三的话 我们把这往上拉一下

输个三的话 那他就34 unknow 都打出来了

输个二呢

哦哦 我刚才输入了

C/C++:从基础语法到优化策略课程列表:

Week 1 Quick start

-1.1 Basic knowledges

-1.2 Setting Out to C++

-Lecture notes

-Example code

-Quiz 1

Week 2 Data types

-2.1 Compile and link

-2.2 Integers

-2.3 Floating-point numbers

-2.4 C++ Arithmetic Operators

-Lecture notes

-Example code

-Quiz 2

Week 3 Expressions and statements

-3.1 Some operators

-3.2 for loop

-3.3 Relational expressions (> < ==)

-3.4 while loop

-3.5 Branching statements (if else)

-3.6 Logical expressions

-3.7 switch statement

-3.8 break and continue statements

-3.9 File input and output

-Lecture notes

-Example code

-Quiz 3

Week 4 Array, string and structures

-4.1 Array

-4.2 Array strings

-4.3 string class strings

-4.4 Structures, Unions and Enumerations

-Lecture notes

-Example code

-Quiz 4

Week 5 Pointers and memory management

-5.1 Pointers

-5.2 Allocate memory: C style

-5.3 Allocate memory: C++ style

-5.4 Managing memory for data

-Lecture notes

-Example code

-Quiz 5

Week 6 Functions

-6.1 Function review

-6.2 Various functions

-6.3 Recursion and pointer to functions

-6.4 Reference and const

-Lecture notes

-Example code

-Quiz 6

Week 7 Functions and optimization

-7.1 Adventures in functions

-7.2 Speedup your program

-Lecture notes

-Example code

-Quiz 7

Week 8 ARM and OpenCV

-8.1 C/C++ with ARM development board

-8.2 Some tricks in OpenCV

-Lecture notes

-Example code

-Quiz 8

Week 9 Class

-9.1 Classes in C++

-9.2 Constructors and destructors

-9.3 this pointer

-Lecture notes

-Example code

-Quiz 9

Week 10 Operator overloading and type casts

-10.1 Operators in cv::Mat in OpenCV

-10.2 Operator overloading

-10.3 Friend functions

-10.4 Automatic conversions and type casts for classes

-Lecture notes

-Example code

-Quiz 10

Week 11 Dynamic memory management and classes

-11.1 Dynamic memory and classes

-11.2 New and improved String class

-11.3 Using pointers to objects

-Lecture notes

-Example code

-Quiz 11

Week 12 Class inheritance and memory management

-12.1 Class inheritance

-12.2 Static and dynamic binding

-12.3 Access control: protected

-12.4 Inheritance and dynamic memory allocation

-Lecture notes

-Example code

-Quiz 12

Week 13 Constructor, assignment and class templates

-13.1 Constructor and assignment

-13.2 Classes with object members

-13.3 Private inheritance

-13.4 Class templates

-Lecture notes

-Example code

-Quiz 13

Week 14 CNN, exceptions in C++

-14.1 CNN for image classification

-14.2 Exceptions

-Lecture notes

-Example code

-Quiz 14

Week 15 Friends, nested classes, RTTI and type cast

-15.1 Friends

-15.2 Nested classes

-15.3 RTTI and type cast operators

-Lecture notes

-Example code

-Quiz 15

Final Exam

-Final exam

3.7 switch statement笔记与讨论

也许你还感兴趣的课程:

© 柠檬大学-慕课导航 课程版权归原始院校所有,
本网站仅通过互联网进行慕课课程索引,不提供在线课程学习和视频,请同学们点击报名到课程提供网站进行学习。