Selection statements Repetition statements - PowerPoint PPT Presentation

1 / 39
About This Presentation
Title:

Selection statements Repetition statements

Description:

The dependent actions are only performed if the answer is Yes (the condition is true) ... Get up; have breakfast; go off to work; have lunch; do more work; have an ... – PowerPoint PPT presentation

Number of Views:73
Avg rating:3.0/5.0
Slides: 40
Provided by: hien2
Category:

less

Transcript and Presenter's Notes

Title: Selection statements Repetition statements


1
Selection statements Repetition statements
2
Lesson plan
  • Main concepts
  • Practice session
  • If- then
  • Switch
  • Nested if

3
Sequential sequence and Control Statement
  • Sequential sequences are insufficient to solve
    most problems.
  • Get in the Car.
  • Drive to the supermarket.
  • Tank nearly empty?
  • Get in the Car.
  • If we need gas, then fill up.
  • Drive to the supermarket.

4
Dependent Actions
  • A question is asked.
  • A condition is checked.
  • The dependent actions are only performed if the
    answer is Yes (the condition is true).
  • Sometimes the dependent actions are performed,
    and sometimes they are not.

5
Example Logging In
  • Computer access
  • Request a login name from the user.
  • Request a matching password.
  • If the password matches the login name, then
    grant access.

6
Simple Choice Statement
if (ltboolean expressiongt) ltblockgt else
ltblockgt
if (ltboolean expressiongt) single
statement else single statement
7
Boolean expression
  • Boolean expression is a conditional expression
    that is evaluated to either true or false.
  • Conditional expression is a three part
    expression
  • ltexp.gt ltrelational operatorsgt ltexp.gt
  • Boolean expressions can be combined by boolean
    operators

8
Relational Operators
9
  • Boolean operators
  • means AND
  • means OR
  • ! means NOT

10
Boolean Operators
11
  • Example of Boolean expression
  • testScore lt 62
  • age gt 35
  • Height gt 6.5
  • (testScore lt 62) (age gt 35)
  • testScore 95 ???????

12
  • Block
  • Represents compound statement
  • ltstatement 1gt
  • ltstatement 2gt
  • ltstatement 3gt

13
  • Example
  • If (age lt 35)
  • coPay 5
  • System.out.println(.)
  • Else
  • coPay 10System.out.println(.)

14
  • Practice
  • Identify invalid boolean expression
  • Identify invalid If-then-else statement

15
Answers for last practice exercise
  • I.
  • (i) (x1gt0) (x1)
  • Valid
  • (ii) x22x-1
  • Invalid. Arithmetic expression
  • (iii) ((y2) ! 0)
  • Valid

16
Answers to last practice exercise
  • if (xy)
  • System.out.println( xy)
  • Else
  • System.out.println( x does not equal to y)

Should be Lowercase
17
Answers to last practice exercise
  • if (xy) then
  • grade A
  • else
  • Grade B

If we actually ask (is x equal y) then it should
be (xy)
18
Answers to last practice exercise
  • if (xy)
  • grade A
  • else
  • grade B

Hanging else
19
Simple Choice Statement
if (ltboolean expressiongt) ltblockgt else
ltblockgt // optional
if (ltboolean expressiongt) single
statement else single statement //
optional
20
Answers to some questions
  • Java machine code is generated and stored in
    .class files. For example
  • LoanCalculator.java
  • LoanCalculator.class
  • When we compile a java program outside of
    Textpad, we need to use javac.exe ltsource_namegt
  • When we run a java program outside of Textpad, we
    need to use java.exe ltclass_namegt

21
Answers to some questions
  • How to clear screen from DOS
  • for (int i0 ilt25 i)
  • System.out.println()

22
  • Nested If Statement
  • if (ltboolean expressiongt)
  • if (ltboolean expressiongt)
  • if (ltboolean expressiongt)
  • statement 1
  • else if (ltboolean expressiongt)
  • statement 2
  • else
  • statement 3
  • else .

23
  • Example
  • if (score gt 95)
  • grade A
  • else if (score gt90)
  • gradeA-
  • else if (scoregt85)
  • grade B

24
  • SWITCH statement
  • switch (variable)
  • ltcase label 1gt ltcase body 1gt
  • ltcase label 2gt ltcase body 2gt
  • ltcase label igt has the form
  • case ltconstantgt
  • or default
  • ltcase body igt is a block or a single statement,
    with break
  • variable of integer, or char data type

25
  • Example

switch (month) case 1
System.out.println("January") break
case 2 System.out.println("February") break
case 3 System.out.println("March")
break case 4 System.out.println("Ap
ril") break case 5
System.out.println("May") break
case 6 System.out.println("June")
break .. default
System.out.println("Not a month!") break

26
Char data type boolean data type
  • Char
  • It is 2 bytes
  • Char constant is denoted by single quote
  • for example s, h
  • Special characters
  • \b backspace, \t tab
  • Boolean
  • - Can be true or false

27
Examples
  • char aLetter
  • aLetter k
  • aLetter k
  • boolean flag
  • flag true
  • flag false

28
  • Practice
  • Evaluate boolean expressions
  • Identify invalid Switch statement

29
Project 1 (d)
  • Exit the system normally
  • System.exit(0)
  • Exit the system abnormally
  • e.g System.exit(1)
  • a nonzero status code indicates abnormal
    termination.

30
Repetition statements
  • Repetition of statements is a further common
    facet of everyday life and object behavior
  • Repeat every day
  • Get up have breakfast go off to work have
    lunch do more work have an evening meal relax
    go to bed.
  • Java has three types of loop for repetition
  • while, do-while, for.

31
The While Loop
while(ltboolean expressiongt) // Repeat
multiple statements. statement 1
statement 2 statement 3 ...
32
  • Example
  • // Print the numbers 1 to maximum.
  • Assuming that maximum is 100
  • int maximum 100
  • int nextToPrint 1
  • while(nextToPrint lt maximum)
  • System.out.print(nextToPrint" ")
  • // Get ready to print the next number.
  • nextToPrint nextToPrint 1
  • System.out.println()

33
The Do Loop
do // Repeat multiple statements.
statement 1 statement 2 statement 3
... while(ltboolean expression)
  • Note that the statements in the body of the loop
    are always executed at least one.
  • Note the final semicolon, which is required.

34
Printing a Sequence
int maximum 100 int nextToPrint 1 do
System.out.print(nextToPrint" ")
// Get ready for the next number.
nextToPrint nextToPrint 1
while(nextToPrint lt maximum)
System.out.println()
35
The For Loop
  • Used to repeat statements a fixed number of
    times
  • Add three teaspoons of sugar.
  • Knock twice and then give the password.
  • Used very often with arrays and collections of
    objects
  • Similar in effect to the while loop.

36
The For-Loop Outline
// Repeat multiple statements. for(initialization
condition post-body update) // Statements
to be repeated. statement 1 statement 2
statement 3 ...
  • Commonly used with increment
  • and decrement operators.

37
Increment and Decrement
  • Used as a shorthand for add-one-to and
    subtract-one-from
  • value value1
  • value 1
  • value
  • Prefix and postfix forms
  • value
  • --value
  • value--

38
A For Loop to Print a Sequence
int maximum 100 int nextToPrint for(nextToPrin
t 1 nextToPrint lt maximum
nextToPrint) System.out.print(next
ToPrint ) System.out.println()
The variable is not available outside the
loop 1234567891011 1 2 3 4 5 6.
39
  • Practice
  • Develop a java class called
  • SumCalculator.java
  • which computes a sum of all integer from 1 to 100
    and displays the result to the screen
Write a Comment
User Comments (0)
About PowerShow.com