Building the Loop

Author Topic: Building the Loop  (Read 1214 times)

Offline chinmay.sahooTopic starter

  • Trade Count: (0)
  • Full Member
  • ***
  • Thank You 2
  • Posts: 158
  • Karma: 0
    • We Helpline
Building the Loop
« on: 05-14-2016, 04:06:11 »
Once you’ve set up the parts of the for statement, the loop itself is easy to use. Place braces ({}) around your code and indent all code that’s inside the loop. You can have as many lines of code as you wish inside a loop, including branching statements and other loops.
The sentry variable has special behavior inside the loop. It begins with the initial value.

Each time the loop repeats, it is changed as specified in the for structure, and the interpreter checks the condition to ensure that it’s still true. If so, the code in the loop occurs again. In the case of the basicArray program, $i begins as 0. The first time the print statement occurs, it prints 0 because that is the current value of $i. When the interpreter reaches the right brace that ends the loop, it increments $i by 1 (following the $i++ directive in the for structure) and checks the condition ($i < 10). Because 0 is less than 10, the condition is true and the code inside the loop occurs again. Eventually, the value of $i becomes 10, so the condition ($i < 10) is no longer true. Program control then reverts to the next line of code after the end of the loop, which ends the
Although the sentry variable is an ordinary variable, it should not be changed inside the loop.
Let the for loop instructions do all the work.


Related Topics

  Subject / Started by Replies Last post
6 Replies
Last post 11-25-2017, 00:05:41
by damponting44
3 Replies
Last post 09-29-2017, 03:05:48
by damponting44
2 Replies
Last post 01-10-2013, 23:29:05
by onefor1two
1 Replies
Last post 07-31-2019, 00:50:09
by John - Smith
2 Replies
Last post 03-17-2016, 23:16:38
by RH-Calvin