Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

For loop

The syntax of the for loop is:

for (initializationStatement; testExpression; updateStatement)


{
// statements inside the body of loop
}

How for loop works?

 The initialization statement is executed only once.


 Then, the test expression is evaluated. If the test expression is evaluated to false, the for
loop is terminated.
 However, if the test expression is evaluated to true, statements inside the body of for loop
are executed, and the update expression is updated.
 Again the test expression is evaluated.

This process goes on until the test expression is false. When the test expression is false, the loop
terminates.

for loop Flowchart


Example 1: for loop

// Print numbers from 1 to 10


#include<stdio.h>

Int main(){
Int i;

for (i = 1; i<11; ++i)


{
printf("%d ", i);
}
return0;
}

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.

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>
intmain()
{
intnum, count, sum = 0;

printf("Enter a positive integer: ");


scanf("%d", &num);

// for loop terminates when num is less than count


for(count = 1; count <= num; ++count)
{
sum += count;
}

printf("Sum = %d", sum);

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.

Nested for loop

The nested for loop means any type of loop which is defined inside the 'for' loop.

1. for (initialization; condition; update)


2. {
3. for(initialization; condition; update)
4. {
5. // inner loop statements.
6. }
7. // outer loop statements.
8. }

Example of nested 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. }

Explanation of the above code


 First, the 'i' variable is initialized to 1 and then program control passes to the i<=n.
 The program control checks whether the condition 'i<=n' is true or not.
 If the condition is true, then the program control passes to the inner loop.
 The inner loop will get executed until the condition is true.
 After the execution of the inner loop, the control moves back to the update of the outer
loop, i.e., i++.
 After incrementing the value of the loop counter, the condition is checked again, i.e.,
i<=n.
 If the condition is true, then the inner loop will be executed again.
 This process will continue until the condition of the outer loop is true.

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;
}

Example 2: Half Pyramid of Numbers

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;
}

Inverted half pyramid of *

*****
****
***
**
*

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;
}

Inverted half pyramid of numbers

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

What is infinite loop?

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.

When to use an infinite loop

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.

Let's understand through an example.

1. #include <stdio.h>
2. int main()
3. {
4. for(;;)
5. {
6. printf("Hello students");
7. }
8. return 0;
9. }

You might also like