.


MathBits.com
     Return to Unit Menu | Java Main Page | MathBits.com | Terms of Use                                                                                      
                                                              
Demo Fragments
Ending a Loop Early

//using break within a loop                      
// eliminating the letter c                             

String puppy = "DipStick";                          
int count = -1;                                                  
while (count < 7)                                              
{                                                                          
       count++;
       if (puppy.charAt(count)=='c')
               break;
    System.out.println(puppy.charAt(count));
}                                                                          
System.out.println("Isn't this puppy a
                                                     cutie!!");
//using continue within a loop
// eliminating the letter c
String puppy="DipStick"; 
int count = -1; 
while (count < 7)
{
     count++;
      if ( puppy.charAt(count) =='c')
               continue; 
    System.out.println(puppy.charAt(count));
}
System.out.println("Isn't this puppy a
                                                cutie!!");          
Screen display:
D
i
p
S
t
i
Isn't this puppy a cutie!!
Screen display:
D
i
p
S
t
i
k
Isn't this puppy a cutie!!

 


//Exiting the program
if ((age < 19) && ( grade >= 85) && (infractions == 0))
   System.exit(0);    // program STOPS here. Nothing else will be printed.

 

                     
Return to Unit Menu | Java Main Page | MathBits.com | Terms of Use