天天看點

結構化程式goto語句_C ++ goto語句| 查找輸出程式| 套裝2

結構化程式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語句