While Loop Fragments

// To print a horizontal row of 50 asterisks: 
(NOTE: there are various ways of accomplishing the same task)
int number = 0; 
while (number < 50)
{
     System.out.print( "*");
     number++;
}
System.out.println("\n Here are " +
number +" stars");
int number = 1;
while (number <= 50)
{
     System.out.print( "*");
      number++;
}
System.out.println("\nHere are " + number + " stars");

 

// To print the numbers 4 - 14 on a single line: 
int num = 4;
while (num < 15)
{
     System.out.print( (num++) + " ");
}
int num = 4; 
while (num <=14)
{
     System.out.print(( num++) + " ");

 

// Program quits when a -1 is entered:
 
(NOTE: using a specified number (called a SENTINEL) to terminate a situation. Also note the use of ONE keyboard retrieval statement to "prime" the while loop, and a SECOND similar statement to continue the loop.)
int count = 0;
int grade;
System.out.println("Please enter grade ( enter -1 to quit)"); // -1 is sentinel value 
grade=reply.nextInt(); //get first value to start (prime) the while loop

while (grade != -1) 
{
     count++;
     System.out.println("Grade # "  + count  + " is " +  grade);
    
     System.out.println("Please enter grade ( enter -1 to quit) "); //get next while value
     grade=reply.nextInt();
}

divider
Return to Unit Menu |  JavaBitsNotebook.com | MathBits.com | Terms of Use  | JavaMathBits.com