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

Info icon This 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 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 Spring 2007 Jan Plane (adapted from Bonnie Dorr) 1 Java, Eclipse and Uninitialized Variables circle6 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? circle6 This feature interacts strangely with if/else statements sometimes circle6 Good programming practice: always initialize new variables CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr) 2 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
Image of page 1

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

View Full Document Right 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: 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 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. 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 ” ); }
Image of page 2
3 CMSC 131 Spring 2007 Jan Plane (adapted from Bonnie Dorr) 6 Blocks circle6 What happens?
Image of page 3

Info iconThis 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