The for statement in C

By: Daniel Malcolm Emailed: 1696 times Printed: 2188 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

There are plenty of different ways to write a program for a particular task. Let's try a variation on the temperature converter.
   #include <stdio.h>

   /* print Fahrenheit-Celsius table */
   main()
   {
       int fahr;

       for (fahr = 0; fahr <= 300; fahr = fahr + 20)
           printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
   }
This produces the same answers, but it certainly looks different. One major change is the elimination of most of the variables; only fahr remains, and we have made it an int. The lower and upper limits and the step size appear only as constants in the for statement, itself a new construction, and the expression that computes the Celsius temperature now appears as the third argument of printf instead of a separate assignment statement.

This last change is an instance of a general rule - in any context where it is permissible to use the value of some type, you can use a more complicated expression of that type. Since the third argument of printf must be a floating-point value to match the %6.1f, any floating-point expression can occur here.

The for statement is a loop, a generalization of the while. If you compare it to the earlier while, its operation should be clear. Within the parentheses, there are three parts, separated by semicolons. The first part, the initialization

   fahr = 0
is done once, before the loop proper is entered. The second part is the test or condition that controls the loop:
   fahr <= 300
This condition is evaluated; if it is true, the body of the loop (here a single ptintf) is executed. Then the increment step
   fahr = fahr + 20
is executed, and the condition re-evaluated. The loop terminates if the condition has become false. As with the while, the body of the loop can be a single statement or a group of statements enclosed in braces. The initialization, condition and increment can be any expressions.

The choice between while and for is arbitrary, based on which seems clearer. The for is usually appropriate for loops in which the initialization and increment are single statements and logically related, since it is more compact than while and it keeps the loop control statements together in one place.


C Home | All C Tutorials | Latest C Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Daniel Malcolm
javac options in Java
Operator Precedence in Java
Calling Multiple Listeners in JSF
Using free() Function in C
ForwardAction in Struts
Listing Files and Directories sample program in C
Binary Tree - (Self-referential Structures) example program in C
A simple program using EL in JSP
Command-line Arguments in C
Example Calculator program in C - describing use of External Variables in C
Assignment Operators and Expressions in C
The for statement in C
JSF Basics
assert() Versus Exceptions in C++
RMS Basics in J2ME

More Tutorials in C
Sum of the elements of an array in C
Printing a simple histogram in C
Sorting an integer array in C
Find square and square root for a given number in C
Simple arithmetic calculations in C
Command-line arguments in C
Calculator in C
Passing double value to a function in C
Passing pointer to a function in C
Infix to Prefix And Postfix in C
while, do while and for loops in C
Unicode and UTF-8 in C
Formatting with printf in C
if, if...else and switch statements in C with samples
Statements in C

More Latest News
Most Viewed Articles (in C )
Fopen and Getc implementation program in C
Variables and Arithmetic Expressions in C
The for statement in C
Character Arrays in C
Structures and Functions in C
malloc, calloc - Storage Management - in C
Using free() Function in C
Using Shift Operators in C
Using qsort() and bsearch() with strings - example program in C
Formatting with printf in C
Find square and square root for a given number in C
The Birth and history of C Programming Language
Basics of C
Getting Started with C
Symbolic Constants using #define in C
Most Emailed Articles (in C)
Command-line Arguments in C
Using printf function in C
Infix to Prefix And Postfix in C
Functions returning non-integer values in C
Variable Scope and functions in C
register Variables in C
Initialization of Variables in C
#if, #elif, #ifndef, #ifdef in C (Conditional Inclusion)
Address Arithmetic and pointers in C
Character Pointers and Functions in C
scanf and sscanf sample program in C
Using malloc() Function in C
Using realloc() Function in C
Using memset(), memcpy(), and memmove() in C
Bitwise Logical Operators in C