Page 64 - Programming the Photon Getting Started With the Internet of Things
P. 64

for loop header:


        for (initialization; condition; increment) {
                              statements
        }

                   for (int x = 0; x < 10; x++) {
                              delay(500);
                   }


        The  initialization  happens  first  and  only  once;  each  time  through  the  loop,  the
        condition  is  tested.  If  it  is  true,  then  the  statements  are  executed,  as  well  as  the
        increment, and then the condition is tested again. The condition becomes false when the

        loop has repeated more than 10 times.





        while Loops



        Another  useful  way  of  looping  in  C  is  to  use  the  while  command  in  place  of  the  for
        command. while loops will loop continuously until the expression inside the parentheses

        () becomes false. Something has to change the tested variable; otherwise, the while loop
        will never end and will run indefinitely. The syntax for using a while loop is as follows:


                   while(expression) {
                              statements
                   }

                   int i = 0;
                   while (i = < 10)
                   {

                              delay(500);
                              i ++;
                   }


        The expression in the parentheses after the while must be true to stay in the loop. When it
        is no longer true, then the sketch continues running the commands after the curly braces.
        You may also notice the following line:


                   i ++;


        This is just C shorthand for the following expression:

                   i = i + 1;




        Arrays
   59   60   61   62   63   64   65   66   67   68   69