0

can anyone help me on for,while and do loops...i honestly don't get them!!!!
can you show me samples of them???

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by AuburnMathTutor
1

FOR LOOPS

They keep executing code for a LIMITED amount of time specified by the user.
eg.

for (int i = 0; i < 10; i++)
{
System.out.println ("hello world");
}

This will print Hello world 10 times.
it has 4 parts. 1) initializing condition: the 'int i = 0;' initializes the loop saying that the loop will start at this number. 2) the stopping condition: the 'i < 10' is the stopping condition saying that only when i is 9, the loop will stop running (keep in mind when you write < 10 or > 10, 10 is NOT inclusive). 3) the increment/decrement which takes us to the stopping condition. i++ increases the value of i each time the loop runs by 1. 4) the code which is to repeat. it goes in {}. In this case hello world will be outputted 10 times.


WHILE LOOPS

They keep executing as long as the condition you put is true. Which means as soon as the condition is false, bam! no more looping.
eg.

int i = 0;
while (i != 10)
{
System.out.println ("Hello World");
}

This prints Hello World 10 times.
It has 3 parts. 1) The stopping variable: In this case it's i. it has the initial value of 0. It is important to give i a value before using it in the loop otherwise it will give you an error. 2) The stopping condition: The loop will go on as long as i is not 10. But as soon as i is 10, the loop doesn't execute. 3)the code INCLUDING the increment/decrement at the end. Without the increment the code will execute infinitely.

DO-WHILE LOOPS

This is almost the same as the while loop. The major difference is that the condition is tested at the bottom of the loop. This means that the body of the loop is executed at least one time regardless if the condition is true or false. If the result of the conditional is true, then the loop body is repeated. This process is repeated until the conditional is false.
eg.

int i = 0;
do{
   System.out.println ("Hello World");
   i++;
  }while (i != 10)

This outputs hello world 10 times.
It has 3 parts. 1) the variable: In this case, it is i. 2) the code: In this case, hello world and the increment. 3) the condition: In this case , "while (i is not equal to 10)". as soon as i's value hits 10, the code will stop running.

The following is the summary and the syntax for these loops:
1)

for ( variable initialized ; stopping condition; increment/decrement)
     {
     //code to loop as long as the stopping condition is not reached
     }

2)

while (condition)
     {
     //code to loop as long as the condition is true
     }

3)

do{
     //perform this code at least once and then keep looping till condition is true
     } while (condition)

I suggest you play around and experiment with these to understand to completely. Hopefully this helped.

Edited by insanely_sane: n/a

Votes + Comments
Thank for wasting 10 minutes of your lifetime writing this.
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.