面向对象程序设计 switch语句:多情况选择 形式 switch(expression) case cI: statements 堂提要 break 第五章C++基础 case c2: statements break 51C++概述 case c3: statements 52数据和表达式 break 53基本语句 default: statements 54函数 55数组与指针 流程先计算exp的值,再相应的 语句,若表达式的值不在{ci} 之内,执行 default语句
第五章 C++基础 5.1 C++概述 5.2 数据和表达式 5.3 基本语句 5.4 函数 5.5 数组与指针 switch 语句:多情况选择 形式 switch (expression) { case c1: statements; break; case c2: statements; break; case c3: statements; break; default: statements; } 流程 先计算 exp 的值,再相应的 语句,若表达式的值不在{ci} 之内,执行 default 语句
面向对象程序设计 switch语句:多情况选择流程 expression 堂提要 case a stats break F 第五章C++基础 51C++概述 52数据和表达式 case y stats break 53基本语句 54函数 T case Z stats break 55数组与指针 default stats
第五章 C++基础 5.1 C++概述 5.2 数据和表达式 5.3 基本语句 5.4 函数 5.5 数组与指针 switch 语句:多情况选择流程 expression case a stats. break T … F case y stats. break case z stats. break T F T F default stats
面向对象程序设计 WF 2: #include<iostream. h> void main() i int m cout < n input an integer cin>> m 堂提要 switch(m) i case 1: cout <<"one"<< endl; break 第五章C++基础 case2:cou<"tWo<<end; break;5,C十概述 case3:cout<<" three"<<endl; break:52数据和表达式 default: cout < " default"<< endl 53基本语句 54函数 55数组与指针 结果 Input an integer 2 two
第五章 C++基础 5.1 C++概述 5.2 数据和表达式 5.3 基本语句 5.4 函数 5.5 数组与指针 例2:#include<iostream.h> void main( ) { int m; cout << "\n input an integer:" ; cin >> m; switch(m) { case 1: cout << "one"<< endl; break; case 2: cout << "two" << endl; break; case 3: cout << "three" << endl; break; default: cout << "default" << endl; } } input an integer: 2 two 结果
面向对象程序设计 switch语句注意点 case后的常量类型= switch中表达式类型 堂提要 第五章C++基础 这些类型只能为:int、char、enum 51C++概述 在每个ae中,必须加入beak,以达到仅52数据和表达式 选一种的目的 54函数 55数组与指针
第五章 C++基础 5.1 C++概述 5.2 数据和表达式 5.3 基本语句 5.4 函数 5.5 数组与指针 switch 语句注意点 • case 后的常量类型 = switch中表达式类型 • 这些类型只能为:int、char、 enum • 在每个 case 中, 必须加入 break, 以达到仅 选一种的目的
面向对象程序设计 重复控制语句: while和do/ while 形式 while(condition) statements 堂提要 第五章C++基础 do 51C++概述 statements 52数据和表达式 3 while(condition) 53基本语句 54函数 区别hil先验证条件,再执行语句 55数组与指针 do/ while:先执行语句,再验证条件 注意不能死循环,即无限循环
第五章 C++基础 5.1 C++概述 5.2 数据和表达式 5.3 基本语句 5.4 函数 5.5 数组与指针 重复控制语句:while 和 do/while 形式 while (condition) { statements; } do { statements; } while (condition); 区别 while: 先验证条件,再执行语句 do/while: 先执行语句,再验证条件 注意 不能死循环,即无限循环