结构化程序goto语句

Program 1:

程序1:

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int num1 = 1;
int num2 = 0;
MY_LABEL:
num2 = num1 * num1;
cout << num2 << " ";
num1 = num1 + pow(2, 0);
if (num1 <= 5)
goto MY_LABEL;
return 0;
}

Output:

输出:

1 4 9 16 25

Explanation:

说明:

The above code will print "1 4 9 16 25" on the console screen.

上面的代码将在控制台屏幕上显示“ 1 4 9 16 25”

Understand the program step by step.

逐步了解程序。

Here we initialize the variables num1 and num2 by 1, 0 respectively.  And we created a label MY_LABEL.

在这里,我们分别将变量num1num2初始化为1、0。 然后我们创建了标签MY_LABEL

Now evaluate statements iteration wise:

现在,明智地评估语句迭代:

First iteration:

第一次迭代:

num1=1, num2=0
num2 = num1 * num1;
Then num1 = 1 and num2 = 1 and then print num2 that is 1.
As we know that if we calculate the
zero power of any number that will be 1.
num1 = num1 + pow(2,0);
Then the above statement will increase the value of num1 by one.
Then num1 become 2.
And num1 is less than equal to 5 then "goto" statement
will transfer the program control to the MY_LABEL.

Second iteration:

第二次迭代:

num1=2, num2=0
num2 = num1 * num1;
After execution of above statement,
num1=2 and num2=4 and then print num2 that is 4
num1 = num1 + pow(2,0);
Then above statement will increase the value of num1 by one.
Then num1 become 3.
And num1 is less then equal to 5 then "goto” statement
will transfer the program control to the MY_LABEL.

Third Generation:

第三代:

num1=3, num2=0
num2 = num1 * num1;
After execution of above statement,
num1=3 and num2=9 and then print num2 that is 9
num1 = num1 + pow(2,0);
Then above statement will increase the value of num1 by one.
Then num1 become 4.
And num1 is less then equal to 5 then "goto" statement
will transfer the program control to the MY_LABEL.

Fourth iteration:

第四次迭代:

num1=3, num2=0
num2 = num1 * num1;
After execution of above statement,
num1=4 and num2=16 and then print num2 that is 16
num1 = num1 + pow(2,0);
Then above statement will increase the value of num1 by one.
Then num1 become 5.
And num1 is less then equal to 5 then "goto" statement
will transfer the program control to the MY_LABEL.

Fifth iteration:

第五次迭代:

num1=3, num2=0
num2 = num1 * num1;
After execution of above statement,
num1=5 and num2=25 and then print num2 that is 25
num1 = num1 + pow(2,0);
Then above statement will increase the value of num1 by one.
Then num1 become 6.
Then condition get false and program get terminated.

Program 2:

程式2:

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int num1 = 1;
int num2 = 0;
int num3 = 1;
MY_LABEL:
num3 = num3 + num2;
cout << num3 << " ";
num2 = num2 + 1;
num1 = num1 + 1;
if (num1 <= 4)
goto MY_LABEL;
return 0;
}

Output:

输出:

1 2 4 7

Explanation:

说明:

The above code will print "1 2 4 7" on the console screen.

上面的代码将在控制台屏幕上显示“ 1 2 4 7”

Understand the program step by step.

逐步了解程序。

Here we initialize the variables num1, num2, and num3 by 1, 0, and 1 respectively.  And we created a label MY_LABEL.

在这里,我们分别将变量num1num2num3分别初始化为1、0和1。 然后我们创建了标签MY_LABEL

Now evaluate statements iteration wise:

现在,明智地评估语句迭代:

First iteration:

第一次迭代:

Here initial values of num1=1, num2 =0, and num3 =1
after executing all statements "1" will be printed on
console screen and modified value will be:
num1 = 2, num2 = 1, num3= 1;
If the condition is true then program control
will be transferred to MY_LABEL.

Second iteration:

第二次迭代:

Here values of num1=2, num2 =1, and num3 =1
after executing all statements "2" will be printed on
console screen and modified value will be:
num1 = 3, num2 = 2, num3= 2;
If the condition is true then program control
will be transferred to MY_LABEL.

Third iteration:

第三次迭代:

Here values of num1=3, num2 =2, and num3 =2
after executing all statements "4" will be printed on
console screen and modified value will be:
num1 = 4, num2 = 3, num3= 4;
If the condition is true then program control
will be transferred to MY_LABEL.

Fourth iteration:

第四次迭代:

Here values of num1=4, num2 =3, and num3 =4
after executing all statements "7” will be printed on
console screen and modified value will be:
num1 = 4, num2 = 4, num3= 7;
Now the if condition gets false and the program will terminate.

Recommended posts

推荐的帖子

  • C++ goto Statement | Find output programs | Set 2

    C ++ goto语句| 查找输出程序| 套装2

  • C++ Operators | Find output programs | Set 1

    C ++运算符| 查找输出程序| 套装1

  • C++ Operators | Find output programs | Set 2

    C ++运算符| 查找输出程序| 套装2

  • C++ const Keyword | Find output programs | Set 1

    C ++ const关键字| 查找输出程序| 套装1

  • C++ const Keyword | Find output programs | Set 2

    C ++ const关键字| 查找输出程序| 套装2

  • C++ Reference Variable| Find output programs | Set 1

    C ++参考变量| 查找输出程序| 套装1

  • C++ Reference Variable| Find output programs | Set 2

    C ++参考变量| 查找输出程序| 套装2

  • C++ Conditional Statements | Find output programs | Set 1

    C ++条件语句| 查找输出程序| 套装1

  • C++ Conditional Statements | Find output programs | Set 2

    C ++条件语句| 查找输出程序| 套装2

  • C++ Switch Statement | Find output programs | Set 1

    C ++转换语句| 查找输出程序| 套装1

  • C++ Switch Statement | Find output programs | Set 2

    C ++转换语句| 查找输出程序| 套装2

  • C++ Looping | Find output programs | Set 1

    C ++循环| 查找输出程序| 套装1

  • C++ Looping | Find output programs | Set 2

    C ++循环| 查找输出程序| 套装2

  • C++ Looping | Find output programs | Set 3

    C ++循环| 查找输出程序| 套装3

  • C++ Looping | Find output programs | Set 4

    C ++循环| 查找输出程序| 套装4

  • C++ Looping | Find output programs | Set 5

    C ++循环| 查找输出程序| 套装5

翻译自: https://www.includehelp.com/cpp-tutorial/goto-statement-find-output-programs-set-1.aspx

结构化程序goto语句

结构化程序goto语句_C ++ goto语句| 查找输出程序| 套装1相关推荐

  1. g++默认参数_C ++默认参数| 查找输出程序| 套装2

    g++默认参数 Program 1: 程序1: #include <iostream> using namespace std; int K = 10; int fun() { retur ...

  2. g++默认参数_C ++默认参数| 查找输出程序| 套装1

    g++默认参数 Program 1: 程序1: #include <iostream> using namespace std; int sum(int X, int Y = 20, in ...

  3. c语言指针++_C ++此指针| 查找输出程序| 套装1

    c语言指针++ Program 1: 程序1: #include <iostream> using namespace std; int main() { int A = 10; this ...

  4. c ++查找字符串_C ++朋友功能| 查找输出程序| 套装1

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; class Sample { int A, B; fr ...

  5. c ++查找字符串_C ++朋友功能| 查找输出程序| 套装2

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; class Sample1 { int A, B; f ...

  6. c语言指针++_C ++此指针| 查找输出程序| 套装3

    c语言指针++ Program 1: 程序1: #include <iostream> using namespace std; class Test { int VAL; public: ...

  7. 输入输出数组元素的函数重载_C ++函数重载| 查找输出程序| 套装3

    输入输出数组元素的函数重载 Program 1: 程序1: #include <iostream> using namespace std; class Test { public: vo ...

  8. c++重载++运算符_C ++运算符重载| 查找输出程序| 套装3

    c++重载++运算符 Program 1: 程序1: #include <iostream> using namespace std; class Test { public: int A ...

  9. 小程序 || 语句_C ++开关语句| 查找输出程序| 套装1

    小程序 || 语句 Program 1: 程序1: #include <iostream> using namespace std; int main() {switch (printf( ...

最新文章

  1. 【matlab】2019.5.10第一节上机课练习
  2. 初创企业股权架构_初创企业如何以每月不到200美元的价格利用生产级基础架构...
  3. 电路交换-报文交换-分组交换- 分组交换包括:虚电路和数据报
  4. 阿里大佬告诉你,支付宝的架构到底有多牛逼!还没看完我就跪了!
  5. 连接池技术 Connection Pooling
  6. 重构:一项常常被忽略的基本功
  7. 无效0_12位浙江高考生成绩被教育考试院判定无效,0分收场的原因很可惜
  8. 计算机cnc键代表啥,计算器上的cnc键表示啥
  9. JBoss AS 7.0.2“ Arc”发布–使用绑定选项
  10. Kubernetes 稳定性保障手册(极简版)
  11. 使⽤用 Spring Boot Actuator 监控应⽤
  12. python字符串转整数_Python连接字符串和整数
  13. Activities 四大组件之一
  14. Android POPWindow
  15. [硬件选型] 工业相机之参数和选型
  16. Unity_Demo | 中世纪风3D-RPG游戏
  17. 支持中文编程、汉语编程的国产C语言编程工具 - 习语言4717版发布
  18. Ubuntu下修改键盘排列
  19. 在浏览器上打开、预览Excel xlsx表格文件
  20. 学习笔记 | SMART原则:制定目标

热门文章

  1. oracle 实例死掉,Oracle 监听莫名死掉
  2. Linux下的ELF文件、链接、加载与库(含大量图文解析及例程)
  3. PyTorch 分布式训练DDP 单机多卡快速上手
  4. python循环嵌套的外循环必须完全包含内循环_Python:循环与嵌套循环实现规律数列...
  5. linux几秒钟同步一次,Linux时间同步配置方法
  6. 照片打印预览正常打印空白_小米发布口袋照片打印机,可无墨打印3寸背胶照片...
  7. linux产生随机数方法
  8. 【Day41】Python之路——AJAX
  9. mysql shell
  10. centos不能挂在ntfs