We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16
Object Oriented
Programming with Java
– Lesson 3 Dr. Daniel Danso Essel Department of ICT EDUCATION At the end of this lesson, students should be able to: declare boolean variables and write Boolean expressions using relational operators implement selection control using one-way if, two-way if-else, nested if and multi-way if Objectives statements use Switch statements implement for loops, while loops, do-While loops use break and continue statements in a program implement loops in VEXcode VR Without conditional logic code is executed from top to bottom. (sequentially) Conditional Conditional statements allows the execution of Logic only certain parts of the code based on some conditions. An if statement is a construct that enables a program to specify alternative paths of execution. E.g. if ( condition to test) Selection { Statements statement(s) to execute (IF) } What happens if the condition is false? You must combine relational or conditional operators with selection statements E.g. <, >, <=, >=, &&, ||, ==, ! if ( condition to test ) { statement to execute if true Selection } Statements else (IF-ELSE) { statement to execute if false } Selection Write an if-else-if condition that gives a student Statements grade based on Total_Score (Use UEW scheme) (IF-ELSE-IF) This means to put one If statement inside another. Selection Write a nested-if statement to check the ff: Statements Student is from UEW and in Dept. of ICT (NESTED-IF) Education and in Level 200 and in your group Display “We are learning Java” A Boolean value is one with two choices: true or false, yes or no, 1 or 0. Using In Java, there is a variable type for Boolean values: Boolean Values boolean user = true; Rewrite the code with if ( user == true) { snippet on the left Selection System.out.println using the ! (NOT) Statements ("it's true"); } operator else { System.out.println ("it's false"); } switch statement gives you the option to test for a range of values for your variables. They can be used instead of long, complex if … else if statements. Syntax switch ( variable_to_test ) { Selection case value: Statements code_here; (Switch) break; case value: code_here; break; default: values_not_caught_above; } Another way to interrupt the flow from top to bottom is by using loops. A programming loop is one that forces the Loops program to go back up again If it is forced back up again you can execute lines of code repeatedly. Most common type of loop for ( start_value; end_value; update statement) { statement to execute } For Loop While loops are a lot easier to understand than for loops. Here's what they look like: While while ( condition ) Loop { statement to execute update statement } The syntax is as follows: do Do-While { Loop statement to execute } while (condition) You have used the keyword break in a switch Jump statement. Statements – You can also use break in a loop to immediately Break terminate the loop. if (sum >= 100) public class continue; TestBreak { } public static void main(String[] args) { System.out.println(" Jump The number is " + Statements – int sum = 0; number); Continue int number = 0; System.out.println(" while (number < 20) { The sum is " + sum); number++; } sum += number; } 1. Write a program that accepts user input to print multiplication table for any number 2. Use the while loop to generate the first 100 even numbers 3. Write a program that simulates the ATM interface. Use a d0-while loop to display 6 services to the user Get the user input and display submenus appropriately 4. Write a Boolean expression that evaluates to true if a Practice number stored in variable num is between 1 and 100 or the number is negative Programs 5. What is y after the following switch statement is executed? Rewrite the code using an if-else statement. x = 3; y = 3; switch (x + 3) { case 6: y = 1; default: y += 1; } 6. Write a program to perform the function of a Simple Interest Calculator.