The while loop is good for scenarios where you don't know how many times a block or statement should repeat, but you want to continue looping as long as some condition is true. A while statement looks like this:
while (expression) {
// do stuff
}
In this case, as in all loops, the expression (test) must evaluate to a boolean result. The body of the while loop will only execute if the expression (sometimes called the "condition") results in a value of true. Once inside the loop, the loop body will repeat until the condition is no longer met because it evaluates to false.
Any variables used in the expression of a while loop must be declared before the expression is evaluated. In other words, you can't say
while (int x = 2) { } // This is not legal
The key point to remember about a while loop is that it might not run at all. If the test expression is false the first time the while expression is checked, the loop body will be skipped and the program will begin executing at the first statement after the while loop. Look at the following example:
int x = 8;
while (x > 8) {
System.out.println("in the loop");
x = 10;
}
System.out.println("past the loop");
Running this code produces
past the loop
Because the expression (x > 8) evaluates to false, none of the code within the while loop ever executes.
This is simpler with a for loop:
for (int i = 2; i <= 10; i += 2)
System.out.println(i);
If you (or your teacher) insists on a while loop, that's not much more difficult:
int i = 2;
while (i <= 10)
{
System.out.println(i++);
}
int main (int argc, char **argv)
{
int i= 0;
do printf ("%d: %s\n", i, argv[i]); while (++i < argc);
return 0;
}
dutkdtukuk
Implement this method: public static void makeTriangle(int limit) { int count = 0; for(int i = 1; i <= limit; i++) { count = i; while(count > 0) { System.out.print(i); count--; } System.out.println(); } }
JAVA
sum = 0; for (int i = 12; i
The below method is written in the assumption that, the n numbers are sent as an ArrayList of Integer values. public void computeSumAndAvg(ArrayList lst){ Iterator itr = lst.iterator(); int sum = 0; float average = 0; int count = 0; while(itr.hasNext(){ Integer val = (Integer) itr.next(); sum = sum + val.intValue(); count++; } average = sum/count; System.out.println("Sum is: " + sum) ; System.out.println("Average is: " + average) ; }
No. Java uses no unsigned numbers.
Implement an isPrime method int JAVA with this: int count = 0, num = 2; while(count < 50) { if(isPrime(num)) count++; num++; }
Implement this method: public static void makeTriangle(int limit) { int count = 0; for(int i = 1; i <= limit; i++) { count = i; while(count > 0) { System.out.print(i); count--; } System.out.println(); } }
JAVA
sum = 0; for (int i = 12; i
The below method is written in the assumption that, the n numbers are sent as an ArrayList of Integer values. public void computeSumAndAvg(ArrayList lst){ Iterator itr = lst.iterator(); int sum = 0; float average = 0; int count = 0; while(itr.hasNext(){ Integer val = (Integer) itr.next(); sum = sum + val.intValue(); count++; } average = sum/count; System.out.println("Sum is: " + sum) ; System.out.println("Average is: " + average) ; }
No. Java uses no unsigned numbers.
for(int i = 1; i < 100; i+=2) { System.out.println(i); }
No, in Java, only signed numbers are defined.
Random numbers can be generated in Java using the "random" class. One needs a single "random" object to generate a series of random numbers as a unit.
import java.util.*; public class avg { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("How many numbers: "); int amount = input.nextInt(); double [] numbers = new double[amount]; double count = 0; double avg; for(int i = 0; i < numbers.length; i++){ System.out.print("Number: "); numbers[i] = input.nextDouble(); count += numbers[i]; } avg = count / amount; System.out.println(avg); } }
Java applet is a program used to run java applications while beans is a compiler used to design java programs (IDE, GUI) :-) GilbertC
Java is always the best. Java is great for Enterprise while desktop is the place for .Net