Horje
For loop Syntax

For loop is a control flow statement in programming that allows you to execute a block of code repeatedly based on a specified condition. It is commonly used when you know how many times you want to execute a block of code.

For-Loop

For loop Syntax

The syntax of For loop varies slightly depending on the programming language, but the basic structure is similar across many languages.

for (initialization; condition; update) {
    // Code block to be executed repeatedly as long as the condition is true
}

Here’s a general overview of the Syntax of For loop:

  1. Initialization: This is where you initialize the loop control variable. This variable is typically used to keep track of the current iteration of the loop.
  2. Condition: This is the condition that is evaluated before each iteration of the loop. If the condition is true, the loop continues; otherwise, it terminates.
  3. Update: This is where you update the loop control variable. Typically, you increment or decrement the variable to ensure progress toward the loop termination condition.

For loop Syntax in C/C++:

Syntax:

for (initialization; condition; update) {
    // Code block to be executed repeatedly as long as the condition is true
}

Explanation of the Syntax:

  • In C and C++, the for loop is initialized with an initial condition, followed by a loop condition, and an increment or decrement operation.
  • The loop first executes the initialization part, typically used to initialize loop control variables.
  • It then evaluates the condition specified in the loop header.
  • If the condition is true, the code block within the curly braces {} is executed.
  • After executing the code block, the loop performs the increment or decrement operation.
  • The loop then returns to the loop header and re-evaluates the condition. If the condition remains true, the process continues; otherwise, the loop terminates.

Implementation of For loop Syntax in C/C++:

C++

#include <iostream>
using namespace std;
 
int main()
{
    // For loop
    for (int i = 0; i < 10; i++) {
        cout << i << " ";
    }
 
    return 0;
}

C

#include <stdio.h>
 
int main()
{
    // For loop
    for (int i = 0; i < 10; i++) {
        printf("%d ", i);
    }
 
    return 0;
}

Output

0 1 2 3 4 5 6 7 8 9 

For loop Syntax in Java:

Syntax:

for (initialization; condition; update) {
    // Code block to be executed repeatedly as long as the condition is true
}

Explanation of the Syntax:

  • In Java, the for loop operates similarly to C and C++.
  • It begins with an initialization expression, followed by a loop condition, and an increment or decrement operation.
  • The loop first executes the initialization part.
  • It then evaluates the condition specified in the loop header.
  • If the condition is true, the code block within the curly braces {} is executed.
  • After executing the code block, the loop performs the increment or decrement operation.
  • The loop returns to the loop header and re-evaluates the condition. If the condition remains true, the process continues; otherwise, the loop terminates.

Implementation of For loop Syntax in Java:

Java

/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG
{
    public static void main(String[] args)
    {
        // For loop
        for (int i = 0; i < 10; i++) {
            System.out.print(i + " ");
        }
    }
}

Output

0 1 2 3 4 5 6 7 8 9 

For loop Syntax Python:

Syntax:

for variable in iterable:
    # Code block to be executed repeatedly for each element in the iterable

Explanation of the Syntax:

  • Python’s for loop iterates over the elements of an iterable object, such as lists, tuples, strings, etc.
  • It assigns each element of the iterable to the specified variable and executes the code block for each element.
  • The loop continues until all elements of the iterable have been processed.

Implementation of For loop Syntax in Python:

Python3

# For loop
for i in range(10):
    print(i, end=" ")

Output

0 1 2 3 4 5 6 7 8 9 

For loop Syntax in C#:

Syntax:

for (initialization; condition; update) {
    // Code block to be executed repeatedly as long as the condition is true
}

Explanation of the Syntax:

  • In C#, the for loop behaves similarly to C, C++, and Java.
  • It starts with an initialization expression, followed by a loop condition, and an increment or decrement operation.
  • The loop first executes the initialization part.
  • It then evaluates the condition specified in the loop header.
  • If the condition is true, the code block within the curly braces {} is executed.
  • After executing the code block, the loop performs the increment or decrement operation.
  • The loop returns to the loop header and re-evaluates the condition. If the condition remains true, the process continues; otherwise, the loop terminates.

Implementation of For loop Syntax in C#:

C#

using System;
 
public class GFG {
 
    static public void Main()
    {
        // For loop
        for (int i = 0; i < 10; i++) {
            Console.Write(i + " ");
        }
    }
}

Output

0 1 2 3 4 5 6 7 8 9 

JavaScript (JS) For loop Syntax:

Syntax:

for (initialization; condition; update) {
    // Code block to be executed repeatedly as long as the condition is true
}

Explanation of the Syntax:

  • JavaScript’s for loop operates similarly to other languages.
  • It consists of an initialization expression, a loop condition, and an increment or decrement operation.
  • The loop first executes the initialization part.
  • It then evaluates the condition specified in the loop header.
  • If the condition is true, the code block within the curly braces {} is executed.
  • After executing the code block, the loop performs the increment or decrement operation.
  • The loop returns to the loop header and re-evaluates the condition. If the condition remains true, the process continues; otherwise, the loop terminates.

Implementation of For loop Syntax in JavaScript:

Javascript

// For loop
for (let i = 0; i < 10; i++) {
    console.log(i + " ");
}

Output

0 
1 
2 
3 
4 
5 
6 
7 
8 
9 



Reffered: https://www.geeksforgeeks.org


Programming

Related
Write a program to print 1 to 100 without using any numerical value Write a program to print 1 to 100 without using any numerical value
What is Syntax? Components, Rules, and Common Mistakes What is Syntax? Components, Rules, and Common Mistakes
While loop in Programming While loop in Programming
Do-While loop in Programming Do-While loop in Programming
For loop in Programming For loop in Programming

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
15