Showing posts with label for. Show all posts
Showing posts with label for. Show all posts

Friday, February 8, 2013

Excercise between W1 and W3

Week-3: 3.5: For Loop


As we mentioned earlier, the programming you are doing now is sequential
programming. This means that flow is downward, from top to bottom, with every line of
code being executed, unless you tell Java otherwise.

You saw in the last section that one way to "tell" Java not to execute every line is by
using IF Statement to section off areas of code.

Another way to interrupt the flow from top to bottom is by using loops. A programming
loop is one that forces the program to go back up again. If it is forced back up again
you can execute lines of code repeatedly.

As an example, suppose you wanted to add up the numbers 1 to 10. You could do it quite
easily in Java like this:

int addition = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10;

But you wouldn't really want to use that method if you needed to add up the numbers 1
to a 1000. Instead, you can use a loop to go over a line of code repeatedly until you've
reached 1000. Then you can exit the loop and continue on your way.

Visit for Details