For Loop in C

In C programming for loop is used to solve various important problems. In this tutorial, we will learn all about for loop in C programming.

A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

  • For loop is used to execute a block of code again and again based on some condition.
  • Condition decides how many times the loop will execute.
  • We use for loop where the number of iterations is pre-known.
  • We use for loop where the number of iterations is pre-known.
  • For example, write a program that prints your name 100 times.
  • In the above example, the number of iterations is pre-known i.e. 100 time.

Syntax of for loop

Note: for is case-sensitive, it always is written in lower-case.

In for loop, there are three very important terms are used that are

  1. initialization
  2. condition
  3. increment/decrement

Let's understand all these three terms with the help of an example.

Example

Write a program that prints Hello World 5 times:

Output

Explanation

In the above example you have used for loop to print Hello World five times.

Initialization : i=0

Condition: i<5

Increment/Decrement: i++ or i--

1. Initialization

Initialization is the process of assigning the value of the variable, In the above example it is i=0, If the variable is already assigned then we can write only i and the place of i=0 in the far loop. In for loop Initialization is done only one time.

2. Condition

The condition decides whether the loop run again or break. If the condition is true then the loop will run otherwise it will break. In the above example, the condition is i<5, the for loop will keep executing until i is less than 5.

3. Increment/Decrement

The increment/decrement is the process of increasing or decreasing the value of the variable. In the above example, the increment is i++ , it increases the value of i by 1.

  • i++ means i=i+1
  • i-- means i=i-1

Note: The increment is not limited till i++ or i--, you can use any increment like i+2, i+3, i-5, etc.

For Loop Working

The for loop executes in below-listed steps, see all steps very carefully.

Step 1: Initialization of variable. Initialization is done only once.

Step 2: Condition checked.

Step 3: If condition is false, terminate the loop, if not fallow step 4, 5.

Step 4: If condition is true then run the scope of for loop.

Step 5: Perform increment or decrement and again repeat steps 2,3.

Example

Write a C program to print number from 1 to 10 using for loop:

Output

Example

Write a program to print table of 5:

Output

Note: i=i+1 and i++ both are same.

Working with more than one variable

  • We can initialize more than one variable by separating with comma (,)
  • We can increment or decrement more than one value of variable by separating with comma (,)

See below example and think about output

Example

Output

Note: you can write more than one conditions, for more understanding see below example:

Example

Output

Pre initialize and post increment/decrement

  • We can initialize the variable before for loop, and the place of initialization can be empty but ; is must before and after condition.
  • We can perform increment or decrement of variables within the loop body but ; is important after condition statement.

For better understanding see the below example

Example

Output

For loop body braces {} are optional

For loop body braces are optional when body of loop contains only one statement like the below example.

Example

Output

Nested for loop in C

Nested for loop means for loop within for loop. In C programming for loop allows the nested form of for loop, you can use multi-level for loop in C programming. Various important problems are solved with the help of the nested for loop.

Nested for loop syntax

Let's take an example to make more clear the concept of nested for loop.

Example

Note: For one true condition of outer for loop, inner for loop runs for all true conditions each time.

Output

Explanation

  • In the outer loop the value of i initialize as 1 and condition i<=row is true because the value of row is 5. So the outer loop will execute.
  • In the inner loop, the value of j initializes as 1 and the condition j<=i is true because currently, the value of i is 1. So inner loop will print * and then j incremented by 1, means j++. Now the value of j is 2 and condition j<=i becomes false so the inner loop will terminate.
  • Below of the inner for loop outer for loop will print new line due to (\n) and the value of i incremented by 1 so i becomes 2.
  • Now the value of i is 2 and in the outer loop, the condition i<=row is still true so again inner loop evaluated this time two star ** will be printed.
  • This process will be continuing until the value of i becomes 6. In this way, you will get the output results.

Infinite for loop

If the condition of far loop is always true then it is called as infinite for loop because the for loop will never terminate. See the below examples

Example

Note: For one true condition of outer for loop, inner for loop runs for all true conditions each time.

Output

In the above example, the for loop condition never will be false, so it is infinite for loop, the loop will never terminate.

Example

Output

Note: If the conditional expression is absent then the compiler treats it as the true condition and it will work as an infinite loop.

Email Us: advertise@gdatamart.com

Donate Us: Support to GDATAMART

© 2023 GDATAMART.COM (All Rights Reserved)