Loops, Conditionals, Scope - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Loops, Conditionals, Scope

Description:

or new String('Hello'); String badWord = greeting.substring(0,4) ... System.out.println('Error cannot ' 'take the square root of a negative ' 'number. ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 16
Provided by: jacobh
Category:

less

Transcript and Presenter's Notes

Title: Loops, Conditionals, Scope


1
Loops, Conditionals, Scope
  • new
  • 2) primitive
  • 3) /Many possible answers see below for
    examples/
  • int numEnrolled
  • double gpaBoost
  • boolean isAP
  • String courseName
  • 4) apCompSciAPer1.add(Larry Jones)
  • 5) String greeting Hello there! // or new
    String(Hello)
  • String badWord greeting.substring(0,4)
  • String capitalGreeting greeting.toUpperCase
    ()
  • 6) Encapsulation

2
Boolean expressions
  • Any expression that can be simplified to true or
    false.
  • Often use
  • lt gt lt gt, , !
  • , , !
  • Example
  • x gt -2 x lt 10
  • or
  • (x gt -2) (x lt 10)
  • (Use parentheses for clarity)

3
If statements
  • if (x gt 0)
  • y Math.sqrt(x)
  • else
  • System.out.println(Error cannot take
    the square root of a negative number.)
  • if (agegt0 age lt 18)
  • prsn.setMinor(true)
  • if (age gt 16)
  • prsn.setCanDrive(true)
  • else
  • prsn.setCanDrive(false)
  • if (age gt 18)
  • if (prsn.getIsCitizen())
  • prsn.setCanVote(true)
  • else
  • prsn.setCanVote(false)

4
if-else statements
  • if (x lt 0)
  • System.out.println(x is negative)
  • else if (x gt 0)
  • System.out.println(x is positive)
  • else
  • System.out.println(x is zero.)

5
while loops
  • What is the output of the following code segment?
  • int x 12
  • int y 1
  • while (x gt 0)
  • x - y
  • y
  • System.out.println(xy)
  • Output 3

6
while loops
  • What is the output of the following code segment?
  • int x -1
  • int y 3
  • while (x gt 0)
  • x - y
  • y
  • System.out.println(xy)
  • Output 2

7
while loops
  • What does this loop do?
  • int mystryNum (int)100Math.random() 1
  • boolean finished false
  • while (!finished)
  • if (mystryNum 2 0)
  • mystryNum / 2
  • else
  • finished true

8
do-while loops
  • What is the output of the following code segment?
  • int x -1
  • int y 3
  • do
  • x - y
  • y
  • while (x gt 0)
  • System.out.println(xy)
  • Output 0

9
Variable Scope
  • Scope
  • the inner-most block of code in which a variable
    is defined.
  • The variable does not exist outside this block.
  • The garbage-collector erases variables as soon as
    they fall out-of-scope.
  • int x 1
  • while (x lt 10)
  • int y 2
  • if (x gt y)
  • int z x y
  • y z
  • else
  • x z

Output Compiler error cannot find symbol
variable z
10
Variable Scope
  • What is the output of the following code segment?
  • int x 0
  • while (x lt 10)
  • int y 2
  • x y
  • y
  • System.out.println(y)
  • Output Compiler Error
  • cannot find symbol variable y

11
Variable Scope
  • What is the output of the following code segment?
  • int x 0
  • while (x lt 10)
  • int y 2
  • x y
  • y
  • System.out.println(x)
  • Output 10

12
for loops
  • int sum 0
  • for (int x 0 x lt 20 x)
  • sum x
  • System.out.println(sum)
  • Output 190

Declarations Initializations
Condition for continuation
Adjustment after each loop
int x 0
for (
x lt 20
x
)
13
for loops
  • What is the output of the following code segment?
  • for (int x0 x lt 10 x)
  • x x
  • System.out.println(x)
  • Output Compiler error
  • symbol not found
  • variable x

14
for loops
  • What is the output of the following code segment?
  • int x 0
  • for (int y0 x lt 10 yx)
  • x y
  • System.out.println(x)
  • Output Runtime error infinite loop!

15
for loops
  • What is the output of the following code segment?
  • int x 0
  • int y
  • for (y0 x lt 10 y)
  • x y
  • System.out.println(xy)
  • Output 15
Write a Comment
User Comments (0)
About PowerShow.com