![]() |
For Loop is a control flow statement that allows you to repeatedly execute a block of code based on a condition. It typically consists of an initialization, a condition, and an iteration statement. Types of For loop in C:In C, there is only one type of for loop, It consists of three main parts initialization, condition, and update. Syntax: for (initialization; condition; update) {
// code block to be executed
} Example:
Output 0 1 2 3 4 Types of For loop in C++:In C++, there are two types of for loop:
1. Traditional For Loop in C++:Traditional For loop consists of three main parts initialization, condition, and update. Syntax: for (initialization; condition; update) {
// code block to be executed
} Example:
Output 0 1 2 3 4 2. Range-based For Loop in C++:Range-based For Loop executes a for loop over a range. This type of loop is used to iterate over the values of a data structure, say array, vector, map, etc. Syntax:
Example:
Output 10 20 30 40 50 Types of For loop in Java:Java provides two types of for loops:
1. Traditional for loop in JavaThe traditional for loop in Java is used to iterate over a block of code a specific number of times. It consists of three parts Initialization, Condition and Update. Syntax: for (initialization; condition; update) {
// code block to be executed
} Example:
Output 0 1 2 3 4 2. For-each loop in JavaThe for-each loop, also known as the enhanced for loop. It provides a simpler way to iterate over arrays or collections in Java. Syntax: for (type variableName : arrayName) {
// code block to be executed
} Example:
Output 1 2 3 4 5 Types of For loop in Python:In Python, there is primarily one type of for loop, which is used to iterate over sequences such as lists, tuples, strings, or other iterable objects. However, Python’s for loop is quite flexible and can be used in various ways: for loop for iterating over a sequence: This is the most common use case for a for loop in Python, where it iterates over the elements of a sequence. Example:
Output apple banana cherry for loop with range(): The range() function is often used with a for loop to iterate over a sequence of numbers. Example:
Output 0 1 2 3 4 Types of For loop in C#:C# provides two types of for loops:
1. Traditional for loop in C#:Traditional for loop in C# is Similar to C, C++ for loop. Syntax: for (initialization; condition; increment)
{
// code to be executed
} Example:
Output 0 1 2 3 4 2. For-each Loop in C#:The foreach loop in C# is used for iterating over elements in arrays and collections. It simplifies the process of iterating over such structures compared to the traditional for loop. Example:
Output 1 2 3 4 5 Types of For loop in JavaScript:In JavaScript, there are three types of for loops:
1. Traditional for loop in JavaScript:Traditional for loop is used to iterate over a block of code a fixed number of times. It’s particularly useful when you know how many times you want to loop beforehand. Syntax: for (initialization; condition; increment/decrement) {
// code block to be executed
} Example:
Output 0 1 2 3 4 2. for…in loop in JavaScript:The for…in loop is used to iterate over the properties of an object. It enumerates the properties of an object in arbitrary order. Syntax: for (variable in object) {
// code block to be executed
} Example:
Output name: Sandeep age: 30 city: Noida 3. for…of loop in JavaScript:The for…of loop in JavaScript is used to iterate over the values of an iterable object, such as an array, string, map, set. Syntax: for (variable of iterable) {
// code block to be executed
} Example:
Output India China Japan There are many types of For loop in different languages but all of serve the same purpose, that is to execute a block of code repeatedly. These are some of the common variations of for loops, each serving different purposes depending on the specific requirements of the program. |
Reffered: https://www.geeksforgeeks.org
Programming |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 15 |