Looping statements, also known as iteration or repetition statements, are used in programming to repeatedly execute a block of code. They are essential for performing tasks such as iterating over elements in a list, reading data from a file, or executing a set of instructions a specific number of times. Here are some common types of looping statements:
The for
loop is used to iterate over a sequence (e.g., a list, tuple, string, or range) and execute a block of code for each item in the sequence.
C++
#include <iostream>
using namespace std;
int main()
{
for ( int i = 0; i < 5; i++) {
cout << i << endl;
}
return 0;
}
|
C
#include <stdio.h>
int main()
{
for ( int i = 0; i < 5; i++) {
printf ( "%d\n" , i);
}
return 0;
}
|
Java
public class Main {
public static void main(String[] args)
{
for ( int i = 0 ; i < 5 ; i++) {
System.out.println(i);
}
}
}
|
C#
using System;
class Program {
static void Main( string [] args) {
for ( int i = 0; i < 5; i++) {
Console.WriteLine(i);
}
}
}
|
Javascript
for ( var i = 0; i < 5; i++) {
console.log(i);
}
|
Python3
for i in range ( 5 ):
print (i)
|
The while
loop is used to repeatedly execute a block of code as long as a specified condition is true.
C++
#include <iostream>
using namespace std;
int main()
{
int count = 0;
while (count < 5) {
cout << count << endl;
count++;
}
return 0;
}
|
C
#include <stdio.h>
int main()
{
int count = 0;
while (count < 5) {
printf ( "%d\n" , count);
count++;
}
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int count = 0 ;
while (count < 5 ) {
System.out.println(count);
count++;
}
}
}
|
C#
using System;
class Program
{
static void Main( string [] args)
{
int count = 0;
while (count < 5)
{
Console.WriteLine(count);
count++;
}
}
}
|
Javascript
let count = 0;
while (count < 5) {
console.log(count);
count++;
}
|
Python3
count = 0
while count < 5 :
print (count)
count + = 1
|
In some programming languages, such as C and Java, a do-while
loop is used to execute a block of code at least once, and then repeatedly execute the block as long as a specified condition is true.
C++
#include <iostream>
using namespace std;
int main()
{
int count = 0;
do {
cout << count << endl;
count++;
} while (count < 5);
return 0;
}
|
C
#include <stdio.h>
int main()
{
int count = 0;
do {
printf ( "%d\n" , count);
count++;
} while (count < 5);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int count = 0 ;
do {
System.out.println(count);
count++;
} while (count < 5 );
}
}
|
C#
using System;
public class GFG {
static public void Main()
{
int count = 0;
do {
Console.WriteLine(count);
count++;
} while (count < 5);
}
}
|
Javascript
let count = 0;
do {
console.log(count);
count++;
} while (count < 5);
|
4. Nested Loops in Programming:
Loops can be nested within one another to perform more complex iterations. For example, a for
loop can be nested inside another for
loop to create a two-dimensional iteration.
C++
#include <iostream>
using namespace std;
int main()
{
for ( int i = 0; i < 2; i++) {
for ( int j = 0; j < 2; j++) {
cout << "i=" << i << " j=" << j << "\n" ;
}
}
}
|
C
#include <stdio.h>
int main()
{
for ( int i = 0; i < 2; i++) {
for ( int j = 0; j < 2; j++) {
printf ( "i=%d j=%d\n" , i, j);
}
}
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main (String[] args) {
for ( int i = 0 ; i < 2 ; i++) {
for ( int j = 0 ; j < 2 ; j++) {
System.out.println( "i=" + i + " j=" + j);
}
}
}
}
|
C#
using System;
public class GFG {
static public void Main()
{
for ( int i = 0; i < 2; i++) {
for ( int j = 0; j < 2; j++) {
Console.WriteLine($ "i={i} j={j}" );
}
}
}
}
|
Javascript
for (let i = 0; i < 2; i++) {
for (let j = 0; j < 2; j++) {
console.log(`i=${i} j=${j}`);
}
}
|
Python3
for i in range ( 2 ):
for j in range ( 2 ):
print (f "i={i} j={j}" )
|
Output
i=0 j=0
i=0 j=1
i=1 j=0
i=1 j=1
Each programming language may have its own syntax and specific variations of these looping statements.
Control flow statements in Programming
Control flow refers to the order in which statements within a program execute. While programs typically follow a sequential flow from top to bottom, there are scenarios where we need more flexibility. This article provides a clear understanding about everything you need to know about Control Flow Statements.
Table of Content
- What are Control Flow Statements in Programming?
- Types of Control Flow statements in Programming
- Conditional Statements in Programming
- Looping Statements in Programming
- Jump Statements in Programming
Contact Us