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!!

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