返回   cpper编程论坛 > C/CPP/TMP/GP
注册账号 论坛帮助 会员列表 日历事件 搜索 今日新帖 标记版面已读

回复
 
LinkBack 主题工具 显示模式
  #1 (permalink)  
旧 2007-11-15
初级会员
 
注册日期: 2007-04-14
帖子: 22
c++primer 正向着好的方向发展
眨眼 一个小问题,作为开胃菜!

#include<iostream>
using namespace std;
int main()
{
int temp;
cout<<"please input a number:"<<endl;
cin>>temp;
switch(cin){
default:
cout<<"the number is not 1 or 2"<<endll;
case 1:
cout<<"the number is 1 "<<endll;
case 2:
cout<<"the number is 2 "<<endll;
}
}
如果输入1的话,结果是the number is 1 the number is 2
如果输入3的话,结果是 the number is not 1 or 2 the number is 1 the number is 2

用断点跟踪了下
输入1的话,从case 1:执行
输入3的话从default:开始执行?
为什么??????
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #2 (permalink)  
旧 2007-11-17
普通会员
 
注册日期: 2006-04-17
帖子: 58
ltqin 正向着好的方向发展
默认 回复: 一个小问题,作为开胃菜!

原来default还可以写在前面啊,我还以为会编译错误呢!汇编是这样嘀:
代码:
switch(temp) 00411509 mov eax,dword ptr [temp] 0041150C mov dword ptr [ebp-0DCh],eax 00411512 cmp dword ptr [ebp-0DCh],1 00411519 je wmain+0AFh (41154Fh) 0041151B cmp dword ptr [ebp-0DCh],2 00411522 je wmain+0DAh (41157Ah) { default: cout<<"the number is not 1 or 2"<<endl; 00411524 mov esi,esp 00411526 mov eax,dword ptr [__imp_std::endl (41A308h)] 0041152B push eax 0041152C push offset string "the number is not 1 or 2" (417724h) 00411531 mov ecx,dword ptr [__imp_std::cout (41A304h)] 00411537 push ecx 00411538 call std::operator<<<std::char_traits<char> > (41115Eh) 0041153D add esp,8 00411540 mov ecx,eax 00411542 call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (41A300h)] 00411548 cmp esi,esp 0041154A call @ILT+415(__RTC_CheckEsp) (4111A4h) case 1: cout<<"the number is 1 "<<endl; 0041154F mov esi,esp 00411551 mov eax,dword ptr [__imp_std::endl (41A308h)] 00411556 push eax 00411557 push offset string "the number is 1 " (417710h) 0041155C mov ecx,dword ptr [__imp_std::cout (41A304h)] 00411562 push ecx 00411563 call std::operator<<<std::char_traits<char> > (41115Eh) 00411568 add esp,8 0041156B mov ecx,eax 0041156D call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (41A300h)] 00411573 cmp esi,esp 00411575 call @ILT+415(__RTC_CheckEsp) (4111A4h) case 2: cout<<"the number is 2 "<<endl; 0041157A mov esi,esp 0041157C mov eax,dword ptr [__imp_std::endl (41A308h)]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #3 (permalink)  
旧 2007-11-18
初级会员
 
注册日期: 2007-04-14
帖子: 22
c++primer 正向着好的方向发展
默认 回复: 一个小问题,作为开胃菜!

楼上的大哥
能解释下么??
有点看不明白~~
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #4 (permalink)  
旧 2007-11-23
初级会员
 
注册日期: 2007-11-23
帖子: 1
yuff100 正向着好的方向发展
默认 回复: 一个小问题,作为开胃菜!

当然了,不是1、2的,当然就是default了。一旦进入了switch case block,除非遇到break、return、goto等语句,否则代码一直执行到block结束为止。
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
回复

书签

主题工具
显示模式

发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛启用 表情符号
论坛启用 [IMG] 代码
论坛禁用 HTML 代码
Trackbacks are 启用
Pingbacks are 启用
Refbacks are 启用



所有时间均为格林尼治时间 +9。现在的时间是 07:16 AM


Powered by vBulletin® 版本 3.7.0
版权所有 ©2000 - 2008,Jelsoft Enterprises Ltd.
(C) Copy Right All Right Reserved 2001 - 2007

Search Engine Friendly URLs by vBSEO 3.1.0