For Loop
In programming languages, the for loop is a control flow statement that executes a block of code several times. It iterates the same code repeatedly until the specified condition is true.
Here, we understand the purpose of for loop statement with a situation. Let's suppose we want to print a particular string like "I am learning for loop" 10 times. There is no requirement to write the same string every time. The easy way is to put the string in a for loop once and iterate the loop 10 times.
Points to remember
- In C and C++, we can provide any values as true and false condition in conditional expression of for loop statement. Both language consider these values as Boolean and treats 0 as false and the rest of the values as true.
- Unlike C and C++, Java throws an error on passing values in conditional expression of for loop statement as it has a Boolean type. It treats these values as integers.
- JavaScript follows the similar for loop syntax as in Java with a difference that the three expressions enclosed within parenthesis are optional.
- Unlike C and other languages, Python's for statement can be used to iterate the elements of any type of sequence (a list or a string).
- Ruby uses do and end keywords in place of for loop open and close curly braces.
- Swift performs the iteration in a range or collection. The range can be represented in two ways such as lowerBound...upperBound like 0...10 or lowerBound..
- In rust, the for loop iterator is represented in the form of expression that contains the start and end position like 0...10. It includes the lower bound value and excludes the upper bound value.
Syntax in C/C++/C#/Java/JavaScript/PHP
Syntax in Python
Syntax in Ruby
Syntax in Swift/Rust
Example 1 in C
Example 1 in C++
Example 1 in C#
Example 1 in Java
Example 1 in JavaScript
Example 1 in Python
Example 1 in Ruby
Example 1 in PHP
Example 1 in Swift
Example 1 in Rust
Example 2 in C
Let's see an example to print the sum of first 50 natural numbers in C.
Example 2 in C++
Example 2 in C#
Example 2 in Java
Example 2 in JavaScript
Example 2 in Python
Example 2 in Ruby
Example 2 in PHP
Example 2 in Swift
Example 2 in Rust
Output:
Example 3 in C
Example 3 in C++
Example 3 in C#
Example 3 in Java
Example 3 in JavaScript
Example 3 in Python
Example 3 in Ruby
Example 3 in PHP
Example 3 in Swift
Example 3 in Rust
Output:
Example 4 in C
Example 4 in C++
Example 4 in C#
Example 4 in Java
Example 4 in JavaScript
Example 4 in Python
Example 4 in Ruby
Example 4 in PHP
Example 4 in Swift
Example 4 in Rust
Output:
*
***
*****
*******
*********
***********
Infinite Loop
Let's see the simple program of usage of an infinite loop in respective languages:
Program in C
This program creates an infinite loop. Until and unless, we press the key y, this loop continues. When we press the key 'y', this leads to the termination from the loop.
Program in C++
This program creates an infinite loop. Until and unless, we press the key 'y', this loop continues. When we press the key 'y', this leads the termination from the loop.
Program in C#
This program creates an infinite loop. Until and unless, we press the key ?Enter?, this loop continues. When we press the key enter, it leads to the termination from the loop.
Program in Java
This program creates an infinite loop and thus, prints 'javaTpoint' infinite times.
Program in JavaScript
This program creates an infinite loop and thus, prints 'javaTpoint' infinite times.
Program in Perl
This program creates an infinite loop and thus, prints 'javaTpoint' infinite times.
Program in PHP
This program creates an infinite loop and thus, prints 'avaTpoint' infinite times.
Program in Ruby
This program creates an infinite loop and thus, prints 'javaTpoint' infinite times.
Program in Swift
This program creates an infinite loop and thus, prints 'javaTpoint' infinite times.
Output:
javaTpoint javaTpoint javaTpoint javaTpoint javaTpoint javaTpoint javaTpoint javaTpoint javaTpoint javaTpoint
javaTpoint javaTpoint javaTpoint javaTpoint javaTpoint javaTpoint javaTpoint javaTpoint ................
(infinite times)
Usage of Nested loop
Let's see the simple program of printing a number pattern in respective languages.
Program in C
In this program, nested looping is used to create a number pattern. With each iteration of i, j is executed 'i' times. In 'C language', printf() function is used to print a pattern.
Program in C++
In this program, i is executed 5 times. Therefore, with each iteration of i, j is executed i times. In C++ language, cout object is used to print the pattern.
Program in C#
In this program, i is executed 5 times. Therefore, with each iteration of i, j is executed i times. In C# language, Console.Write() function is used to print the pattern.
Program in Java
In this program, i is executed 5 times. With each iteration of i, j is executed i times. In "java language", System.out.print() is used to print the pattern.
Program in JavaScript
In this program, i is executed 5 times. With each iteration of i, j is executed i times. In javascript, document.write() is used to print the pattern.
Program in Perl
In this program, $i is executed 5 times. With each iteration of $i, $j is executed $i times. In perl, printf() function is used to print the pattern.
Program in PHP
In this program, $i is executed 5 times. With each iteration of $i, $j is executed $i times. In PHP, echo keyword is used to print the pattern.
Program in Ruby
In this program, $i is executed 5 times. With each iteration of $i, $j is executed $i times. In ruby, print keyword is used to print the pattern on the screen.
Program in Swift
In this program, i is executed 5 times. With each iteration of i, j is executed i times. In swift, print function is used to print the pattern.
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
|