goto statement in C++

 The goto statement in C++ is used to jump to a labeled section within the same function. It is generally not recommended in modern programming because it makes code harder to read and debug. However, for educational purposes, here are some examples using goto.


🛠️ Goto Statement in C++

Syntax:

goto label;
...
label: 
   // Code to execute
  • goto label; → Moves execution to the specified label:
  • Avoid overusing goto, as it makes debugging difficult.

1️⃣ Factorial using Goto in C++

📌 Example: Compute the factorial of a number using goto.

#include <iostream>
using namespace std;

int main() {
    int num, fact = 1, i = 1;
    cout << "Enter a number: ";
    cin >> num;

    factorial:
    if (i <= num) {
        fact *= i;
        i++;
        goto factorial;
    }

    cout << "Factorial of " << num << " is " << fact << endl;
    return 0;
}

🔹 Output (if input is 5)

Factorial of 5 is 120

2️⃣ Printing Even Numbers using Goto

📌 Example: Print even numbers from 1 to 10.

#include <iostream>
using namespace std;

int main() {
    int num = 2;

    even:
    if (num <= 10) {
        cout << num << " ";
        num += 2;
        goto even;
    }

    return 0;
}

🔹 Output

2 4 6 8 10

3️⃣ Printing a Given Word using Goto

📌 Example: Print a word n times.

#include <iostream>
using namespace std;

int main() {
    int count, i = 1;
    cout << "Enter how many times to print 'Hello': ";
    cin >> count;

    print_hello:
    if (i <= count) {
        cout << "Hello ";
        i++;
        goto print_hello;
    }

    return 0;
}

🔹 Output (if input is 3)

Hello Hello Hello

4️⃣ Printing Numbers using Goto

📌 Example: Print numbers from 1 to 10.

#include <iostream>
using namespace std;

int main() {
    int num = 1;

    print_number:
    if (num <= 10) {
        cout << num << " ";
        num++;
        goto print_number;
    }

    return 0;
}

🔹 Output

1 2 3 4 5 6 7 8 9 10

5️⃣ Printing Odd Numbers using Goto

📌 Example: Print odd numbers from 1 to 10.

#include <iostream>
using namespace std;

int main() {
    int num = 1;

    odd:
    if (num <= 10) {
        cout << num << " ";
        num += 2;
        goto odd;
    }

    return 0;
}

🔹 Output

1 3 5 7 9

6️⃣ Sum of Numbers using Goto

📌 Example: Find the sum of numbers from 1 to n.

#include <iostream>
using namespace std;

int main() {
    int num, sum = 0, i = 1;
    cout << "Enter a number: ";
    cin >> num;

    add_numbers:
    if (i <= num) {
        sum += i;
        i++;
        goto add_numbers;
    }

    cout << "Sum of first " << num << " numbers is " << sum << endl;
    return 0;
}

🔹 Output (if input is 5)

Sum of first 5 numbers is 15

7️⃣ Print Tables using Goto

📌 Example: Print a multiplication table for a given number.

#include <iostream>
using namespace std;

int main() {
    int num, i = 1;
    cout << "Enter a number: ";
    cin >> num;

    table:
    if (i <= 10) {
        cout << num << " x " << i << " = " << num * i << endl;
        i++;
        goto table;
    }

    return 0;
}

🔹 Output (if input is 3)

3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
...
3 x 10 = 30

8️⃣ Print Reverse Tables using Goto

📌 Example: Print a multiplication table in reverse order.

#include <iostream>
using namespace std;

int main() {
    int num, i = 10;
    cout << "Enter a number: ";
    cin >> num;

    rev_table:
    if (i >= 1) {
        cout << num << " x " << i << " = " << num * i << endl;
        i--;
        goto rev_table;
    }

    return 0;
}

🔹 Output (if input is 3)

3 x 10 = 30
3 x 9 = 27
3 x 8 = 24
...
3 x 1 = 3

📌 Summary

goto jumps to labeled sections within a function.
✔ It can replace loops, but isn't recommended due to poor readability.
✔ Use for, while, or do-while instead of goto whenever possible.

Would you like examples using loops instead of goto? 🚀

Post a Comment

0 Comments