L07-Selection1 - King Fahd University of Petroleum &...

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

View Full Document Right Arrow Icon
ICS102 Lecture 7: Selection Structures Information & Computer Science Department
Background image of page 1

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

View Full DocumentRight Arrow Icon
December 6, 2010 ICS102: The course 2 Outline Block statement Branching Statements Simple if statement if-else statement if-else-elseif statement switch statement  Nested if statements
Background image of page 2
Introduction to selection structures Write a program that accepts an integer value from  the user and then: prints “positive” if the integer is larger or equal to zero Prints “negative” if the integer is less than zero
Background image of page 3

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

View Full DocumentRight Arrow Icon
December 6, 2010 ICS102: The course 4 Block Statement A block statement consists of one or more Java statements  enclosed in braces. Example of a block statement:      { statement 1; statement 2; statement n; } Blocks can be nested. A block statement can be used anywhere that a single statement  can be used.
Background image of page 4
December 6, 2010 ICS102: The course 5 Branching Statement A branching statement consists of one or more block  statements The execution of a block statement in a branching statement is  controlled by a boolean expression which we call a condition. There are mainly the following four types of branching  statement. Simple if If-else If-elseif-else switch
Background image of page 5

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

View Full DocumentRight Arrow Icon
December 6, 2010 ICS102: The course 6 Simple if Statement … Simple if statement has the following structure: if ( <boolean_expression> ) { <then_block> } The  boolean_expression  must be enclosed in parentheses If the  boolean_expression  is  true , then the  then_block  is executed.  Otherwise it will NOT execute. if ( testScore >= 95 ) { System.out.println("You are a good student"); } Then Block Boolean Expression
Background image of page 6
December 6, 2010 ICS102: The course Chapter 6 - 7 -- Simple if Statement … testScore >= 95? false System.out.println ("You are a good student"); true Control Flow of if:
Background image of page 7

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

View Full DocumentRight Arrow Icon
December 6, 2010 ICS102: The course 8 … -- Simple if Statement  Example: Design and write a Java program prints the absolute  value of a number. import java.util.Scanner; class absolute { public static void main(String [] args) { Scanner kb = new Scanner(System.in); System.out.print(“Enter a number: “); double x = kb.nextDouble(); double y = x; if( y < 0) { y = -y; } System.out.print(“The absolute value of “ + x + “ is “ + y); } }         
Background image of page 8
December 6, 2010 ICS102: The course 9 -- if-else Statement …
Background image of page 9

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

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/05/2010 for the course ICS 102 taught by Professor Zaman during the Fall '08 term at King Fahd University of Petroleum & Minerals.

Page1 / 29

L07-Selection1 - King Fahd University of Petroleum &...

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

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