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 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:
- 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.
- 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.
- 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;
}
Output0 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 + " ");
}
}
}
Output0 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=" ")
Output0 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 + " ");
}
}
}
Output0 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 + " ");
}
Output0
1
2
3
4
5
6
7
8
9
Similar Reads
Solidity For Loop This is the most compact way of looping. It takes three arguments separated by a semi-colon to run. The for loop includes three most important parts: Loop Initialization: The first one is 'loop initialization' where the iterator is initialized with starting value, this statement is executed before t
2 min read
PostgreSQL - For Loops In PostgreSQL, PL/pgSQL (Procedural Language/PostgreSQL) introduces control structures like FOR loops to simple complex data processing. The FOR loop allows developers to iterate over a specified range of integers or the results of a query and making repetitive tasks more manageable. This feature is
6 min read
COBOL - Basic Syntax Cobol is a high-level language, which has its own compiler. The COBOL compiler translates the COBOL program into an object program, which is finally executed. A Syntax refers to the rules and regulations for writing any statement in a programming language. It is related to the grammar and structure
3 min read
Basic Syntax in LISP LISP is a list processing programming language. It is widely used in the manipulation of data strings. It provides an input and output library. LISP provides a macro system and provides well control structures for the manipulation of data. Basic Blocks in LISP:There are three basic building blocks o
2 min read
JavaScript Syntax JavaScript syntax refers to the rules and conventions dictating how code is structured and arranged within the JavaScript programming language. This includes statements, expressions, variables, functions, operators, and control flow constructs.Syntaxconsole.log("Basic Print method in JavaScript");Ja
6 min read
CSS Syntax CSS is written as a rule set, which consists of a selector and a declaration block. The basic syntax of CSS is as follows:The selector is a targeted HTML element or elements to which we have to apply styling.The Declaration Block or " { } " is a block in which we write our CSS.HTML<html> <h
2 min read
For loop in Programming For loop is one of the most widely used loops in Programming and is used to execute a set of statements repetitively. We can use for loop to iterate over a sequence of elements, perform a set of tasks a fixed number of times. In this article, we will learn about the basics of For loop, its syntax al
11 min read
Solidity - Basic Syntax Solidity is a programming language specifically designed for developing smart contracts on the Ethereum blockchain. It is a high-level, statically-typed language with syntax and features similar to those of JavaScript, C++, and Python. Solidity is used to write self-executing smart contracts that ca
5 min read
Dart - Basic Syntax Dart is a static programming language developed by Google. According to the GitHub popularity index, it became the most popular programming language as it actually supports the flutter toolkit. Flutter is a framework that uses dartâs native compilation ability to generate fast cross-platform apps. D
5 min read
Ruby Basic Syntax Ruby is a pure Object-Oriented language developed by Yukihiro Matsumoto (also known as Matz in the Ruby community) in the mid 1990âs in Japan. To program in Ruby is easy to learn because of its similar syntax to already widely used languages. Here, we will learn the basic syntax of Ruby language. Le
3 min read