Example 2: for loop // Program to calculate the sum of first n natural numbers // Positive integers 1,2,3...n are known as natural numbers #include <stdio.h> int main() { int num, count, sum = 0; printf("Enter a
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 ...
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. ...
C++11 introduced the rangedforloop. Thisforloop is specifically used with collections such asarraysandvectors. For example, // initialize an int arrayintnum[3] = {1,2,3};// use of ranged for loopfor(intvar : num) {// 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...
The for loop in Kotlin is used to iterate or cycle though the elements of array, ranges, collections etc. In this guide, we will learn how to use for loop in Kotlin with the help of various examples. A simple example of for loop in Kotlin In the followin
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; ...
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, ...
C for Loop: Example 2Print integers from 1 to 5 using for loop.#include <stdio.h> int main() { int i; //loop counter //method 1 printf("Method 1...\n"); for(i=1; i<=5; i++) printf("%d ",i); printf("\n"); //method 2 (increment is in printf statement) printf("...
Here is an example of an infinitely recursive loop: for (i = 1; i <= 10; i = 1) { printf(“%d\n”, i); } In this example, the programmer likely intended to type “i = i+1.” But instead, they typed “i=1.” Every time the loop initiates, the integeriis set to 1 agai...