For loop
For loop in programming is a control flow structure that iterates over a sequence of elements, such as a range of numbers, items in a list, or characters in a string. The loop is entry-controlled because it determines the number of iterations before entering the loop.
Below is the implementation of For loop in Programming
The basic syntax for a for loop often includes a variable that takes on each value in the sequence, and the loop body contains the code to be executed during each iteration.
Below is the implementation of For Loop in Programming:
#include <iostream>
using namespace std;
int main()
{
for (int i = 0; i < 5; i++) {
cout << i << " ";
}
cout << endl;
return 0;
}
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args)
{
// Entry-controlled for loop
for (int i = 0; i < 5; i++) {
System.out.print(i + " ");
}
System.out.println();
}
}
# Entry-controlled for loop
for i in range(5):
print(i, end=" ")
print()
using System;
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 5; i++)
{
Console.Write(i + " ");
}
Console.WriteLine();
}
}
let output = '';
for (let i = 0; i < 5; i++) {
output += i + ' ';
}
console.log(output);
Output
0 1 2 3 4
Loops in Programming
Loops or Iteration Statements in Programming are helpful when we need a specific task in repetition. They’re essential as they reduce hours of work to seconds. In this article, we will explore the basics of loops, with the different types and best practices.
Table of Content
- What are Loops in Programming?
- Types of Loops in Programming
- For loop
- While Loop
- Do-While Loop
- Nested Loops
- For Loop vs While Loop vs Do-While Loop in Programming
- Common mistakes to avoid in Programming