Sunteți pe pagina 1din 53

Control Structures

Java Boot Camp


Module 1

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Objectives
 At the end of the lesson the student should be
able to:
 Use decision control structures (if, else, switch)
which allow selection of specific sections of code
for execution.
 Use repetition control structures (while, do-while,
for) which allow executing specific sections of code
repeatedly.
 Use branching statements (break, continue, return)
which allow redirection of program flow

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Control Structures
 Control structures
 allow us to change the ordering of how the
statements in our programs are executed
 Two types of Control Structures
 decision control structures
 allow us to select specific sections of code for
execution
 repetition control structures
 allow us to execute specific sections of the code
repeatedly

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Decision Control
Structures
 Decision control structures
 Java statements that allow us to select and
execute specific blocks of code while skipping
other sections
 Types:
 if-statement
 if-else-statement
 If-else if-statement

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
if-statement
 if-statement
 specifies that a statement (or block of code) will be
executed if and only if a specified boolean statement
is true.
 if-statement has the form

or

where boolean_expression is either a boolean


expression or boolean variable.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
if-statement Flowchart

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Example 1

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Example 2

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Coding Guidelines
 The boolean_expression part of a
statement should evaluate to a boolean
value. That means that the specified
condition should result in a value of
either true or false.
 Indent the statements inside the if-block.
 For example

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
if-else statement
 if-else statement
 used when we want to execute one statement if a
condition evaluates to true, and a different statement
if the condition evaluates to false.
 if-else statement has the form

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Flowchart

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Example 1

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Example 2

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Coding Guidelines
 To avoid confusion, always place the statement
or statements of an if or if-else block inside curly
braces { }.
 You can have nested if-else blocks. You can
have if-else blocks inside an if-else block.
 For example

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
if-else-else if statement
 The statement in the else-clause of an if-
else block can be another if-else structure.
 This cascading of structures allows us to
make more complex selections.
 The statement has the form

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Flowchart

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Example

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Common Errors
 The condition inside the if-statement does not
evaluate to a boolean value.
For example:

The variable number does not hold a boolean


value.
 Writing elseif instead of else if.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Common Errors
 Using = instead of == for comparison.
For example

This should be written as

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Sample Program

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
switch statement
 switch
 allows branching on multiple outcomes.
 switch statement has the form:

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
switch statement
 where,
 switch_expression
 is a byte, short, char, or int primitive data type
 is a Character, Byte, Short, and Integer object
 is an enumerated type (enum)
 case_selector1, case_selector2 and so on,
 are unique integer or character constants
 ... or are the elements of the same enum type as
the switch_expression.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
switch statement
 When a switch is encountered,
 Java first evaluates the switch_expression, and
jumps to the case whose selector matches the value
of the expression.
 The program executes the statements in order from
that point on until a break statement is encountered,
skipping then to the first statement after the end of
the switch structure.
 If none of the cases are satisfied, the default block is
executed. Take note however, that the default part is
optional.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
switch-statement
 NOTE:
 Unlike with the if statement, the multiple statements
are executed in the switch statement without needing
the curly braces.
 When a case in a switch statement has been
matched, all the statements associated with that case
are executed. Not only that, the statements
associated with the succeeding cases are also
executed.
 To prevent the program from executing statements in
the subsequent cases, we use a break statement as
our last statement.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Flowchart

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Example

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Coding Guidelines
 Deciding between an if statement or a switch
statement is a judgment call. You can decide
which to use based on readability and other
factors.
 An if statement can be used to make decisions
based on ranges of values or conditions, whereas
a switch statement can make decisions based
only on a single integer, character, or enum value.
Also, the value provided to each case statement
must be unique.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Repetition Control
Structures
 Repetition control structures
 are Java statements that allow us to execute
specific blocks of code repeatedly.
 Types:
 while-loop
 do-while loop
 for-loop

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
while-loop
 while loop
 is a statement or block of statements that is repeated
as long as some condition is satisfied.
 while loop has the form:

 The statements inside the while loop are executed as


long as the boolean_expression evaluates to
true.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Example 1

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Example 2

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Example 3

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
do-while-loop
 do-while loop
 is similar to the while-loop
 statements inside a do-while loop are repeatedly
executed as long as the condition is satisfied
 The main difference between a while and do-while loop
is that the statements inside a do-while loop are
executed at least once.
 do-while loop has the form:

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Example 1

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Example 2

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Example 3

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Coding Guidelines
 A common programming mistake when
using the do-while loop is forgetting to put
the semi-colon after the while expression.

 Just like in while loops, make sure that


your do-while loops will terminate at some
point

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Coding Guidelines
 Do-while loops are actually discouraged
because they are error-prone. You can
always re-write a do-while loop into code
that uses a while loop instead.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
for-loop
 for loop
 allows execution of the same code a specified
number of times.
 for loop has the form:

where,
 InitializationExpression -initializes the loop variable.
 LoopCondition - compares the loop variable to some limit
value.
 StepExpression - updates the loop variable.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Example

 The code shown above is equivalent to


the following while loop.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Branching Statements
 Branching statements allow us to redirect
the flow of program execution.
 Java provides three branching
statements:
 break
 continue
 return

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Unlabeled break
statement
 unlabeled break
 terminates the enclosing statement, and flow
of control transfers to the immediately
following statement.
 This can also be used to terminate a for,
while, or do-while loop

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Example

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
labeled break statement
 labeled break statement
 terminates an outer statement, which is identified by
the label specified in the break statement.
 the flow of control transfers to the statement
immediately following the labeled (terminated)
statement.
 The sample program in the next slide searches for a
value in a two-dimensional array. Two nested for
loops traverse the array. When the value is found, a
labeled break terminates the statement labeled
search, which is the outer for loop.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Example

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Unlabeled continue
statement
 unlabeled continue statement
 skips to the end of the innermost loop's body
and evaluates the boolean expression that
controls the loop, basically skipping the
remainder of this iteration of the loop.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Example

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Labeled continue
statement
 labeled continue statement
 skips the current iteration of an outer loop
marked with the given label.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Example

 In this example, message 2 never gets printed


since we have the statement continue outerloop
which skips the iteration.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
return statement
 return statement
 used to exit from the current method.
 flow of control returns to the statement that
follows the original method call.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
return statement
 To return a value
 simply put the value (or an expression that
calculates the value) after the return keyword.
 For example,
return ++count;
 or

return "Hello";
 The data type of the value returned by return
must match the type of the method's declared
return value.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
return statement
 When a method is declared void, use the
form of return that doesn't return a value.
 For example,
return;
 We will cover more about return
statements later when we discuss about
methods.

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved
Summary
 Decision Control  Branching Statements
Structures  break
 if  continue
 if-else  return
 if – else if
 switch
 Repetition Control
Structures
 while
 do-while
 for

Copyright 2008 Orange & Bronze Software Labs Ltd.Co. All Rights Reserved

S-ar putea să vă placă și