When i becomes 11, i < 11 will be false, and the for loop terminates. 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; print...
Case2 (Always FALSE condition): Variables ‘i’ is initialized before ‘do-while’ loop to ‘20’; iteration is increment of counter variable ‘i’; condition is FALSE always as ‘0’ is provided that causes NOT to execute loop statements, but it is noted here in output that loop stateme...
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 we say that the for loop must run if the counter i is smaller then ten. Last we say that every cycle i must be increased by one (i++). ...
Syntax and Examples of For Loop in C Programming The essential syntax of a for loop in C is: for ( ; ; ) This for loop lacks an evaluation expression. If nothing is evaluated as true or false, there can be no exit to this loop. If this program ran, it would begin an infinite ...
Now we will see some examples of this through some programs.C For Loop ExamplesProgram-1: Program to find the factorial of a numberFlowchart:Algorithm:Step 1: Start. Step 2: Initialize variables. Step 3: Check FOR condition. Step 4: If the condition is true, then go to step 5 ...
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 » In this example, we create a program that only print even numbers ...
Program to print half Pyramid of numbers using Nested loops #include<stdio.h> void main( ) { int i, j; /* first for loop */ for(i = 1; i < 5; i++) { printf("\n"); /* second for loop inside the first */ for(j = i; j > 0; j--) ...
Loops are very useful when you want to perform a task repeatedly. Loop's body has set of statements, which gets executed on every iteration until a given condition is met. We have three types of loops in C. The working of these loops are almost similar,
Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself. The methods used to do so are as follows: Using For Loop Using While Loop Using Do-While Loop As th...
For loop Examples Example - 1: The following program calculate the sum of 1+2+3+...+50. The sum is stated in sum = sum + x, where i takes values from 1 to 50. #include<stdio.h>main(){intsum;intx;sum=0;for(x=1;x<=50;++x)// x take values in {1,2,3,...,50}{sum...