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

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

ICS102 Lecture 7: Selection Structures King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department
Image of page 1

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

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
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
Image of page 3

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

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.
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
Image of page 5

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

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
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:
Image of page 7

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

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); } }         
Image of page 8
December 6, 2010 ICS102: The course 9
Image of page 9

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

Image of page 10
This is the end of the preview. Sign up to access the rest of the document.
  • Fall '08
  • ZAMAN
  • Expression, System.out.println, Computer Science Department, King Fahd University of Petroleum & Minerals College of Computer Science & Engineering

{[ 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