CSC 313 Java Control Structures - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

CSC 313 Java Control Structures

Description:

Java has a sequence structure 'built-in' Java provides three selection structures ... CSC 313 Slides by L.M. Hicks. page27. End of Loopy Java! Have a Nice Weekend! ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 28
Provided by: gar59
Category:

less

Transcript and Presenter's Notes

Title: CSC 313 Java Control Structures


1
CSC 313 Java Control Structures
2
Selection Structures
  • Java has a sequence structure built-in
  • Java provides three selection structures
  • if
  • if/else
  • switch
  • Java provides three repetition structures
  • while
  • do/while
  • do
  • Each of these words is a Java keyword

3
Decision Making
  • if control structure
  • Simple version in this section, more detail later
  • If a condition is true, then the body of the if
    statement executed
  • 0 interpreted as false, non-zero is true
  • Control always resumes after the if structure
  • Conditions for if structures can be formed using
    equality or relational operators (next slide)
  • if ( condition )
  • statement executed if condition true
  • No semicolon needed after condition
  • Else conditional task not performed

4
The if Statement
  • The if statement, in its simplest
    configuration, takes this form
  • if (expression)
  • statement next statement

5
The syntax of the if/else Statement
6
The if Statement without else
7
The else Clause
8
An if Statement with an else clause
9
An if Statement with an else if and else clause
10
Nested if Statements
11
The Conditional Operator
  • The conditional operator is a notational variant
    on certain forms of the if-else statement
  • Also called the ternary operator or arithmetic if
  • The following examples are equivalent
  • if (n1 gt n2) max n1
  • else max n2
  • vs.
  • max (n1 gt n2) ? n1 n2
  • The expression to the right of the assignment
    operator is a conditional operator expression
  • If the Boolean expression is true, then the
    expression evaluates to the value of the first
    expression (n1), otherwise it evaluates to the
    value of the second expression (n2)

12
The while Repetition Structure
  • Repeat action while condition remains true

13
The Syntax of the while loop
14
While Loop
15
Statement Blocks
  • In general, wherever you can have one executable
    statement in Java, you can replace it with a
    block of statements enclosed between braces
    instead.
  • if(expression)
  • statement 1
  • statement 2
  • ...

16
The switch Statement
17
The switch Statement no break
18
Loop Instructions
19
while loop
  • The while loop
  • int i 0
  • final int n 100
  • while ( i lt n )
  • System.out.println( i )
  • i i 1
  • prints 0,1,,99.

20
do while loop
  • The do while loop
  • int i 0
  • final int n 100
  • do
  • System.out.println( i )
  • i i 1
  • while ( i lt n )
  • prints 0,1,,99.

21
for loops
22
Labeled continue Statement
  • Where you have nested loops, there's a special
    form of the continue statement that enables you
    to stop executing the inner loop-not just the
    current iteration of the inner loop-and continue
    at the beginning of the next iteration of the
    outer loop.
  • In general, you can use the labeled continue to
    exit from an inner loop to any enclosing outer
    loop, not just the one immediately enclosing the
    loop containing the labeled continue statement.

23
Labeled continue illustrated
  • public class Factorial
  • public static void main(String args)
  • long limit 20 // Calculate
    factorial of integers up to this value
  • long factorial 1 // Calculate
    factorial in this variable
  • // Loop from 1 to the value of limit
  • OuterLoop
  • for(int i 1 i lt limit i)
  • factorial 1 //
    Initialize factorial
  • for(int j 2 j lt i j)
  • if(i gt 10 i 2 1)
  • continue OuterLoop // Transfer
    to the outer loop
  • factorial j
  • System.out.println("!" i " is "
    factorial)

24
the break Statement in a Loop
25
Variable Scope
  • A variable doesn't exist before its declaration,
    so you can only refer to it after it has been
    declared.
  • It continues to exist until the end of the
    block in which it is defined, and that includes
    any blocks nested within the block containing
    its declaration.

26
Variable Scope Illustrated
27
End of Loopy Java!
Have a Nice Weekend!!
Write a Comment
User Comments (0)
About PowerShow.com