{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

SelectionNew - Selection Statements CS 180 Sunil Prabhakar...

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

View Full Document Right Arrow Icon
Selection Statements CS 180 Sunil Prabhakar Department of Computer Science Purdue University Thursday, February 9, 2012
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
© Sunil Prabhakar Problem Write a program that tells a patient if their total cholesterol measure is too high or not. ¡ਟ The measure is an integer and is too high if it exceeds 239. Your program should read in the measure and output an appropriate evaluation. 2 Thursday, February 9, 2012
Image of page 2
© Sunil Prabhakar Choices Clearly, in order to solve this problem, we need to be able to choose which of the alternative messages to print. All programming languages provide this ability to choose: selection statements Java provides if-else and switch statements. 3 Thursday, February 9, 2012
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
© Sunil Prabhakar 4 Flow of control Once a statement is executed, the next statement of the program is executed. Calling a method transfers the control to the statements in the method. Once the method returns, control returns to statement that made the call. Changing this flow of control is achieved using if and switch (and other) statements. These are called control flow statements. Thursday, February 9, 2012
Image of page 4
© Sunil Prabhakar Solution 5 public class CholesterolCheck { public static void main(String[] args){ int chLevel; chLevel = Integer.parseInt(JOptionPane.showInputDialog( null , "Enter your cholesterol measure" )); if (chLevel > 239) System.out.print( "Your cholesterol level is too high." ); else System.out.print( "Your cholesterol level is not too high." ); } } Thursday, February 9, 2012
Image of page 5

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

View Full Document Right Arrow Icon
6 if-else Control Flow previous statement if (chLevel > 239) System.out.print( ". . . is too high." ); else System.out.print( ". . l is not too high." ); is chLevel >239 ? true false next statement Depending upon the value of chLevel, one or the other branch is executed, not both. System.out.print( "... is too high." ); System.out.print( "... is not too high." ); Thursday, February 9, 2012
Image of page 6
© Sunil Prabhakar if-else syntax The boolean expression is a special type of expression which can have one of two values: true or false values If the expression evaluates to true , the if- statement is executed; otherwise the else-statement is executed. 7 if ( <boolean expression> ) if- statement; else else- statement; Thursday, February 9, 2012
Image of page 7

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

View Full Document Right Arrow Icon
© Sunil Prabhakar Multiple conditional statements We can have multiple statements for the if and/or else branches. Braces are used to combine multiple statements into a single block. 8 if ( <boolean expression> ) { if-statement1; if-statement2; ... } else { else- statement1; else-statement2; else-statement3; ... } Then block Else block Thursday, February 9, 2012
Image of page 8
9 if-else Blocks Control Flow previous statement if ( <boolean expression> ) { if-statement1; if-statement2; } else { else-statement1; else-statement2; else-statement3; } ? true false next statement if-statement1; if-statement2; else-statement1; else-statement2; else-statement3; Thursday, February 9, 2012
Image of page 9

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

View Full Document Right Arrow Icon
© Sunil Prabhakar Solution 10 public class CholesterolCheck { public static void main(String[] args){ int chLevel; chLevel = Integer.parseInt(JOptionPane.showInputDialog( null , "Enter your cholesterol measure" )); if (chLevel > 239) { System.out.println(
Image of page 10
Image of page 11
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