answersLogoWhite

0

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

14y ago

What else can I help you with?

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 < 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 <= limit; i++) { count = i; while(count > 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) ; }


What does the count minus minus in java mean?

In Java, the expression count-- is a post-decrement operator. It decreases the value of the variable count by 1, but it returns the original value of count before the decrement. This means that if you use count-- in an expression, you will get the value of count prior to it being decremented. For example, if count is 5, using count-- will yield 5 in the current expression, but afterwards, count will be 4.


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


Java code that display even numbers from 0 to 10?

import java.util.*; class even{ public static void main(String[]args){ Scanner cons = new Scanner(System.in); int even; even = cons.nextInt(); if(even % 2 == 0) { System.out.println(even); } } }