
































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

































Lecture 2 User inputs and conditionals Dr. Milad Ghantous Spring 2023 GUC
The Scanner class
Step 2 (^) int x = sc.nextInt();
Can we only read integers?
Entering Strings
Examples: Strings Scanner sc = new Scanner(System.in); System.out.print("Enter a value please:"); String x = sc.next(); System.out.println("you entered " + x); Scenario 1 Scenario 2 Scanner sc = new Scanner(System.in); System.out.print("Enter a value please:"); String x = sc. nextLine (); System.out.println("you entered " + x); Scenario 1 Scenario 2
if/else
if/else syntax if (condition) {
} if (condition) { // your then statement(s) } else { // your else statement(s) }
Complex Conditions
Examples Can also be written this way Can also be written this way Why does this work you think?
Examples
Tricky cases
Refactoring if conditionals Case 2 Example 1
Refactoring if conditionals Case 2 Example 2 Are those equivalent?