Tutorial02 - Tutorial#2 Selection(Ch 2 OPTIONAL...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
Tutorial #2: Selection (Ch 2) O P T I O N A L Introduction: This tutorial is recommended if you have limited experience using “if” statements or the Scanner class for input. You will enter several forms of “if” statements that select particular program paths based on boolean conditions. You will get to experiment with blocks and indentation. Feel free to try your own experiments. 1. Enter the following program and run it three times using the following input values: 0, 1, and 2, respectively. Be sure to include the “import” line at the top for using the Scanner. Notice the indentation for lines 10 and 11. 1 2 3 4 5 6 7 8 9 10 11 12 13 import java.util.Scanner; public class Tutorial02 { public static void main(String args[]) { Scanner scanner = new Scanner(System.in); int num; System.out.print("Enter a number: "); num = scanner.nextInt(); if (num <= 1) System.out.println("True path"); System.out.println("Extra line"); } } 2. Replace lines 9 – 11 with the following code and rerun the program using input values 0, 1, and 2. Both
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Ask a homework question - tutors are online