| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3145 人关注过本帖
标题:用goto写九九乘法表??
只看楼主 加入收藏
whykyxht
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2008-3-22
收藏
得分:0 
回复关于goto语句
goto语句改变了程序正常的流程,建议不要用,其实能用goto语句做出来的,用循环和选择结构也通常能做出来
2008-03-22 20:09
蓝色神话
Rank: 2
等 级:论坛游民
威 望:1
帖 子:404
专家分:24
注 册:2006-5-11
收藏
得分:0 
[quote][bo]以下是引用 [un]qfyzy[/un] 在 2008-3-22 19:30 的发言:[/bo]


c的实现者曾考虑过去掉goto。
这句话的原文出自哪里,给出来!

[[it] 本帖最后由 蓝色神话 于 2008-3-23 10:11 编辑 [/it]]
2008-03-23 10:07
qfyzy
Rank: 2
等 级:论坛游民
威 望:1
帖 子:380
专家分:86
注 册:2008-2-17
收藏
得分:0 
C provides the infinitely-abusable goto statement, and labels to branch to. Formally, /*注意*/the goto statement is never necessary, and in practice it is almost always easy to write code without it. We have not used goto in this book.
Nevertheless, there are a few situations where gotos may find a place. The most common is to abandon processing in some deeply nested structure, such as breaking out of two or more loops at once. The break statement cannot be used directly since it only exits from the innermost loop. Thus:

       for ( ... )
           for ( ... ) {
               ...
               if (disaster)
                   goto error;
           }
       ...
   error:
       /* clean up the mess */

This organization is handy if the error-handling code is non-trivial, and if errors can occur in several places.
A label has the same form as a variable name, and is followed by a colon. It can be attached to any statement in the same function as the goto. The scope of a label is the entire function.

As another example, consider the problem of determining whether two arrays a and b have an element in common. One possibility is

       for (i = 0; i < n; i++)
           for (j = 0; j < m; j++)
               if (a[i] == b[j])
                   goto found;
       /* didn't find any common element */
       ...
   found:
       /* got one: a[i] == b[j] */
       ...

Code involving a goto can always be written without one, though perhaps at the price of some repeated tests or an extra variable. For example, the array search becomes
   found = 0;
   for (i = 0; i < n && !found; i++)
       for (j = 0; j < m && !found; j++)
           if (a[i] == b[j])
               found = 1;
   if (found)
       /* got one: a[i-1] == b[j-1] */
       ...
   else
       /* didn't find any common element */
       ...

/*注意*/With a few exceptions like those cited here, code that relies on goto statements is generally harder to understand and to maintain than code without gotos. Although we are not dogmatic about the matter, it does seem that goto statements should be used rarely, if at all.

[[it] 本帖最后由 qfyzy 于 2008-3-23 11:33 编辑 [/it]]

当对C的经验增加时,它会显的很好用.----Dennis M Ritche如是说
2008-03-23 11:32
einstein2178
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2008-3-23
收藏
得分:0 
才开始学C 语言,试了一下4楼大哥的程序好像有点问题,还希望各位大哥多多指教。
2008-03-23 12:04
qifaman
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2008-3-22
收藏
得分:0 
同意大家的意见啊,GOTO我也很少用,看起来思路也不是特别的清晰哦,看多了头疼。。
写个没有GOTO的出来给大家浏览吧
稍微改一下给定的值可以得出不单单是99乘法了:
#include "stdio.h"
#define N 9
main()
{
      int i,j,sum;
      printf("\n");
      for(i=1;i<=N;i++)
      {
                       for(j=1;j<=i;j++)
                       {
                                        sum=i*j;
                                        printf("%d*%d=%-3d",i,j,sum);
                       }
                       printf("\n");
      }
      system("pause");
      return 0;
}

编译器I用:DEV_C++
2008-03-23 14:30
蓝色神话
Rank: 2
等 级:论坛游民
威 望:1
帖 子:404
专家分:24
注 册:2006-5-11
收藏
得分:0 
[bo]以下是引用 [un]qfyzy[/un] 在 2008-3-23 11:32 的发言:[/bo]

C provides the infinitely-abusable goto statement, and labels to branch to. Formally, /*注意*/the goto statement is never necessary, and in practice it is almost always easy to write code without it.  ...



这个例子中的嵌套也只是两个,如果更多呢?那你岂不是要设很多类似flag1,flag2,flag3,......这样的标志,到时你的逻辑判断岂不很多,那不是比使用goto更容易出错吗?另外使用goto不一定就会带来维护上的麻烦,在反汇编出来的代码中,你可以看到到处都有类似goto的jmp语句,那怎么说呢?

[[it] 本帖最后由 蓝色神话 于 2008-3-23 15:06 编辑 [/it]]
2008-03-23 15:04
qfyzy
Rank: 2
等 级:论坛游民
威 望:1
帖 子:380
专家分:86
注 册:2008-2-17
收藏
得分:0 
回复 16# 的帖子
我看你还是歇着把,写程序本来就是自己的事。你愿意用就用,但你不能鼓励别人去用。“这个例子中的嵌套也只是两个”,如果你愿意把程序写的全是多重的循环,又不肯优化算法,就算你用longjmp(非局部转跳),也救不了你。
况且,谁告诉你asm里不能用jmp的

当对C的经验增加时,它会显的很好用.----Dennis M Ritche如是说
2008-03-23 17:49
蓝色神话
Rank: 2
等 级:论坛游民
威 望:1
帖 子:404
专家分:24
注 册:2006-5-11
收藏
得分:0 
[bo]以下是引用 [un]qfyzy[/un] 在 2008-3-23 17:49 的发言:[/bo]

我看你还是歇着把,写程序本来就是自己的事。你愿意用就用,但你不能鼓励别人去用。“这个例子中的嵌套也只是两个”,如果你愿意把程序写的全是多重的循环,又不肯优化算法,就算你用longjmp(非局部转跳),也救不了你。
况且,谁告 ...

不要张口一个算法,闭口一个算法,你究竟懂多少算法呢?你要是算法NB,就去参加ICPC,拿个final的Top one看看!你让我不鼓励别人使用GOTO,那你怎么唆使别人不用呢?你还不是看书上说不用的你才让别人不用的,要是书上说用,你还不是让别人用!人云亦云,矮人看戏,没有自己的主见,所以你以后也只能是个coder!
2008-03-23 19:11
hyq1122
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2008-3-24
收藏
得分:0 
GOTO是可以用的
但是要注意一点,如果你从多重循环中直接跳出,有可能会导致计数器不正确从而影响代码的正常运行,所以,简单的语句可以用GOTO,多重循环尽量避免!
2008-03-25 10:31
蓝色神话
Rank: 2
等 级:论坛游民
威 望:1
帖 子:404
专家分:24
注 册:2006-5-11
收藏
得分:0 
[bo]以下是引用 [un]hyq1122[/un] 在 2008-3-25 10:31 的发言:[/bo]

但是要注意一点,如果你从多重循环中直接跳出,有可能会导致计数器不正确从而影响代码的正常运行,所以,简单的语句可以用GOTO,多重循环尽量避免! ...

你没有理解我的意思,当你需要从多重循环中跳出来时(比如找到了符合条件的值从而不需要在多重循环中继续进行匹配了),使用GOTO是一种比较好的选择!
2008-03-25 19:11
快速回复:用goto写九九乘法表??
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016359 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved