C++_Switch Statement
2015-08-07 11:14
344 查看
Switch is C++'s multiway decision statement.
It works like this: the value of an expression is successively tested against a list of integer or character constants. When a match is found, the statement sequence associated with that match is executed. The general form of the switch statement is
**************************************
switch(expression){
case constant1:
statement sequence
break;
case constant2:
statement sequence
break;
case constant3:
statement sequence
break;
.
.
.
.
default:
statement sequence
}
**************************************
The switch expression must evaluate to either a character or an integer value. In other words, floating-point expressions are not allowed. Frequently, the expression controlling the switch is simply a variable.
break stops the execution of code within a switch.
There are four important things to know about the switch statement.
1. The switch differs from the if in that switch can test only for equality, whereas the if conditional expression can be of any type.
2. No two case constants in the same switch can have identical values. Of course, a switch statement enclosed by an outer switch may have case constants in common.
3. A switch statement is usually more efficient than nested ifs.
4. The statement sequences associated with each case are nor blocks. However, the entire switch statement does define a block. The importance of this will become apparent as you learn more about C++.
Standard C++ specifies that a switch can have at least 16834 case statements.
You can have empty cases.
It works like this: the value of an expression is successively tested against a list of integer or character constants. When a match is found, the statement sequence associated with that match is executed. The general form of the switch statement is
**************************************
switch(expression){
case constant1:
statement sequence
break;
case constant2:
statement sequence
break;
case constant3:
statement sequence
break;
.
.
.
.
default:
statement sequence
}
**************************************
The switch expression must evaluate to either a character or an integer value. In other words, floating-point expressions are not allowed. Frequently, the expression controlling the switch is simply a variable.
break stops the execution of code within a switch.
There are four important things to know about the switch statement.
1. The switch differs from the if in that switch can test only for equality, whereas the if conditional expression can be of any type.
2. No two case constants in the same switch can have identical values. Of course, a switch statement enclosed by an outer switch may have case constants in common.
3. A switch statement is usually more efficient than nested ifs.
4. The statement sequences associated with each case are nor blocks. However, the entire switch statement does define a block. The importance of this will become apparent as you learn more about C++.
Standard C++ specifies that a switch can have at least 16834 case statements.
You can have empty cases.
相关文章推荐
- Sublime 在win上搭建C++环境
- C++抽象机制之二:运算符重载
- HDOJ 2024 C语言合法标识符(水)
- C++ Vector
- C++ AMP 介绍(两)
- c++ STL中的set容器
- C++模板元编程
- c++子类和父类成员函数重名
- c语言》排序法
- #编码风格#C++ Programming Style Guidelines
- VC++开发中遇到的一些问题整理
- 怎样从一个DLL中导出一个C++类
- C++格式化输出
- C/C++ vector详解
- 浅析C++标准库元组(tuple)源码
- (C/C++学习笔记)Copy构造函数应用场景
- C++ 头文件重复包含
- C/C++获取当前系统时间
- C语言基础、技巧_自总结版
- C++ Hello World