Example 1: for loop // Print numbers from 1 to 10#include<stdio.h>intmain(){inti;for(i =1; i <11; ++i) {printf("%d ", i); }return0; } Run Code Output 1 2 3 4 5 6 7 8 9 10 iis initialized to 1. The test expressioni < 11is evaluated. Since 1 less than 11 is tr...
for (item in collection) { // body of loop } Example: Iterate Through a Range funmain(args:Array<String>){for(iin1..5) { println(i) } } Here, the loop iterates through the range and prints individual item. Output 1 2 3 4 5 ...
Function withIndex() in for loop In the above example we have iterated through the array using array indices. Another way of doing the same is with the use of withIndex() function. packagebeginnersbook fun main(args:Array<String>){val myArray=arrayOf("Steve","Robin","Kate","Lucy")for(...
Example 2 - for Loop With a Break Statement The break statement is used within the ‘for loop’ to end the loop. First, create a file and run the following code. For instance, we will use for loop to read the list of names, and we will test two conditions. The first is if the ...
1.1 Python For Loop Example with String A string contains a sequence of characters so, we can iterate each character in a string using for loop. Here we have taken ‘Hello‘ as a string so, using for the statement we can iterate over each character in a string. For example, ...
Here is a simple example of how you can use the For loop to get the Even Numbers from 1 to 30. Sub Get_Even_Numbers() Dim i As Integer For i = 1 To 30 If i Mod 2 = 0 Then Debug.Print i Else End If Next i End Sub Use this code in your VBA editor and Run the code. ...
Example: Iterate an array using for loop Let’s see another example of for loop. Here we areiterating and displaying array elements using the for loop. classForLoopExample3{publicstaticvoidmain(Stringargs[]){intarr[]={2,11,45,9};//i starts with 0 as array index starts with 0 toofor...
Example of For Loop: Lets create a program to find the sum of 1 to 100 number using for Loop: public class ForExample { public static void main(String[] args) { int i; int total = 0; for(i=1;i<=100;i++){ total = total + i; ...
Loop can run infinitely if condition is set to TRUE always or no condition is specified. For example: for (;;) If loop contain only one statement then braces are optional; generally it is preferred to use braces from readability point of view. For example : ...
2. Java For-loop Example In the following program, we are iterating over an array ofintvalues. The array contains 5 elements so the loop will iterate 5 times, once for each value in the array. int[]array=newint[]{0,1,2,3,4};for(inti=0;i<array.length;i++){System.out.format(...