int x = 3; if (x > 1) if (x > 2) { if (x > 3) System.out.println("one"); } else System.out.println("three"); else System.out.println("four"); Let’s think about this code. What does it do, how does it work?
int x = 3; if (x > 1) if (x > 2) { if (x > 3) System.out.println("one"); } else System.out.println("three"); else System.out.println("four"); x = 3 We visualize the instructions one by one. First x gets created and is assigned a value.
int x = 3; if (x > 1) if (x > 2) { if (x > 3) System.out.println("one"); } else System.out.println("three"); else System.out.println("four"); x > 1 x = 3 true We continue by drawing the flowchart. It will be our map as we walk the code line by line.
int x = 3; if (x > 1) if (x > 2) { if (x > 3) System.out.println("one"); } else System.out.println("three"); else System.out.println("four"); x > 1 x = 3 x > 2 true
int x = 3; if (x > 1) if (x > 2) { if (x > 3) System.out.println("one"); } else System.out.println("three"); else System.out.println("four"); x > 1 x = 3 x > 2 { true
int x = 3; if (x > 1) if (x > 2) { if (x > 3) System.out.println("one"); } else System.out.println("three"); else System.out.println("four"); x > 1 x = 3 x > 3 x > 2 { true
int x = 3; if (x > 1) if (x > 2) { if (x > 3) System.out.println("one"); } else System.out.println("three"); else System.out.println("four"); x > 1 x = 3 x > 3 x > 2 “one” { true
int x = 3; if (x > 1) if (x > 2) { if (x > 3) System.out.println("one"); } else System.out.println("three"); else System.out.println("four"); x > 1 x = 3 x > 3 x > 2 “one” { } true
int x = 3; if (x > 1) if (x > 2) { if (x > 3) System.out.println("one"); } else System.out.println("three"); else System.out.println("four"); x > 1 x = 3 x > 3 x > 2 “one” { } true
int x = 3; if (x > 1) if (x > 2) { if (x > 3) System.out.println("one"); } else System.out.println("three"); else System.out.println("four"); x > 1 x = 3 x > 3 x > 2 “one” { } true
int x = 3; if (x > 1) if (x > 2) { if (x > 3) System.out.println("one"); } else System.out.println("three"); else System.out.println("four"); x > 1 x = 3 x > 3 x > 2 “one” { } true
int x = 3; if (x > 1) if (x > 2) { if (x > 3) System.out.println("one"); } else System.out.println("three"); else System.out.println("four"); x > 1 x = 3 x > 3 x > 2 “three” “one” { } true
int x = 3; if (x > 1) if (x > 2) { if (x > 3) System.out.println("one"); } else System.out.println("three"); else System.out.println("four"); x > 1 x = 3 x > 3 x > 2 “three” “one” { } true
int x = 3; if (x > 1) if (x > 2) { if (x > 3) System.out.println("one"); } else System.out.println("three"); else System.out.println("four"); x > 1 x = 3 x > 3 x > 2 “three” “one” { } true
int x = 3; if (x > 1) if (x > 2) { if (x > 3) System.out.println("one"); } else System.out.println("three"); else System.out.println("four"); x > 1 x = 3 x > 3 x > 2 “three” “one” { } true
int x = 3; if (x > 1) if (x > 2) { if (x > 3) System.out.println("one"); } else System.out.println("three"); else System.out.println("four"); x > 1 x = 3 x > 3 x > 2 “three” “one” “four” { } true
int x = 3; if (x > 1) if (x > 2) { if (x > 3) System.out.println("one"); } else System.out.println("three"); else System.out.println("four"); x > 1 x = 3 x > 3 x > 2 “three” “one” “four” { } true
int x = 3; if (x > 1) if (x > 2) { if (x > 3) System.out.println("one"); } else System.out.println("three"); else System.out.println("four"); x > 1 x = 3 x > 3 x > 2 “three” “one” “four” { } The end. true
int x = 3; if (x > 1) if (x > 2) { if (x > 3) System.out.println("one"); } else System.out.println("three"); else System.out.println("four"); x > 1 x = 3 x > 3 x > 2 “three” “one” “four” { } The end. Nothing gets printed. true
int x = 3; if (x > 1) if (x > 2) { if (x > 3) System.out.println("one"); } else System.out.println("three"); else System.out.println("four"); Old problem
int x = 3; if (x > 1) { if (x > 2) if (x > 3) System.out.println("one"); } else System.out.println("three"); else System.out.println("four"); New problem
int x = 3; if (x > 1) if (x > 2) { if (x > 3) System.out.println("one"); } else System.out.println("three"); else System.out.println("four"); Old problem
int x = 3; if (x > 1) { if (x > 2) if (x > 3) System.out.println("one"); } else System.out.println("three"); else System.out.println("four"); New problem
int x = 3; if (x > 1) if (x > 2) { if (x > 3) System.out.println("one"); } else System.out.println("three"); else System.out.println("four"); Old problem
int x = 3; if (x > 1) { if (x > 2) if (x > 3) System.out.println("one"); } else System.out.println("three"); else System.out.println("four"); New problem
int x = 3; if (x > 1) { if (x > 2) if (x > 3) System.out.println("one"); } else System.out.println("three"); else System.out.println("four"); New problem else w/out if Does not compile!
int x = 3; if (x > 1) { if (x > 2) if (x > 3) System.out.println("one"); } else System.out.println("three"); else System.out.println("four"); New problem else w/out if Does not compile!
int x = 3; if (x > 1) { if (x > 2) if (x > 3) System.out.println("one"); } else System.out.println("three"); else System.out.println("four"); New problem else w/out if Does not compile! The only solution, then, is to carefully draw the flowchart!
int x = 3; if (x > 1) { if (x > 2) if (x > 3) System.out.println("one"); } else System.out.println("three"); else System.out.println("four"); New problem else w/out if Does not compile! The only solution, then, is to carefully draw the flowchart! On paper, or in your mind, it doesn’t matter. There really isn’t any reliable, uniform shortcut alternative to that.
int x = 3; if (x > 1) { if (x > 2) if (x > 3) System.out.println("one"); } else System.out.println("three"); else System.out.println("four"); New problem else w/out if Does not compile! The only solution, then, is to carefully draw the flowchart! On paper, or in your mind, it doesn’t matter. There really isn’t any reliable, uniform shortcut alternative to that.