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
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 » ...
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; } ...
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...
In this example, we’ll use a C++ for loop to calculate the factorial of a given number step by step. Code Example: #include <iostream> using namespace std; int main() { int n, factorial = 1; cout << "Enter a number: "; cin >> n; for (int i = 1; i <= n; i++) {...
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("@”);
If the test condition is true, the body of the loop executes The update expression gets updated Again the test condition is evaluated The process repeats until the test condition becomes false. Example: For loop in C Compiler // Program to print numbers from 1 to 10 #include <stdio.h> in...
Note:For those who don’t know printf or need to know more about printf format specifiers, then first a look at ourprintf C language tutorial. Let’s look at the “for loop” from the example: We first start by setting the variable i to 0. This is where we start to count. Then ...