LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 148|回复: 0

控制的转移

[复制链接]
发表于 2024-1-30 23:36:39 | 显示全部楼层 |阅读模式
可以在 switch 语句中使用 goto 语句或 case 标签来指定分支超出初始值设定项的程序。 此类代码是非法的,除非包含初始值设定项的声明在跳转语句发生的块所封闭的块中。

下面的示例显示了声明和初始化对象 total、ch 和 i 的循环。 也存在将控制权传递过初始值设定项的错误 goto 语句。

// transfers_of_control.cpp
// compile with: /W1
// Read input until a nonnumeric character is entered.
int main()
{
   char MyArray[5] = {'2','2','a','c'};
   int i = 0;
   while( 1 )
   {
      int total = 0;

      char ch = MyArray[i++];

      if ( ch >= '0' && ch <= '9' )
      {
         goto Label1;

         int i = ch - '0';
      Label1:
         total += i;   // C4700: transfers past initialization of i.
      } // i would be destroyed here if  goto error were not present
   else
      // Break statement transfers control out of loop,
      //  destroying total and ch.
      break;
   }
}
在前面的示例中,goto 语句尝试将控制权传递过 i 的初始化。 但是,如果已声明但未初始化 i,则该传递是合法的。

在用作 while 语句的 statement 的块中声明的对象 total 和 ch 在使用 break 语句退出此块时将被销毁。

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表