C Programming: Understand For loop syntax in deep detail




Syntax: 
                       for( A ; B ; C}
                       {
                            /* statement or what you want to do */
                       }
                        
                        My experience:
        1. A is initial variable if more than one we use && to separate it.
        2. B is condition, you may have one condition or many conditions so use && separate it.
        3. C will be performed if condition(s) B is/are true. C also can do more than one statement so use && between them as well.
Sample 1: Simple For loop
                      int main(int argc, char *argv[])
                      {
                           int a;
                           for( a=10; a<20; a=a+1 ) /* do one thing per ; as simple */
                           {
                              printf("value of a: %d\n",a);
                           }
                           return 0;
                       }
Some explanation from online course might not cover all thing For loop can do. 

Standalone DevC++ uses TIM-GCC version 4.9.2 64-bit




Sample 2: Old school C compiler with many things to do in For loop
                      int main(int argc, char *argv[]) 
                      { 
                            int a;
                            /* For loop as shown below do many things in ; */
                            for( a=10 ; a<20 ; printf("value of a: %d\n",a) && a++); 
                            return 0;
                      }
Standalone DevC++ uses TIM-GCC version 4.9.2 64-bit
                         

Sample 3: New version of C compiler such as C99 or C11 
                      int main(int argc, char *argv[]) 
                      {
                            /* We can declare variable anywhere such as int a=10 in for loop from C11 or C99 compiler */
                            for(int a=10 ; a<20 ; printf("value of a: %d\n",a) && a++); 
                            return 0;
                      }


                     3.1 In case C compiler is not enhanced or new version, It will raise error
It's my intention 
Standalone C-Free compiler can choose C compiler version


 3.2 In case C compiler is new version so it will be completely compiled 
Standalone DevC++ TIM-GCC version 4.9.2 64-bit

www.codingground.com uses GNU GCC version 7.1.1

www.jdoodle.com uses GNU GCC version 8.1.0
www.onlinegdb.com uses GCC 5.4.1 C99 version














You may leave comment to improve and know more than  this. Thank you.

Popular posts from this blog

C Programming: How to improve C to support more Array N dimensions

C Programming: Superb E-Book from beginner to expert [C career semi retired]