pengulangan pada c++ terbagi mejadi 3 jenis.
1. For statement
2. While statement
3. Do while statement
boka codeblock anda dan berikut saya beri contoh penggunaan statment masing2 looping.
For Statment
#include <iostream>
using namespace std;
int main()
{
string nama;
cout<<"contoh for looping\n";
cout<<"\nmasukkan nama ";
cin>>nama;
for (int i=1; i <= 5; i++){
cout<<nama <<i <<" (for loop)" <<endl;
}
}
While Statment
#include <iostream>
using namespace std;
int main()
{
string nama;
cout<<"contoh for while\n";
cout<<"\nmasukkan nama ";
cin>>nama;
int i=1;
while(i<=5){
cout<<endl<<i <<". ";
cout<<nama <<" (while looping)";
i++;
}
int j=1;
}
do While Statment
#include <iostream>
using namespace std;
int main()
{
string nama;
cout<<"contoh for do while\n";
cout<<"\nmasukkan nama ";
cin>>nama;
int j=1;
do{
cout<<endl<<j <<". ";
cout<<nama <<" (do while looping)";
j++;
}
while (j<=5);
}
0 comments:
Post a Comment