From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/SYNTAX/for.html | 175 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 reference/C/SYNTAX/for.html (limited to 'reference/C/SYNTAX/for.html') diff --git a/reference/C/SYNTAX/for.html b/reference/C/SYNTAX/for.html new file mode 100644 index 0000000..e90ac20 --- /dev/null +++ b/reference/C/SYNTAX/for.html @@ -0,0 +1,175 @@ +The FOR keyword. + + + + +
+

The FOR keyword.

+
+The for keyword is used to +repeat a block of code many times. +

+

+ +
+

Basic principles

+Say you wanted to print all the numbers between 1 and 10, you could write: +
+	main()
+        {
+	   int count=1;
+
+	   printf("%d\n", count++);
+	   printf("%d\n", count++);
+	   printf("%d\n", count++);
+	   printf("%d\n", count++);
+	   printf("%d\n", count++);
+	   printf("%d\n", count++);
+	   printf("%d\n", count++);
+	   printf("%d\n", count++);
+	   printf("%d\n", count++);
+	   printf("%d\n", count++);
+        }
+
+ +As you can see this program would NOT be very practical if we wanted +1000 numbers. The problem can be solved with the for statement +as below. +
+	main()
+        {
+	   int count;
+
+           for ( count=1 ; count <= 10 ; count++) printf("%d\n", count);
+        }
+
+ +The for statement can be broken down into 4 sections: +

+

+
count=1 +
is the initalisation.

+ +

count <= 10 +
An expression. The for statement continues to loop while this +statement remains true

+ +

count++ +
increament or +decreament.

+ +

printf("%d\n", count) +
the statement to execute.

+

+ + +
+

Repeating several lines of code

+ +The previous example showed how to repeat ONE statement. This example +shows how many lines can be repeated. + +
+	main()
+        {
+	   int count, sqr;
+
+           for ( count=1 ; count <= 10 ; count++) 
+           {
+	      sqr=count * count;
+              printf( " The square of");
+	      printf( " %2d", count);
+	      printf( " is %3d\n", sqr);
+           }
+        }
+
+ +The { and } following the for statement define +a
block of statements. + + +
+

More detail

+ +The for statement performs the following functions while looping. + +
+	for (expression_1 ; expression_2 ; expression_3) statement ;
+
+
    +
  1. Executes expression_1.

    +

  2. Executes statement. +
  3. Executes expression_3. +
  4. Evaluates expression_2. +

    +

      +
    • If TRUE, Jumps to item 2. +
    • If FALSE, Stops looping. +
    +
+ +Any of the three expressions can be missing, if the first or third is missing, +it is ignored. If the second is missing, is is assumed to be TRUE.

+ +The following example is an infinite loop: + +

+	main()
+	{
+	   for( ; ; ) puts(" Linux rules!");
+	}
+
+ + + +
+

Examples:

+ +
+Basic for example. +
+ + +Advanced for example. +
+ +
+

See also:

+ + +

+ +


+

+

+ + + + +
+ Top + + Master Index + + Keywords + + Functions +
+
+

+


+
Martin Leslie +

+ + -- cgit v1.2.3-54-g00ecf