COMPUTER SCIENCE Summer 2006
Unit 1, Loops and if

Learn for and while loops. Learn about certain commands that return a true/false value.

  1. for loops. The basic structure is:
        for(counter start; counter stop test; counter increment) {
            body of the loop
      
        } 
    
    Example:
        for(int i=0; i<n ; i++) {
           robot.pickBeeper();
        }
    

  2. while loops. Basic structure:
           while(test condition(s)) {
               
               body of the loop
    
           }
    
           The test condition(s) are functions that return true or false
      
    Example:
          while(robot.nextToABeeper()) {
             robot.pickBeeper();
          }
    
          while (!robo1.rightIsClear() && !robo1.nextToABeeper()) {
                   
                 commands
                   
          }
    
     
  3. boolean commands that return true/false. Some examples:
         
         facingEast()
         facingNorth()
         facingSouth()
         facingWest()
         frontIsClear()
         leftIsClear()
         nextToABeeper()
         nextToARobot()
     
  4. if statements
         if(boolean statement(s)) {
    
             body of if
       
         } else {
        
             alternative body
         }
    
         The "else" portion is optional