You are on page 1of 4

#include<iostream> using namespace std; void main() { //loop allows the program to run several times //loop with

counter int i=0; while(i<10) { //by default only one line //belongs to the loop cout<<"Loop"<<endl; //without changing i - it's infinite loop i++;//same i=i+1; } } #include<iostream> #include<string> using namespace std; void main() { string ans=""; while(ans!="no") { cout<<"loop"<<endl;

cout<<"Another loop?"<<endl; cin>>ans; } } #include<iostream> #include<string> using namespace std; void main() { int choice=0;//var is declared //outside the loop while(choice!=2) { cout<<"1. to do something"<<endl; cout<<"2. exit"<<endl; cin>>choice; if(choice==1) cout<<"do something"<<endl; if(choice==2) cout<<"goodbye"<<endl; } }

#include<iostream>

using namespace std; void main() { int i=10; //main difference between while loop //and do while loop //do while runs at least once (no matter what //condition you have do { cout<<"loop"; i++; }while(i<10); }

#include<iostream> using namespace std; //the program will output rectangle //of stars (10 by 10) void main() { //nested while - one loop inside the other //you also can do nested do while int i=0;//you have to put //initial condition to be sure that //compiler will go inside the loop

//i loop will count how many rows //in the rectangle //outer loop while(i<10)//; after the loop //means that compiler will check condition //and that's it { //for nested loop you //have to have 2 counters //j loop - how many stars in a row int j=0; while(j<10)//inner loop { cout<<"*"; j++; } cout<<endl; i++; } }

You might also like