03controlSmall

03controlSmall - Lecture Set #3: If Statements Control...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr) Lecture Set #3: If Statements Control Structures c uninitialized variables c if branching c if / else branching c logical operators c nesting of control structures c proper indenting and spacing conventions c java identifier naming conventions c named constants c while loop c do-while loop c for loop CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr) 1 Java, Eclipse and Uninitialized Variables c Eclipse will complain if you try to use an uninitialized local variable: int i; System.out.println ( i is + i); What is value of i? c This feature interacts strangely with if/else statements sometimes c Good programming practice: always initialize new variables CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr) 2 Control Flow and Conditionals c Control flow: the order in which statements are executed c General rule: top to bottom c Several Control Structures that change that c Conditional statements : permit control flow to be dependent on (true/false) conditions c if c if-else
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr) 3 if and if-else The if and if-else statements should have the following form: c if ( condition ) { statements ; } c tests the condition c if true statement is done; otherwise it is skipped c if ( condition ) { statements1 ; } else { statements2 ; } c tests the condition c if true, statements1 is done; otherwise statements2 is done CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr) 4 Java and White Space http://java.sun.com/docs/codeconv/html/CodeConv TOC.doc.html 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) 5 Logical Operators Used for forming more complex conditions. c “and” if ( temp >= 97 temp <= 99 ) { System.out.println( “ Patient is healthy ” ); } c “or” || if ( months >= 3 || miles >= 3000 ) { System.out.println( “ Change your oil ” ); } c “not” : ! if ( ! phone.equals( “ 301-555-1212 ” ) ) { System.out.println( “ Sorry, wrong number ” ); }
Background image of page 2
3 CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr) 6 Blocks c What happens? if (i > 10)
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 8

03controlSmall - Lecture Set #3: If Statements Control...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online