{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture 2 - Selection - Decision making and selection...

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

View Full Document Right Arrow Icon
Lecture 2 Control Statements - Selection Decision making and selection z Programming languages like Java provide language constructs for making decisions in a program z Until now, our simple programs have been sequential z These programs begin with the first statement of method main() and perform successive statements in order before finishing z Decision making constructs allow you to take different paths through your programs z The values of variables determine the path taken z Selection control structures allow program to select one of multiple execution paths based on decisions Making choices Very often you will want your programs to make choices among different groups of instructions. An example A program processing requests for airline tickets could have the following choices to make: – display the price of the seats requested; – display a list of alternative flights; – display a message saying that no flights are available to that destination. Selection , is a method of program control in which a choice can be made among which instructions to execute. Implementing selection in Java In Java there are three forms of selection you can use: – an if statement; – an if…else statement; – a switch statement. The if statement Sometimes one or more instructions need to be guarded so that they are executed only when appropriate; This particular form of selection is implemented by making use of Java’s if statement; The general form of an if statement is given as follows: The if Statement: flowchart z If the boolean expression evaluates to true, the statement will be executed. Otherwise, it will be skipped. boolean expression statement true false if ( boolean expression ) statement ;
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
Tests – boolean expressions A test is any boolean expression that gives a result of true or false .
Image of page 2
Image of page 3
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