Complete Program

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

Answer:

count is 0
count is 1
count is 2
count is 3
count is 4
Done with the loop

Complete Program

Here is a complete program that lets the user pick the initial value and the limit value:

import java.util.Scanner;
// User picks starting and ending value
class loopExample
{
  public static void main (String[] args )  
  {
    Scanner scan = new Scanner( System.in );
    int count, limit;

    System.out.print( "Enter initial value: " );
    count = scan.nextInt();

    System.out.print( "Enter limit   value: " );
    limit = scan.nextInt();

    while ( count <= limit )   // less-than-or-equal operator
    {
      System.out.println( "count is: " + count );
      count = count + 1;
    }
    System.out.println( "Done with the loop" );
  }
}

Of course, you will want to copy this to a file and run this in the usual way.

QUESTION 10:

If the user sets the initial value to -2 and the limit value to 1 what values will be printed out?