For Loop Syntax
The general syntax of for loop varies slightly depending on the programming language, but it typically consists of three main components: initialization, condition, and increment (or decrement).
for (initialization; condition; increment/decrement) {
// Code to be executed repeatedly
}
Here’s a basic overview of how a for loop works:
- Initialization: This part of the loop is where you initialize a variable or set a starting value for a counter variable that controls the loop. It typically occurs before the loop starts and is executed only once.
- Condition: The condition is a Boolean expression that determines whether the loop should continue executing or not. If the condition evaluates to true, the loop body is executed. If it evaluates to false, the loop terminates.
- Increment (or Decrement): This part of the loop is responsible for updating the loop control variable after each iteration. It typically occurs at the end of each iteration and is used to modify the loop control variable to eventually make the condition false.
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 along with its usage in different programming languages.
Table of Content
- What is For Loop?
- For Loop Syntax
- How does For Loop work?
- For Loop in different programming languages
- For Loop in C
- For Loop in C++
- For Loop in Java
- For Loop in C#
- For Loop in Python
- For Loop in JavaScript
- For Loop in PHP
- For Loop Use Cases
- Types of For Loops
- For Loop vs other loops