Tutorial dan Contoh goto Statement Pemrograman C+

Tutorial dan Contoh goto Statement Pemrograman C+

Selamat datang kembali di TeknoTips.NET. TeknoTips.NET adalah media online Indonesia yang diluncurkan pada bulan Maret 2015. Kami berkomitmen menyajikan konten seputar teknologi yang berkualitas dan bermanfaat. Ulasan kami terdiri dari berbagai topik: pemrograman komputer & database, review produk, tutorial windows, linux, android, kumpulan source code & project program, referensi tugas akhir & skripsi, download ebook, game & software.

Pada kesempatan ini kita akan mengulas artikel yang berjudul : Tutorial dan Contoh goto Statement Pemrograman C+.

In C++ programming, goto statement is used for altering the normal sequence of program execution by transferring control to some other part of the program.

Syntax of goto Statement

goto label;
... .. ...
... .. ...
... .. ...
label: 
statement;
... .. ...
In syntax above, label is an identifier. When goto label; is encountered, the control of program jumps to label: and executes the code below it.
C++ Flow Control, C++ if else, C++ for Loop, C++ do while Loop, C++ break & continue, C++ switch Statement, C++ goto Statement

Example 1: goto Statement

/*  C++ program to demonstrate the working of goto statement. */
/* This program calculates the average of numbers entered by user. */
/* If user enters negative number, it ignores that number and 
   calculates the average of number entered before it.*/
# include <iostream>
using namespace std;
int main() {
    float num, average, sum = 0.0;
    int i, n;
    cout<<"Maximum number of inputs: ";
    cin>>n;

    for(i=1; i <= n; ++i) {
        cout<<"Enter n"<<i<<": ";
        cin>>num;
        
        if(num < 0.0) {
            goto jump;  /* Control of the program moves to jump; */
        } 
        sum += num;
    }
    
jump:
    average=sum/(i-1);
    cout<<"\nAverage = "<<average;
    return 0;
}
Output
Maximum number of inputs: 10
Enter n1: 2.3
Enter n2: 5.6
Enter n3: -5.6

Average = 3.95
You can write any C++ program with use of goto statement and it is generally considered good idea not to use goto statement.

Reason to Avoid goto Statement

The goto statement gives power to jump to any part of program but, makes the logic of the program complex and tangled. In modern programming, goto statement is considered a harmful construct and a bad programming practice.
The goto statement can be replaced in most of C++ program with the use of break and continue statements.

Tag : C++ Flow Control, C++ if else, C++ for Loop, C++ do while Loop, C++ break & continue, C++ switch Statement, C++ goto Statement
Untuk mendapat notifikasi setiap artikel terbaru, masukkan e-mail anda disini
Selanjutnya cek e-mail anda untuk verifikasi.

Silahkan tuliskan komentar relevan Anda dengan jelas dan sopan, sesuai dengan topik postingan tentang ", " pada halaman dibawah ini.

0 Response to "Tutorial dan Contoh goto Statement Pemrograman C+"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2


Iklan Bawah Artikel