SelectionNew - Selection Statements CS 180 Sunil Prabhakar...

Info iconThis 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
Background image of page 1

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

View Full DocumentRight Arrow Icon
© Sunil Prabhakar Problem n 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. n Your program should read in the measure and output an appropriate evaluation. 2 Thursday, February 9, 2012
Background image of page 2
© Sunil Prabhakar Choices n Clearly, in order to solve this problem, we need to be able to choose which of the alternative messages to print. n All programming languages provide this ability to choose: selection statements n Java provides if-else and switch statements. 3 Thursday, February 9, 2012
Background image of page 3

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

View Full DocumentRight Arrow Icon
© Sunil Prabhakar 4 Flow of control n Once a statement is executed, the next statement of the program is executed. n Calling a method transfers the control to the statements in the method. n Once the method returns, control returns to statement that made the call. n Changing this flow of control is achieved using if and switch (and other) statements. n These are called control flow statements. Thursday, February 9, 2012
Background 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
Background image of page 5

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

View Full DocumentRight 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
Background image of page 6
© Sunil Prabhakar if-else syntax n The boolean expression is a special type of expression which can have one of two values: true or false values n If the expression evaluates to true , the if- statement is executed; otherwise n the else-statement is executed. 7 if ( <boolean expression> ) if- statement; else else- statement; Thursday, February 9, 2012
Background image of page 7

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

View Full DocumentRight Arrow Icon
© Sunil Prabhakar Multiple conditional statements n We can have multiple statements for the if and/or else branches. n 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
Background 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
Background image of page 9

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

View Full DocumentRight 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( "Your cholesterol level is too high." ); System.out.println (
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/19/2012 for the course CS 180 taught by Professor Staff during the Spring '08 term at Purdue University-West Lafayette.

Page1 / 69

SelectionNew - Selection Statements CS 180 Sunil Prabhakar...

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

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