Loops are a very important part of the programming languages. Loops are used to iterate over given collection, array, list, etc. There are mainly two types of loops named for loop and while loop. There are other types of loops but they can specially design for the specific programming languages.
Loop Types
As stated previously there are two types of general loop. They are while
and for
loops. For
loop is more popular than while
loop. Loops need some start case, continue condition and end condition which will end the loop to prevent infinite loop.
For Loop
For loop will have the general syntax below.
for(START_STATEMENT; END_CONDITION ; EXECUTION_STATEMENT){ CODE }
- `START_STATEMENT` is used for the first statement during execution and start of the for loop. After the first execution, it will not use again. It is like an initializer.
- `EXECUTION_STATEMENT` will be executed in every step of the loop. It will help to change the END_CONDITION variables to reach END_CONDITION.
- `END_CONDITION` specifies the end condition for the loop. If the end condition met the loop will end.
While Loop
While loop will have the following general syntax below. As we can see while loop is more simple then for loop where it has END_CONDITION
.
while(END_CONDITION){ CODE }
- `END_CONDITION` is the condition used in While loop where it specifies the end condition by using variables. When the end condition met the while loop will end.
Java Loop
Java programming language provides both for and while loops. It is very similar to the C/C++ and C# programming languages.
Java For Loop
Java programming language for loop will have the following syntax. It is the same which is described at the beginning of the post.
for(START_STATEMENT; END_CONDITION ; EXECUTION_STATEMENT){ CODE }
and as an example, we will start counting from 1 to the 10 with the following Java for a loop.
for (int i = 1; i < 11; i++) { System.out.println(i); }
- `int i` is the start statement of the loop. It will create an integer variable named `i` and set its initialization value to the `1`.
- `i < 11` is the end condition which will be checked in every loop step whether it meets or nor. If the end condition is met which means `i` is lower then 11 the loop will continue where when `i` is higher then 11 the loop will end.
- `i++` is the execution statement which is executed in every step of the loop. This is used to reach `i` to the end condition by increasing it.
Java While Loop
Java programming language also provides while loop like below.
while(END_CONDITION){ CODE }
We will use the following example which will start counting from 1 to the 10 .
int i = 1; while (i < 11) { System.out.println(i); i++; }
- `int i = 1` is used to specify initialize `i`.
- `i < 11` is the end condition where the loop will end when `i` reaches to the 10.
C/C++ Loop
C/C++ is very similar languages to Java and C#.
C/C++ For Loop
C/C++ programming languages for loop will have the following syntax. It is the same which is described at the beginning of the post.
for(START_STATEMENT; END_CONDITION ; EXECUTION_STATEMENT){ CODE }
and as an example, we will start counting from 1 to the 10 with the following C/C++ for a loop.
for (int i = 1; i < 11; i++) { printf("%d",i); }
- `int i` is the start statement of the loop. It will create an integer variable named `i` and set its initialization value to the `1`.
- `i < 11` is the end condition which will be checked in every loop step whether it meets or nor. If the end condition is met which means `i` is lower then 11 the loop will continue where when `i` is higher then 11 the loop will end.
- `i++` is the execution statement that is executed in every step of the loop. This is used to reach `i` to the end condition by increasing it.
C/C++ While Loop
C/C++ programming languages also provide while loop like below.
while(END_CONDITION){ CODE }
We will use the following example which will start counting from 1 to the 10.
int i = 1; while (i < 11) { printf("%d",i); i++; }
- `int i = 1` is used to specify initialize `i`.
- `i < 11` is the end condition where the loop will end when `i` reaches to the 10.
PHP Loop
PHP programming language provides both for and while loops. It is very similar to the C/C++ and C# programming languages.
PHP For Loop
PHP programming language for loop will have the following syntax. It is the same which is described at the beginning of the post.
for(START_STATEMENT; END_CONDITION ; EXECUTION_STATEMENT){ CODE }
and as an example, we will start counting from 1 to the 10 with the following PHP for a loop.
for ($i = 1; $i < 11; $i++) { echo $i; }
- `int i` is the start statement of the loop. It will create an integer variable named `i` and set its initialization value to the `1`.
- `i < 11` is the end condition which will be checked in every loop step whether it meets or nor. If the end condition is met which means `i` is lower then 11 the loop will continue where when `i` is higher then 11 the loop will end.
- `i++` is the execution statement that is executed in every step of the loop. This is used to reach `i` to the end condition by increasing it.
PHP While Loop
PHP programming language also provides while loop like below.
while(END_CONDITION){ CODE }
We will use the following example which will start counting from 1 to the 10 .
$i = 1; while ($i < 11) { echo $i; $i++; }
- `int i = 1` is used to specify initialize `i`.
- `i < 11` is the end condition where the loop will end when `i` reaches to the 10.
Python Loop
Python is a programming language that is designed for novice users. Python provides a different syntax from other programming languages like C/C++, Java, C#.
Python For Loop
We can use for
loop by specifying a list or using range()
function which will create an iterable list for specified range with numbers.
for ELEMENT in LIST: print(ELEMENT)
We will loop over a list from 1 to 10 which is created with the range()
function like below.
for x in range(1,10): print(x)

Python While Loop
While loop will be similar to the for loop where we will create an x
variable with the 1
initialization value and then increment in the while loop one by one.
x=1 while(x<11): print(x) x=x+1

C# Loop
The C# programming language is very similar to the C/C++ and Java programming languages.
C# For Loop
C# programming languages for the loop will have the following syntax. It is the same which is described at the beginning of the post.
for(START_STATEMENT; END_CONDITION ; EXECUTION_STATEMENT){ CODE }
and as an example, we will start counting from 1 to the 10 with the following C# for a loop.
for (int i = 1; i < 11; i++) { Console.WriteLine(i.ToString()); }
- `int i` is the start statement of the loop. It will create an integer variable named `i` and set its initialization value to the `1`.
- `i < 11` is the end condition which will be checked in every loop step whether it meets or nor. If the end condition is met which means `i` is lower then 11 the loop will continue where when `i` is higher then 11 the loop will end.
- `i++` is the execution statement that is executed in every step of the loop. This is used to reach `i` to the end condition by increasing it.
C# While Loop
The C# programming language also provides while loop like below.
while(END_CONDITION){ CODE }
We will use the following example which will start counting from 1 to the 10.
int i = 1; while (i < 11) { Console.WriteLine(i.ToString()); i++; }
- `int i = 1` is used to specify initialize `i`.
- `i < 11` is the end condition where the loop will end when `i` reaches to the 10.
JavaScript Loop
The JavaScript programming language is very similar to the C/C++ and Java programming languages.
JavaScript For Loop
JavaScript programming languages for the loop will have the following syntax. It is the same which is described at the beginning of the post.
for(START_STATEMENT; END_CONDITION ; EXECUTION_STATEMENT){ CODE }
and as an example, we will start counting from 1 to the 10 with the following JavaScript for a loop.
for ( i = 1; i < 11; i++) { console.log(i); }
- `int i` is the start statement of the loop. It will create an integer variable named `i` and set its initialization value to the `1`.
- `i < 11` is the end condition which will be checked in every loop step whether it meets or nor. If the end condition is met which means `i` is lower then 11 the loop will continue where when `i` is higher then 11 the loop will end.
- `i++` is the execution statement that is executed in every step of the loop. This is used to reach `i` to the end condition by increasing it.
JavaScript While Loop
The JavaScript programming language also provides while loop like below.
while(END_CONDITION){ CODE }
We will use the following example which will start counting from 1 to the 10.
int i = 1; while (i < 11) { console.log(i); i++; }
- `int i = 1` is used to specify initialize `i`.
- `i < 11` is the end condition where the loop will end when `i` reaches to the 10.