While Loops

         fred = 0;
         while (fred <29)
         { System.out.println(“Hi”);
            fred ++;
         }

A String based stop:

Enter your name: Joe
Hello, Joe, how are you?
Do you want to be greeted again? (y/n) y
Enter your name: Melvin
Hello, Melvin, how are you?
Do you want to be greeted again? (y/n) y
Enter your name: Sarah
Hello, Sarah, how are you?
Do you want to be greeted again? (y/n) n
Good bye!

public class stopYet
{
   public static void main (String args [])
   {
   new stopYet ();
   }
   public stopYet ()
   {
   String continu = "y";
   while (continu.equals ("y") || continu.equals ("Y"))
   {
   String name = IBIO.inputString ("Enter your name: ");
   System.out.println ("Hello, " + name + ", how are you?");
   continu = IBIO.inputString ("Do you want to be greeted again? (y/n) ");
   }
   
   System.out.println("Good bye!");
   }
}
 

A longer program:

Starting count down:
1
2
3
4
5
6
7
8
9
10

 

Ooops. Wrong way. Next try:

Starting count down:
9
8
7
6
5
4
3
2
1
0

Ooops. Need to start at 10. Next try:

Starting count down:
10
9
8
7
6
5
4
3
2
1
Blast off!

 

public class counting
{
   public static void main (String args [])
   {
   new counting ();
   }
   public counting ()
   {
   System.out.println ("Starting count down: ");
   int i = 0;
   while (i < 10)
   {
   i++;
   System.out.println (i + "");
   }
   
   System.out.println("\nOoops. Wrong way. Next try: ");
   System.out.println ("\nStarting count down: ");
   i = 10;
   while (i >0)
   {
   i--;
   System.out.println (i + "");
   }
   
   System.out.println("\nOoops. Need to start at 10. Next try: ");
   System.out.println ("\nStarting count down: ");
   i = 10;
   while (i >0)
   { 
   System.out.println (i + "");
   i--;
   }
   System.out.println("Blast off!");
 }
}