C For Loop

Advertisements
Advertisements

The for loop is a simpler version of the while loop that we learned in the previous lesson. It has the same components as while loop.

For Loop Basics

The for loop is more popular because all conditions for loop is declared in one place.

Before the for loop starts.

  1. Loop variable – It is used for initializing the loop, test condition for loop and increment or decrement the loop.
  2. Loop test condition– decide whether to execute the loop body or not.
  3. Loop body – contains statements required for performing a task inside the loop.

Logically while loop and for loop is same but the for loop is lot more simple than the while loop. This is because you can initialize, put test conditions and increment or decrement the for loop in a single line.

for( initialize counter; test condition; increment or decrement counter)
{
   Statements;
}

Flowchart Symbol of ‘for’ loop

The following symbol is used when we draw a flowchart for a C program that make use of a for loop.

Advertisements
Diagram - For Loop
Diagram – For Loop

Example Program

In this example program, we are going to a number over and over for 10 times and print the result. This program uses a single for loop.

/* C Program to add a number 10 times */
#include <stdio.h>
int main ()
{
     int number, total, i;
     number = 2;
     total = 0;
  
     for (i=0; i<10; i++)
     {
          total = total + number;
     }
  
     printf ("\n\n\n\n");
     printf ("\t\t\tThe total is %d", total);
  
getch ();
return 0;
}

Output

The total is 20

Example Explained

The program has a three variables – loop variable i, \hspace{2px} number \hspace{2px} and \hspace{2px}  total and it uses one for loop to repeatedly add the number to sum until the loop terminates.

First the variables number and total are initialized.

total = 0;
number = 2;

The loop variable i is used inside the for loop to initialize the loop, define the test condition and increment the loop.

for (i =0; i<10; i++)

Since the loop starts at i = 0, it will run for 10 iteration because the 10th iteration it will terminate. At each iteration, the loop will check the test condition against the current value of i.

When the condition for loop is ‘true’, the loop body will execute and add value of variable number to total repeatedly.

total = total + number;

The loop add the number 10 times and when i = 10, it terminates and prints the value of total as output.

printf ("\t\t\t\t The total is %d", total);

The program ends.

References

  • Balagurusamy, E. 2000. Programming in ANSI C. Tata McGraw-Hill Education,.
  • Kanetkar, Yashavant. 20 November 2002. Let us C. Bpb Publications.

Bestseller

Effective C: An Introduction to Professional C Programming

A detailed introduction to the C programming language for experienced programmers.

The world runs on code written in the C programming language, yet most schools begin the curriculum with Python or Java. Effective C bridges this gap and brings C into the modern era–covering the modern C17

You’ll learn:

  • How to identify and handle undefined behavior in a C program
  • The range and representations of integers and floating-point values
  • How dynamic memory allocation works and how to use nonstandard functions
  • How to use character encodings and types
  • How to perform I/O with terminals and filesystems using C Standard streams and POSIX file descriptors
  • How to understand the C compiler’s translation phases and the role of the preprocessor
  • How to test, debug, and analyze C programs

Effective C will teach you how to write professional, secure, and portable C code that will stand the test of time and help strengthen the foundation of the computing world.

Advertisements
Advertisements
Advertisements