Changes between Version 21 and Version 22 of ControlSystems/SoftwareTeam/Training/GettingStarted/IntroJava


Ignore:
Timestamp:
Nov 4, 2019, 3:13:30 PM (6 years ago)
Author:
David Albert
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ControlSystems/SoftwareTeam/Training/GettingStarted/IntroJava

    v21 v22  
    1 == Conditionals
    2 Programs need to be able to react differently to varying data.  Conditional statements let us do this.  For example, a robot must be able to monitor its sensors to prevent collisions.  Consider (and try running) the following program:
    3 * Create a new folder and new file; cut and paste this program into it and save it as !ConditionalExample.java
    4 {{{
    5 public class ConditionalExample {
    6     public static void main(String args[]) {
    7         int rangeInInches;
    8 
    9         rangeInInches = 10;
    10 
    11         if (rangeInInches < 20) {
    12            System.out.println("stop motors so we don't crash!");
    13         } else {
    14            System.out.println("full speed ahead!");
    15         }
    16     }
    17 }
    18 }}}
    19 * Save the program as !ConditionalExample.java (notice how the name of the file must match the name of the class)
    20 * Run the program using the debugger with a breakpoint set on the first executable line (rangeInInches=10); step through the program using the stepover tool and observe the flow of the program.
    21 * What happens if you change the values stored in the variable rangeInInches to 25?
    22 
    231== Loops
    242It's also important for a program to be able to do things repeatedly; for example, the robot must read its sensors repeatedly and react only when certain conditions are met.  Consider (and try running) the following program: