Loops

Loops
A loop is a programmers way of repeating the same thing over and over again. There are three main types of loops; while, do-while, and for loops. A while loop accepts a bool value that it tests before every repetition. A do-while loop also accepts a bool value that it tests after every repetition which causes it to loop at-least once. A for loop has three parts; initialization, bool test, and the increment. The for loop continues until the bool test is false and calls the increment at the end of every loop.

In Java
code format="java" public class Main{ public static void main(String[] args){ //while loop int var = 5; while(var > 0){ System.out.print(var + " "); var--; }

//do-while loop boolean go = false; do{ System.out.print("\ngo\n"); }while(go);

//for loop for(int x = 0/*init*/; x<5/*test*/; x++/*increment*/){ System.out.print(x + " "); }   } } code code Output: 5 4 3 2 1 go 0 1 2 3 4 code

In C++
code format="cpp"
 * 1) include

int main{ //while loop int var = 5; while(var > 0){ printf("%d ", var); var--; }

//do-while loop boolean go = false; do{ printf("\ngo\n"); }while(go);

//for loop for(int x = 0/*init*/; x<5/*test*/; x++/*increment*/){ printf("%d ", x); }

return 0; } code code Output: 5 4 3 2 1 go 0 1 2 3 4 code