0% found this document useful (0 votes)
56 views11 pages

CPG101 - Java Programming: Lesson 5: Program Flow

This document discusses program flow in Java programming through selection and iteration statements. It covers if/else, switch, while, do-while, and for statements. Examples are given for each type of statement to conditionally execute code based on evaluations or repeat code a set number of times. An exercise is provided to capture a user's name and age, and output different messages depending on their age range through if/else statements.

Uploaded by

Osmar Ix
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views11 pages

CPG101 - Java Programming: Lesson 5: Program Flow

This document discusses program flow in Java programming through selection and iteration statements. It covers if/else, switch, while, do-while, and for statements. Examples are given for each type of statement to conditionally execute code based on evaluations or repeat code a set number of times. An exercise is provided to capture a user's name and age, and output different messages depending on their age range through if/else statements.

Uploaded by

Osmar Ix
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 11

CPG101 Java Programming

Lesson 5: Program Flow

Lesson 5: Program Flow


Objectives To write Selection statements To write Iteration statements Programming Exercises

Selection Statements
Based on the user input, decision making will take place. It involves choosing between two alternative courses of action. Java supports two kinds of selection statements
The if, if..else or nested if statement The switch statement

Selection Statements
The if structure
A single-alternate if as it only performs an action based on one alternative.

The if..else structure


A dual-alternate if as it requires two options for the next course of action.
Performs one action when a boolean expression evaluates as true and another when the boolean expression evaluates as false.

Selection Statements
The Nested if structure
An if within another if statement. Nested if statements are useful when two conditions must be met before some action is taken.

The switch structure


Another alternative for nested if statements. Useful when testing single variable against a series of exact integer or character values.

Iteration Statements
A structure that allows a block of statements to be executed 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 The for loop
6

Iteration Statements
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
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
The for loop Allows the statements in the loop body to be executed a specific number of times.

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, the program will terminate.

10

The End

11

You might also like