Tutorial dan Contoh break dan continue Statement Pemrograman C++

Tutorial dan Contoh break dan continue 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 break dan continue Statement Pemrograman C++.

There are two statements (break; and continue;) built in C++ programming to alter the normal flow of program.
Loops are used to perform repetitive task until test expression is false but sometimes it is desirable to skip some statement/s inside loop or terminate the loop immediately with checking test condition. On these type of scenarios, continue; statement and break; statement is used respectively. The break; statement is also used to terminate switch statement.

C++ break Statement

The break; statement terminates the loop(for, while and do..while loop) and switch statement immediately when it appears.

Syntax of break

break;
In real practice, break statement is almost always used inside the body of conditional statement(if...else) inside the loop.

Working of break Statement


Example 1: C++ break

C++ program to add all number entered by user until user enters 0.
// C++ Program to demonstrate working of break statement
#include <iostream>
using namespace std;
int main() {
    float number, sum = 0.0;

    while (true) {     // test expression is always true
        cout<<"Enter a number: ";
        cin>>number;
        
        if (number != 0.0) {
            sum += number;
        }
        else {
            break;    // terminating the loop if number equals to 0.0
        }

    }
    cout<<"Sum = "<<sum;
    return 0;
}
Output

Enter a number: 4
Enter a number: 3.4
Enter a number: 6.7
Enter a number: -4.5
Enter a number: 0
Sum = 9.6
In this C++ program, the test expression is always true. The user is asked to enter a number which is stored in variable number. If the user enters any number other than 0, that number is added to sum and stored to it and again user is asked to enter another number. When user enters 0, the test expression inside if statement is false and body of else is executed which terminates the loop. Finally, the sum is displayed.

C++ continue Statement

It is sometimes necessary to skip some statement/s inside the loop. In that case, continue; statement is used in C++ programming.

Syntax of continue

continue;
In practice, continue; statement is almost always used inside conditional statement.

Working of continue Statement

Example 2: C++ continue

C++ program to display integer from 1 to 10 except 6 and 9.
// C++ Program to demonstrate working of continue statement
#include <iostream>
using namespace std;
int main() {
    for (int i = 1; i <= 10; ++i) {
        if ( i == 6 || i == 9) {
            continue;
        }
        cout<<i<<"\t";
    }
    return 0;
}
Output
1 2 3 4 5      7 8 10 
In above program, when i is 6 or 9, execution of statement cout<<i<<"\t"; is skipped inside the loop using continue; statement.

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 break dan continue Statement Pemrograman C++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2


Iklan Bawah Artikel