pranay_teja
10/28/2018 - 7:07 AM

CPP-Loops_Syntax

#include<bits/stdc++.h>
using namespace std;

// #CPP_STL

int main(){
    
//==============================
// if iterator manipulations are required mid loop using while loop is easy to keep track of iterator
// break -> breaks out of current loop
// continue -> skips remaining part of current loop and goes to top of current loop

    int i=0;
    while(i<=10){
        if(i==7){   // 7 is skipped
            i++;
            continue;   // continue skips the remaining part and goes to top of loop
        }
        if(i==2){   // i=2 -> i=2*2=4
            i*=2;   // i manipulated in mid-loop
            continue;
        }
        cout<<i<<" ";
        i++;
    }
    cout<<endl;
    
//=====================================
// iterator manipulations are confusing in for loop as i++ is automatic for each iteration
    for(int i=0;i<=10;i++){
        if(i==7){
            continue;
        }
        if(i==2){
            i*=2;   // i=2 -> i=2*2=4,
            i--;   // but i++ is automatic, so additional i--
            continue;
        }
        cout<<i<<" ";
    }
    
//=====================( "for each" in loop )==============================
	vector<int> a={0,1,2,3}
	for(int z:a){	// for each a[i]
	    cout<<z<<" ";
	}
	cout<<endl;

    return 0;
}