What is Looping?

ahmadruslandia

Ahmad Ruslandia Papua

Posted on January 29, 2024

What is Looping?

Looping is a command that executes code repeatedly. Looping itself is divided into 2 types, namely Counted Loops and Uncounted Loops.

COUNTED LOOP

Counted Loop is a repetition where it is clearly known how many repetitions there are.

The repetition referred to as a counted loop is:

For
Example For Looping in C++ Language:

#include <iostream>

using namespace std;

int main() {

  for (int i = 0; i < 10; i++) {
    cout << "Perulangan For - " << i << endl;
  }

}
Enter fullscreen mode Exit fullscreen mode

actually it could also be included

While
Example While Looping in C++ Language:

#include <iostream>

using namespace std;

int main(){

int i = 0;

  while(i < 10){
    cout << "Perulangan While - " << i << endl;
    i++;
  }
}
Enter fullscreen mode Exit fullscreen mode

Do While
Example Do While Looping in C++ Language:

#include <iostream>

using namespace std;

int main() {

int i = 0;

  do {
    cout << "Perulangan Do While – " << i << endl;
    i++;
  } while (i < 10);
}

Enter fullscreen mode Exit fullscreen mode

UNCOUNTED LOOP

An Uncounted Loop is a repetition where it is not clear how many times it will repeat.

The repetition referred to as an uncounted loop is:

While
Example While Looping in C++ Language:

#include <iostream>

using namespace std;

int main(){

char ulang = 'y';
int i = 0;

  while(ulang == 'y'){
    cout << "\nMasukkan huruf y untuk contoh perulangan\n" << endl;
    cout << "Masukkan huruf : ";
    cin >> ulang;
    i++;
  }
  cout << "\nSelesai" << endl;
}
Enter fullscreen mode Exit fullscreen mode

Do While
Example Do While Looping in C++ Language:

#include <iostream>

using namespace std;

int main(){

char ulang = 'y';
int i = 0;

  do {
    cout << "\nMasukkan huruf y untuk contoh perulangan\n" << endl;
    cout << "Masukkan huruf : ";
    cin >> ulang;
    i++;
  } while(ulang == 'y');
  cout << "\nSelesai" << endl;
}
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
ahmadruslandia
Ahmad Ruslandia Papua

Posted on January 29, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related