03ControlSmall

03ControlSmall - Lecture Set#3 Conditional and Iterative Structures Control Structures uninitialized variables if branching if else branching

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

View Full Document Right Arrow Icon
1 CMSC 131 Fal 2009 Jan Plane (adapted from Bonnie Dorr) Lecture Set #3: Conditional and Iterative Structures 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 Fal 2009 Jan Plane (adapted from Bonnie Dorr) 1 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 CMSC 131 Fal 2009 Jan Plane (adapted from Bonnie Dorr) 2 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
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 Fal 2009 Jan Plane (adapted from Bonnie Dorr) 3 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 Fal 2009 Jan Plane (adapted from Bonnie Dorr) 4 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 ” ); } CMSC 131 Fal 2009 Jan Plane (adapted from Bonnie Dorr) 5 Blocks c What happens? if (i > 10) i = 10; saturate = true; c Desired: both i , saturate are set only when i > 10 c Actual: only the i=10 statement is dependant c Only one statement can be associated with if c The saturate assignment statement is not part of the if c Blocks solve this problem
Background image of page 2
3 CMSC 131 Fal 2009 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.

This note was uploaded on 12/04/2011 for the course CMSC 131 taught by Professor Plane during the Fall '08 term at Maryland.

Page1 / 7

03ControlSmall - Lecture Set#3 Conditional and Iterative Structures Control Structures uninitialized variables if branching if else branching

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