{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

03ControlSmall - Lecture Set#3 Conditional and Iterative...

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

View Full Document Right Arrow Icon
1 CMSC 131 Fall 2009 Jan Plane (adapted from Bonnie Dorr) Lecture Set #3: Conditional and Iterative Structures Control Structures circle6 uninitialized variables circle6 if branching circle6 if / else branching circle6 logical operators circle6 nesting of control structures circle6 proper indenting and spacing conventions circle6 java identifier naming conventions circle6 named constants circle6 while loop circle6 do-while loop circle6 for loop CMSC 131 Fall 2009 Jan Plane (adapted from Bonnie Dorr) 1 Control Flow and Conditionals circle6 Control flow: the order in which statements are executed circle6 General rule: top to bottom circle6 Several Control Structures that change that circle6 Conditional statements : permit control flow to be dependent on (true/false) conditions circle6 if circle6 if-else CMSC 131 Fall 2009 Jan Plane (adapted from Bonnie Dorr) 2 if and if-else The if and if-else statements should have the following form: circle6 if ( condition ) { statements ; } circle6 tests the condition circle6 if true statement is done; otherwise it is skipped circle6 if ( condition ) { statements1 ; } else { statements2 ; } circle6 tests the condition circle6 if true, statements1 is done; otherwise statements2 is done
Image of page 1

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

View Full Document Right Arrow Icon
2 CMSC 131 Fall 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 Fall 2009 Jan Plane (adapted from Bonnie Dorr) 4 Logical Operators Used for forming more complex conditions. circle6 “and” && if ( temp >= 97 && temp <= 99 ) { System.out.println( “ Patient is healthy ” ); } circle6 “or” || if ( months >= 3 || miles >= 3000 ) { System.out.println( “ Change your oil ” ); } circle6 “not” : ! if ( ! phone.equals( “ 301-555-1212 ” ) ) { System.out.println( “ Sorry, wrong number ” ); } CMSC 131 Fall 2009 Jan Plane (adapted from Bonnie Dorr) 5 Blocks circle6 What happens? if (i > 10) i = 10; saturate = true; circle6 Desired: both i , saturate are set only when i > 10 circle6 Actual: only the i=10 statement is dependant circle6 Only one statement can be associated with if circle6 The saturate assignment statement is not part of the if circle6 Blocks solve this problem
Image of page 2
3 CMSC 131 Fall 2009 Jan Plane (adapted from Bonnie Dorr) 6 Blocks circle6 What happens?
Image of page 3

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern