bembry.org
Home / Technology / Java

Java Lesson 4: Characters and Loops

  • Characters
    • char is the data type for an individual character.
    • Initiate characters with following syntax:
      char init = 'B';
    • You must use single quotes when declaring characters. This allows the compiler to distinguish character definitions from variables in quote strings.
    • Characters can be arithmatically manipulated. ie. 'A'+1='B'.
    • The following methods are available in the Character class:
      isDigit(), isLetter(), isLetterOrDigit(), isLowerCase(), isUpperCase(), isWhitespace(), toLowerCase(), toUpperCase()
    • The following escape characters are also available:
      • \b backspace
      • \f form-feed
      • \n new line
      • \r carriage return
      • \t tab
  • Loops
    • for loops use the following syntax:
              for (initialization; loop_condition; increment_expression)
              { 
                  statements; 
              }
              
    • for loop Example:
              for (int i = 0; i < 10; i++)
              {
                  System.out.println(i);
              }
              
    • while loops use the following syntax:
              while(expression)
              {
                  statements
              }
            
    • while loop Example:
              int i = 0;
              while (i < 10)
              {
                  System.out.println(i);
                  i += 2;
              }
              
    • do ... while loops use the following syntax:
              do {
                  statements;
              } while (expression);
              
    • do...while loop Example:
              int i = 0;
              do {
                  System.out.println(i);
                  i += 5;
              } while(i <= 1000);
              
    • Although you can use floating point numbers in loop tests, never construct the test to rely on precise values for floating point numbers. Construct so that 1.9999999999 will work as well as 2.0
  • Breaks and Continues
    • A continue statement restarts a loop wih the next iteration, thus bypassing the remaining code in the loop.
    • The following example uses a continue stamet to sum and print only the odd numbers from 1 - 100
      
      public class ContinueCounting
      {
          public static void main(String[] args)
          {
              int sum = 0;
              for (int i = 1; i <= 100; i++)
              {
                  if (i % 2 == 0)
                      continue;
                  System.out.println(i);
                  sum += i;
              }
              System.out.println("\n Total: " + sum);
          }
      }
              
    • A break statement completely exits a loop, moving to the first line of code after the loop structure.
    • The following example lists the ASCII characters in reverse order from z, using a break statement to exit the loop at the first captial letter
          
      public class StopCaps
      {
          public static void main(String[] args)
          {
              char letter = 'z';
              while(Character.isDefined(letter))
              {
                 if (Character.isUpperCase(letter))
                      break;
                 System.out.println(letter);
                 letter -= 1;
              }
      
          }
      }
          
    • Labeled continue and labeled break statements are also available in Java for exiting loops to a specified point in the code.
Restricted access