lesson_06.1 - Control Structures Introduction Decision...

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

View Full Document Right Arrow Icon
Control Structures Introduction Decision Statements The if Construct The if .. else Construct The switch Statement Iterative Constructs The while Construct The do .. while Construct The for Construct
Image of page 1

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

View Full Document Right Arrow Icon
Control Structures Objectives To understand what control structures are and the need for them To establish and understand the three categories of program control flow selection, iteration and exception. To understand selection statements if and switch - as means of controlling the program flow. To understand iterative statements while, do/while, and for as means of controlling the program flow.
Image of page 2
Control Structures Program statements are executed in a linear fashion, unless otherwise specified. That is: Execution begins from the first statement to the last No statement is skipped, and None is repeated The calling of methods: Does not constitute an altering of the control flow Is an expansion of the statement call replaced by the method definition. Control structures alter the flow of the statements in a piece of code. The flow of execution can be altered a) Selection statements or b) Iterative statements.
Image of page 3

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

View Full Document Right Arrow Icon
Selection Statements Selection statements allow you to choose between statements The choice is based on the result of a conditional expression. There are two basic selection statements: The if statement, and The switch statement
Image of page 4
The if Statement The if statement has two forms, namely: One without an alternate choice, and The other with an alternate choice The general format of an if statement without an alternate choice is as follows: if ( conditional_expression ) a_single_statement; or if ( conditional_expression ) { Two or more statements; } Interpretation - if the conditional expression evaluates to true then the statement block is executed; failing that, the block is skipped.
Image of page 5

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

View Full Document Right Arrow Icon
The if Statement if ( 30 > 25 ) System.out.println(“That is correct”); Note: if is the keyword, and 30 > 25 is the relational expression that is being tested. The condition is evaluated to true, and hence the print statement is executed if ( 30 > 25 ) { System.out.println(“That is correct”); System.out.println(); } The conditional expression evaluates to true, hence the statement block is executed
Image of page 6
The if Statement The if statement must be viewed as one complete thought In both cases - with the single statement as well as with block of statements. if ( 30 > 25 ) System.out.println(“That is correct”); Is the same as saying: if ( 30 > 25 ) System.out.println(“That is correct”); The second case could have been written as: if ( 30 > 25 ) { System.out.println(“That is correct”); System.out.println(); }
Image of page 7

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

View Full Document Right Arrow Icon
Example A Florida company wants to offer scholarship to some needy students. Preference is given to Florida residence who fall in the age range of 18 years and 25 years.
Image of page 8
Image of page 9
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