DOC PREVIEW
UMD CMSC 131 - Lecture Set #5: If Statements

This preview shows page 1-2-3-4-5-6 out of 18 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture Set 5 If Statements Last time 1 Variables and types 2 Expressions in Java 3 User input with Scanner objects This set 1 if statements CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr Control Flow and Conditionals Control flow the order in which statements are executed General rule top to bottom Conditional statements permit control flow to be dependent on true false conditions if if else CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 1 The if Statement Form if boolean expression statement Example boolean expression a condition true or false if inchesOfSnow 7 System out println Go home statement The println statement is executed only if the variable inchesOfSnow is greater than 7 Otherwise it is skipped CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 2 Example 6 public class Example6 public static void main String args Scanner sc new Scanner System in System out print Enter an integer int i sc nextInt if i 0 System out println That was a negative number System out println The number was i CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 3 The if else Statement Form if condition statements 1 else statements 2 Example if inchesOfSnow 7 System out println Go home else System out println Go to school If inchesOfSnow 7 the first println statement is executed and the second is skipped Otherwise i e inchesOfSnow 7 the first println statement is skipped and the second is executed CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 4 Indentation Convention for ifelse The if else class of statements should have the following form if condition statements if condition statements else statements CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 5 Blocks What happens if i 10 i 10 saturate true Desired both i saturate are set only when i 10 Actual only the i 10 statement is dependant Only one statement can be associated with if The saturate assignment statement is not part of the if Blocks solve this problem CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 6 Blocks What happens if i 10 i 10 saturate true else k 100 Desired both i saturate are set only when i 10 Actual syntax error Only one statement can be associated with if The saturate assignment statement is not part of the if The else can t find the if it belongs to Blocks solve this problem also CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 7 What Blocks Are Blocks are sequences of statements glued together into one Form statement 1 statement 2 Example if i 10 block i 10 saturate true else block i i 1 CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 8 Indentation Conventions for Blocks Either if statement statement Or if statement statement 1 2 This is what we will use Sun code convention 1 2 CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 9 See Sun Code Conventions on Resource Page http java sun com docs codeconv html CodeConvTOC doc html CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 10 Java and White Space You can add carriage returns spaces tabs wherever you want in Java Properly used this makes your program easier to read and understand CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 11 Example 7 public class Example7 public static void main String args Scanner sc new Scanner System in System out print Enter an integer int i sc nextInt if i 0 System out println That was a negative number else System out println That was a non negative number System out println The number was i CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 12 Logical Operators Used for forming more complex conditions and if temp 97 temp 99 System out println Patient is healthy or if months 3 miles 3000 System out println Change your oil not if phone equals 301 555 1212 System out println Sorry wrong number CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 13 Example 8 public class Example8 public static void main String args Scanner sc new Scanner System in System out print Enter an integer int i sc nextInt if i 0 System out println That was a negative number System out println I prefer positive ones so I ll fix it i i else System out println That was a positive number System out println That makes me happy System out println The number is now i CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 14 Example 9 public class Example9 public static void main String args Scanner sc new Scanner System in System out print Enter an integer from 1 to 10 int i sc nextInt if i 1 i 10 System out println Good job else System out println You didn t follow instructions CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 15 Statement Constructors and Nesting if if else are statement constructors They take statement s and convert them into a new statement Example if i 1 i 10 System out println Good job else System out println Oops Two sub statements come in A single big statement if else comes out Implications if statements etc can also appear inside be nested within one another CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 16 Java Eclipse and Uninitialized Variables Eclipse will complain if you try to use an uninitialized variable int i System out println i is i What is value of i This feature interacts strangely with if else statements sometimes Good programming practice always initialize new variables CMSC 131 Spring 2007 Jan Plane adapted from Bonnie Dorr 17


View Full Document

UMD CMSC 131 - Lecture Set #5: If Statements

Documents in this Course
Set #3

Set #3

7 pages

Exam #1

Exam #1

6 pages

Exam #1

Exam #1

6 pages

Notes

Notes

124 pages

Notes

Notes

124 pages

Load more
Download Lecture Set #5: If Statements
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Set #5: If Statements and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Set #5: If Statements and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?