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