CPG101 Lesson 5

download CPG101 Lesson 5

of 11

Transcript of CPG101 Lesson 5

  • 7/28/2019 CPG101 Lesson 5

    1/11

    1

    CPG101 Java Programming

    Lesson 5: Program Flow

  • 7/28/2019 CPG101 Lesson 5

    2/11

    2

    Lesson 5: Program FlowObjectives

    To write Selection statements

    To write Iteration statements

    Programming Exercises

  • 7/28/2019 CPG101 Lesson 5

    3/11

    3

    Selection Statements

    Based on the user input, decision making will takeplace.

    It involves choosing between two alternative

    courses of action.

    Java supports two kinds of selection statements

    The if, if..else or nested ifstatement

    The switch statement

  • 7/28/2019 CPG101 Lesson 5

    4/11

    4

    The ifstructure

    A single-alternate ifas it only performs an action

    based on one alternative.

    Theif..else

    structure A dual-alternate ifas it requires two options for the

    next course of action.

    Performs one action when a boolean expression

    evaluates as true and another when the booleanexpression evaluates as false.

    Selection Statements

  • 7/28/2019 CPG101 Lesson 5

    5/11

  • 7/28/2019 CPG101 Lesson 5

    6/11

    6

    Iteration Statements

    A structure that allows a block of statements to beexecuted repeatedly depending on the evaluation

    of the boolean expression.

    A loop that never ends is called a endless loop or

    infinite loop.

    A loop within a loop is called a nested loop.

    Java supports three kinds of iteration statements

    The while loop The do..while loop

    Theforloop

  • 7/28/2019 CPG101 Lesson 5

    7/11

    7

    The while loop Used to repeat a statement or block only if the

    condition is true.

    The do..while loop

    Used when we need to execute the loop body at least

    once before checking the condition.

    like the while loop, it will only repeat a statement or

    block if the condition is true.

    Iteration Statements

  • 7/28/2019 CPG101 Lesson 5

    8/11

    8

    The testing of the condition is placed differently while loop test the condition first, then execute the

    body of statements.

    do.. while execute the body of statements before

    testing the condition.

    Iteration Statements

  • 7/28/2019 CPG101 Lesson 5

    9/11

    9

    Theforloop

    Allows the statements in the loop body to be

    executed a specific number of times.

    Iteration Statements

  • 7/28/2019 CPG101 Lesson 5

    10/11

    10

    Programming Exercises

    Write a Java program to capture the users name and age

    and display a message based on the following information:

    if age > 65 display retired

    if age is between 22 to 65 display working

    if age is between 7 to 21 display studying

    if age < 7 display playing

    The user will be prompted to enter another name and age.

    If the user decides not to enter another name, theprogram will terminate.

  • 7/28/2019 CPG101 Lesson 5

    11/11

    11

    The End