结构化程序goto语句
Program 1: 程序1:#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int num = 5;
int count = 1;
MY_LABEL:
cout << count * num << " ";
count++;
if (count <= 10)
goto MY_LABEL;
return 0;
}
Output: 输出: 5 10 15 20 25 30 35 40 45 50
Explanation: 说明: The above code will print a table of 5 on the console screen. Here num is initialized with 5 and count is initialized with 1, count variable will increase till 10 and multiple by number in each iteration then it will print complete table of 5.
上面的代码将在控制台屏幕上打印5表。 这里NUM被初始化为5和计数被初始化为1, 计数变量将在每次迭代增加直到10和多个通过编号,然后将打印的5完整表。
Program 2: 程式2:#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int num = 5;
int result = 1;
MY_LABEL:
result = num * result;
num--;
if (num > 0)
goto MY_LABEL;
cout << result;
return 0;
}
Output: 输出: 120
Explanation: 说明: The above code will print factorial of 5, here we initialize variable num by 5, and decrease the variable num from 5 to 1, Let's understand the flow,
上面的代码将输出阶乘为5,这里我们将变量num初始化为5,并将变量num从5减少到1,让我们了解流程,
num = 5, result=1 then
result = 5*1 that is 5.
num = 4, result=5 then
result = 4*5 that is 20.
num = 3, result=20 then
result = 3*20 that is 60.
num = 2, result=60 then
result = 2*60 that is 120
num = 1, result=120 then
result = 1*120 that is 120
And then the condition will be false
and then print result that is 120.
Program 3: 程式3: #include <iostream>
#include <math.h>
using namespace std;
int main()
{
int num1 = 5;
int num2 = 3;
int result = 1;
MY_LABEL:
result = result * num1;
num2--;
if (num2 > 0)
goto MY_LABEL;
cout << result;
return 0;
}
Output: 输出: 125
Explanation: 说明: The above code will print "125". Here we are calculating power 3 of number 5 that is 125.
上面的代码将打印“ 125” 。 在这里,我们正在计算5的幂3,即125。
Let's understand the program, here we took three variables num1, num2, and result that are initialized with 5, 3, and 1 respectively.
让我们了解一下程序,这里我们使用了三个变量num1 , num2和result ,它们分别用5、3和1初始化。
num1 = 5 , num2= 3 and result 1
result = 1 * 5;
= 5;
Decrease num2 by 1.
Condition is true then program control will be
transferred at label MY_LABEL.
num1 = 5 , num2= 2 and result 5
result = 5 * 5;
= 25;
Decrease num2 by 1.
Condition is true then program control will be
transferred at label MY_LABEL.
num1 = 5 , num2= 1 and result 25
result = 25 * 5;
= 125;
Decrease num2 by 1.
The condition will be true and then print result
that is 125 on the console screen.
Recommended posts
推荐的帖子
-
C++ goto Statement | Find output programs | Set 1
C ++ goto语句| 查找输出程序| 套装1
-
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-2.aspx
结构化程序goto语句