Loops in C++

Loops are probably one of the most important programming concepts in existence. There are so many applications of loops it would be impossible to list them all. To name a few though, things like parsing an array, trapping for errors, and animation. Also, since it will allow you to execute a block of code over and over, it saves time and typing. Now, depending on what kind of programming language you came from, loops might already be familiar to you, but if you’ve come from a low level programming language such as assembly or GWBasic, you are probably more familiar with jump or goto statements. Well, loops take into account all that lovely comparison crap you used to have to do before and puts it into one nice package.

C++ for Loop

The easiest and most used of these loops is the for loop.

for Loop Syntax

for ( variable initialization; condition; variable update ) {
Code to execute while the condition is true
}

for Loop Example

#include <iostream>
using namespace std;

int main() {

for ( int x = 0; x < 10; x++ ) {

cout<< x << “\n”;

}
return 0;

}

C++ while Loop

The while loop is almost exactly the same as the do loop except that its condition is tested at the start of the loop instead of at the end… and the format is slightly different. The easiest way to see this is simply to re-write the above example with a while loop.

while Loop Example

#include <iostream>

using namespace std;
int main()
{

int x = 0;

while ( x < 10 ) {

cout<< x << “\n”;
x++;

}

return 0;

}

C++ do.. while Loop

The do while loop is like the while loop except that the condition is tested at the bottom of the loop.

do.. while Loop Syntax

do {

statement(s);

} while (condition);

do.. while Loop Example

#include <iostream>

using namespace std;

int main()
{

int x;

x = 0;
do {

cout<<“Hello, world!\n”;

} while ( x != 0 );

return 0;

}

Loops in C

Loops are used to repeat one statement or set statements more than one time. Most real programs contain some construct that loops within the program, performing repetitive actions on a stream of data or a region of memory. There are several ways to loop in C.

for Loop

For loop is a counter loop. The for loop allows automatic initialization of instrumentation of a counter variable. The general form is

for (initialization; condition; increment/decrement)
{

statements block

}

If the statement block is only one statement, the braces are not necessary. Although the for allows a number of variations, generally the initialization is used to set a counter variable to its starting value. The condition is generally a relational statement that checks the counter variable against a termination value, and the increment increments (or decrements) the counter value. The loop repeats until the condition becomes false.

Example

main(){

int i;

for(i = 0; i < count; i++)
{

printf(“%d\n”,i);

}

}

while Loop

The while loop repeats a statement until the test at the top proves false.

The while loop has the general form:

while(condition)
{

statement block

}

The while tests its condition at the top of the loops. Therefore, if the condition is false to begin with, the loop will not execute at all. The condition may be any expression. An example of a while follows. It reads characters until end-of-file is encountered.

Example

main(){

int t = 0;
while(t<=10)
{

printf(“%d\n”,t);

t=t+1;

}

}

do-while Loop

This is very similar to the while loop except that the test occurs at the end of the loop body. This guarantees that the loop is executed at least once before continuing. Such a setup is frequently used where data is to be read. The test then verifies the data, and loops back to read again if it was unacceptable.

void main(void) {

int val;
do
{

printf(“Enter 1 to continue and 0 to exit :”);
scanf(“%d\n”, &val);

} while (val!= 1 && val!= 0);

}

Java Loops

Loops are used to repeat the execution of a statement or block of statements while the provided condition returns true. There are following type of java loops.

Java For Loop

for loop executes a bunch of statements continuously to accomplish a particular condition. A for loop is used when you know how many time a block of code to be repeated.

For Loop Syntax

for(initialization; condition; update)

{

//for loop body

}

Initialization: initialize the loop control variable

Condition: a boolean expression

Update: update loop control variable

For Loop Example

public class JavaLoop {

public static void main(String args[]) {

for(int i = 1; x <= 10; i++) {

System.out.print(“i = ” + i );

System.out.print(“\n”);

}

}

}

Output:

i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9
i = 10

Java While Loop

The while loop is used when you don’t want to repeat the execution of the code for a specific number of time. Instead you want to repeatedly execute code until a specified condition is met. The condition is tested each time before execution of the loop body.

While loop syntax

while(condition)

{

//while loop body

}

While loop example

public class JavaLoop{

public static void main(String args[]) {

int i = 1;

while( i <= 10 ) {

System.out.print(“Repetition number = ” + i );

System.out.print(“\n”);

i++;

}

}

}

Output:

Repetition number = 1
Repetition number = 2
Repetition number = 3
Repetition number = 4
Repetition number = 5
Repetition number = 6
Repetition number = 7
Repetition number = 8
Repetition number = 9
Repetition number = 10

Java do..while loop

The only difference between while and do..while loop is that the body of do..while is executed before testing the condition which mean the body of do..while loop will be executed at least once.

do..while loop syntax

do

{

//do..while loop body

} while(condition);

do..while loop example

public class JavaLoop{

public static void main(String args[]) {

int i = 1;

do {

System.out.print(“Repetition number = ” + i );

System.out.print(“\n”);

i++;

} while( i <= 10 );

}

}

Output:

Repetition number = 1
Repetition number = 2
Repetition number = 3
Repetition number = 4
Repetition number = 5
Repetition number = 6
Repetition number = 7
Repetition number = 8
Repetition number = 9
Repetition number = 10