回 帖 发 新 帖 刷新版面

主题:再请教一个问题cjcse或其他大虾.

   可以告诉我goto怎么用吗?我没查到!!!

回复列表 (共6个回复)

沙发

jmp是汇编中的无条件跳转。

板凳

C中的break是不是也属于无条件跳转呀!我有点忘记了,不过现在认为是.

意思大致是这样的:编译是遇见break语句的话,立即结束,转而执行下面的语句.

可能不准确或不对,大家来谈一谈!

3 楼

汇编中的jmp的用法基本等同于c中的goto语句(c++中不建议使用goto),与c/c++的break却不相同,break是强行退出与之最近的一层循环,相对应的还有一个continue,continue是跳过当前迭代,并不退出循环,而是转到下一次迭代。在c、c++中还有一个与之相关的语句就是exit(),它是退出整个程序,有人把它称为不负责的退出。应该清楚了吧!

4 楼

如果有程序有以下层次:
while(..)
{switch(...)
{case ...:
   ...........;break;
  case ...:
   if (...){...}
   else{...
         if(...) {...}
         else {...}
   break;}
  ..........(最后一段代码);
}
请问应该用什么语句使程序从最后一个if语句跳出,执行(最后一段代码)?   

5 楼

 
你的(……(最后一段代码);)是在switch(){}里面还是外面啊!如果是里面,只要把最后一个break移到最后一段代码之后就行了!如果是在while里面,并且在switch外面,那么最后一段代码无论如何都会执行。
简单例子:
#include<iostream>
using namespace std;
main()
{
int i;
int count=0;

while(count<3)
{
cin>>i;
switch(i)
{
case 1:
    cout<<"the i is 1!"<<endl;
    break;
case 2:
    cout<<"the i is 2!"<<endl;
    break;
default:
    if(i==3)cout<<"the i is 3!"<<endl;
    else cout<<"illegal input!"<<endl;
    
    break;
}
   
cout<<"the last sentence!"<<endl;
         count++;
}
return 0;
}

6 楼

至于goto的用法,虽然是不建议使用,但是还是说一下吧!
goto 说白了就是转到哪,最简单的就是程序转到哪一行开始运行,或者是什么标号!
还是举一个例子,大家一看就明白了:
#include<iostream>
using namespace std;
main()
{
     int gotoSelect;
     cout<<"input the select(from 1 to 3):"<<endl;
     cin>>gotoSelect;
     if(gotoSelect==1)goto line1;
     if(gotoSelect==2)goto line2;
     else goto line3;

line1:
    cout<<"the line of line1!"<<endl;
    goto lastline;

line2:
    cout<<"the line of line2!"<<endl;
    goto lastline;

line3:
    cout<<"the line of line3!"<<endl;

lastline:
    cout<<"the last line!"<<endl;
    return 0;
}

不明白的建议运行一下,就很简单了。

我来回复

您尚未登录,请登录后再回复。点此登录或注册