Welcome to pickSourcecode.com Login | Register    
pickZy.com
 Home  | search  | games  | General  | C  | C++  | Java  | Php  | Networking  | Visual Basic  | VC++  | Win32  | MFC  | JavaScript  | Jobs  | JavaScript  | Post jobs
C - For loop statements       Share
2007-08-30 |  RatheeshTR  | Viewed: 296  |    0


Loops --  For statement:

The third and last looping construct in C is the for loop. The for loop can execute a block of code for a fixed or given number of times. Its syntax is as follows.        The basic format of the for statement is:

for ( start condition; continue condition;  re-evaluation)
            program statement;

           for  ( expr 1;  expr 2;  expr 3; )
                    statement
is equivalent to

                                expr 1;
                                while ( expr2 )
                                { 
                                        statement
                                        expr 3;
                                }


The simplest way to understand for loops is to study several examples.

First, here is a for loop that counts from 1 to 10.

for (count = 1; count <= 10; count++)
{
    printf("%d\n",count);
}

The test conditions may be unrelated to the variables being initialized and updated. Here is a loop that counts until a user response terminates the loop.

for (count = 1; response != 'N'; count++)
{
    printf("%d\n",count);
    printf("Dam man, you still want to continue? (Y/N): \n");
    scanf("%c",&response);
}

An example of using a for loop to print out characters

#include <stdio.h>

int main(void)
{

char letter;

for (letter = ‘A’; letter <= ‘E’; letter = letter + 1)
{
    printf(“%c”, letter);
}

return 0;


}


Example program:

#include <stdio.h>

int main(void)
{

int count;

for (count = 1; count <=10; count = count + 1)
printf(“%d”, count);

printf(”\n”);

return 0;

}

Sample program output

1 2 3 4 5 6 7 8 9 10

Example program 2:

1. 
For loop sample 


Latest topics
C - Arrays of structures  Viewed: 285
C - structures and functions  Viewed: 292
tcpdump by host name or ip  Viewed: 292
C - pointers and Arrays  Viewed: 295
C - File Access  Viewed: 295
C - For loop statements  Viewed: 296
C - pointers to pointers  Viewed: 296
C - Register variables  Viewed: 297
C - Static variables  Viewed: 299
C - Pass-by-reference parameters  Viewed: 300

Comments:





Submit comment's

Type:

User Comment's:

Submitted By:
Prof: Software Engineer
Tech: C ,Cpp
Send Mail: ratheesh



Related topics
C - Functions and program structure, Programmed pass-by-reference via pointers, Pass-by-reference parameters
C - Pass-by-reference parameters
C - pass-by-reference via pointers

Related References
c - swap num, pass-by-reference parameters
c - pass-by-reference via pointers

Web site contents © Copyright 2007, All rights reserved.
Help | Terms and Conditions | Privacy Policy | About Us