C For Loop Examples ❮ Previous Next ❯ Real-Life ExamplesTo demonstrate a practical example of the for loop, let's create a program that counts to 100 by tens:Example for (i = 0; i <= 100; i += 10) { printf("%d\n", i);} Try it Yourself » ...
For example, the factorial of 3 is 3 * 2 * 1 = 6. Return the factorial of the input number num. 1 2 int factorial(int num){ } Check Code Video: C for Loop Previous Tutorial: C if...else Statement Next Tutorial: C while and do...while Loop Share on: Did you find th...
A loop is used for executing a block of statements repeatedly until a particular condition is satisfied. For example, when you are displaying number from 1 to 100 you may want set the value of a variable to 1 and display it 100 times, increasing its valu
Example 1: Ranged for Loop Using Array #include<iostream>usingnamespacestd;intmain(){// initialize arrayintnumArray[] = {1,2,3,4,5};// use of ranged for loop to print array elementsfor(intn : numArray) {cout<< n <<" "; }return0; } ...
Example: for loop C# int i = 0; for(;;) { if (i < 10) { Console.WriteLine("Value of i: {0}", i); i++; } else break; }Output:Value of i: 0Value of i: 1Value of i: 2Value of i: 3Value of i: 4Value of i: 5Value of i: 6Value of i: 7Value of i: 8Value...
Example-6: Create infinite for loop Create a bash named loop6.bash with the following script to know the way to declare infinite for loop. Here, the loop will iterate for infinite times and print the counter value until the user presses Ctrl+C. #!/bin/bash # Initialize counter variable ...
Loops can be nested too. There can be loop inside another loop. Given below is example for nested loop to display right angle triangle of ‘@’ symbol. for (i=0; i < 5; i++) { for (j=0;j<=i;j++) { printf("@”);
Example for(inti =0; i <5; i++) { cout << i <<"\n"; } Try it Yourself » Example explained Statement 1 sets a variable before the loop starts (int i = 0). Statement 2 defines the condition for the loop to run (i must be less than 5). If the condition is true, the ...
However, some statements can be placed in the body of a for loop, which will exit the loop without the evaluation needing to return false. These are statements such as break, return, or goto. To demonstrate a more functional for loop in C programming language, take the example of a progr...
A 'for loop' is a bash programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement i.e. it is the repetition of a process within a bash script. Tutorial details For example, you can run UNIX command or task 5 times or ...