answersLogoWhite

0


Best Answer

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.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

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++);

}

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

int main (int argc, char **argv)

{

int i= 0;

do printf ("%d: %s\n", i, argv[i]); while (++i < argc);

return 0;

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

dutkdtukuk

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: A java do while loop to count even numbers from one to ten?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Write a method which displays first 50 prime numbers?

Implement an isPrime method int JAVA with this: int count = 0, num = 2; while(count &lt; 50) { if(isPrime(num)) count++; num++; }


1 22 333 4444 55555 i need a java program that generates a triangle with these numbers?

Implement this method: public static void makeTriangle(int limit) { int count = 0; for(int i = 1; i &lt;= limit; i++) { count = i; while(count &gt; 0) { System.out.print(i); count--; } System.out.println(); } }


What Program that will display the even numbers in data structures and algorithms?

JAVA


Java program to find sum on even numbers from 12-45?

sum = 0; for (int i = 12; i


Java code to average and total a set of numbers?

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) ; }


Is unsigned int var valid in java?

No. Java uses no unsigned numbers.


Write a java program to find sum of even and odd numbers in given array?

for(int i = 1; i &lt; 100; i+=2) { System.out.println(i); }


Does java supports unsigned data types?

No, in Java, only signed numbers are defined.


How can random numbers be generated in Java?

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.


How can you create java average calculator..any integer number.using netbeans or notepad plus plus?

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 &lt; numbers.length; i++){ System.out.print("Number: "); numbers[i] = input.nextDouble(); count += numbers[i]; } avg = count / amount; System.out.println(avg); } }


Differences between Java Applet and Java Beans?

Java applet is a program used to run java applications while beans is a compiler used to design java programs (IDE, GUI) :-) GilbertC


What would be the best java or net?

Java is always the best. Java is great for Enterprise while desktop is the place for .Net