For Loop - 1
For Loop - 1
This process goes on until the test expression is false. When the test expression is false, the loop
terminates.
Int main(){
Int i;
Output
1 2 3 4 5 6 7 8 9 10
1. i is initialized to 1.
2. The test expression i< 11 is evaluated. Since 1 less than 11 is true, the body of for loop is
executed. This will print the 1 (value of i) on the screen.
3. The update statement ++i is executed. Now, the value of i will be 2. Again, the test
expression is evaluated to true, and the body of for loop is executed. This will print 2
(value of i) on the screen.
4. Again, the update statement ++i is executed and the test expression i< 11 is evaluated.
This process goes on until i becomes 11.
5. When i becomes 11, i< 11 will be false, and the for loop terminates.
#include<stdio.h>
intmain()
{
intnum, count, sum = 0;
return0;
}
Nested Loops in C
C supports nesting of loops in C. Nesting of loops is the feature in C that allows the looping of
statements inside another loop. Let's observe an example of nesting loops in C.
Any number of loops can be defined inside another loop, i.e., there is no restriction for defining
any number of loops. The nesting level can be defined at n times. You can define any type of
loop inside another loop; for example, you can define 'while' loop inside a 'for' loop.
Syntax of Nested loop
1. Outer_loop
2. {
3. Inner_loop
4. {
5. // inner loop statements.
6. }
7. // outer loop statements.
8. }
Outer_loop and Inner_loop are the valid loops that can be a 'for' loop, 'while' loop or 'do-while'
loop.
The nested for loop means any type of loop which is defined inside the 'for' loop.
1. #include <stdio.h>
2. int main()
3. {
4. int n;// variable declaration
5. printf("Enter the value of n :");
6. // Displaying the n tables.
7. for(int i=1;i<=n;i++) // outer loop
8. {
9. for(int j=1;j<=10;j++) // inner loop
10. {
11. printf("%d\t",(i*j)); // printing the value.
12. }
13. printf("\n");
14. }
Output:
Program to print:
*
**
***
****
*****
#include<stdio.h>
intmain(){
inti, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i<= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
return0;
}
1
12
123
1234
12345
C Program
#include <stdio.h>
intmain() {
inti, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i<= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("%d ", j);
}
printf("\n");
}
return 0;
}
*****
****
***
**
*
C Program
#include<stdio.h>
intmain(){
inti, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = rows; i>= 1; --i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
return0;
}
12345
1234
123
12
1
C Program
#include<stdio.h>
intmain(){
inti, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = rows; i>= 1; --i) {
for (j = 1; j <= i; ++j) {
printf("%d ", j);
}
printf("\n");
}
return0;
}
Program to:
1
23
456
7 8 9 10
#include<stdio.h>
intmain(){
int rows, i, j, number = 1;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i<= rows; i++) {
for (j = 1; j <= i; ++j) {
printf("%d ", number);
++number;
}
printf("\n");
}
return0;
}
Infinite Loop in C
An infinite loop is a looping construct that does not terminate the loop and executes the loop
forever. It is also called an indefinite loop or an endless loop. It either produces a continuous
output or no output.
An infinite loop is useful for those applications that accept the user input and generate the output
continuously until the user exits from the application manually. In the following situations, this
type of loop can be used:
All the operating systems run in an infinite loop as it does not exist after performing some
task. It comes out of an infinite loop only when the user manually shuts down the system.
All the servers run in an infinite loop as the server responds to all the client requests. It
comes out of an indefinite loop only when the administrator shuts down the server
manually.
All the games also run in an infinite loop. The game will accept the user requests until the
user exits from the game.
For loop
Let's see the infinite 'for' loop. The following is the definition for the infinite for loop:
1. for(; ;)
2. {
3. // body of the for loop.
4. }
As we know that all the parts of the 'for' loop are optional, and in the above for loop, we have
not mentioned any condition; so, this loop will execute infinite times.
1. #include <stdio.h>
2. int main()
3. {
4. for(;;)
5. {
6. printf("Hello students");
7. }
8. return 0;
9. }