Question:
Why do you think the Java language provides three different types of loops?
UGLYASSBITYCH
2011-11-10 05:52:07 UTC
if all loops can be written using the while statement?
10 points for answer closest to what I think
Three answers:
steve
2011-11-10 05:57:57 UTC
certain loops make it easier to do certain things. While you can do it all with a while loop, its easier to know that in the following loop



for(int i = 0; i <10; i++)



the "i" is the counter, and you know that you didn't forget to increment it at the end. Whereas, If you had



int i = 0;

while (i < 10)

{

//code

//code

//oops, forgot to increment i

}



its harder to spot, and doesn't look as clean/readable. (also, there is a difference between the two loops as i has a different scope in the for loop)



and

for( something : somethingelse)

lets you iterate through an array or data structure really easily. You can do it with other loops, but its easier to type less.
2011-11-10 05:54:39 UTC
I think so, you can do that.

Loop is the control statement of any language in which whenever you want to perform the repetitious work then you use the Loop control statement. There are mainly three types of loops. Loop repeats a statement or a process multiple times according to the specified conditions. It allows the multiple statements or process to be run for the specified time or it also follows the certain conditions. Loop makes your program readable, flexible and reliable.



While loop: While loop checks the certain condition first, if the condition is true then all the statements or processes written under the while loop are executed otherwise ignored all.



The Syntax for the while loop:

while(condition)

{

statements;

}



In this program you will see how to use the while loop statement. This program take the value of variable n and generate the table of that number which is 2.



Here is a code of program:-



public class table{

public static void main(String[] args){

int n = 2;

int i = 1;

System.out.println("The table of "+n+" = ");

while(i<=10)

{

int t = n * i;

System.out.println(t);

i++;

}

}

}
Alma
2015-04-26 20:58:16 UTC
tricky factor. browse using google and yahoo. that will will help!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...