lesson_06.1

lesson_06.1 - Control Structures Introduction Decision...

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

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

View Full DocumentRight 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.
Background 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.
Background image of page 3

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

View Full DocumentRight 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
Background 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.
Background image of page 5

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

View Full DocumentRight 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
Background 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(); }
Background image of page 7

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

View Full DocumentRight 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. Write a class that accepts at minimum, the name, age, and the state of residence.
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 34

lesson_06.1 - Control Structures Introduction Decision...

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

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