Intro to Computer Programming

Conditional Statements

A computer can determine whether a “relational” statement about two values is true. Java contains the following relational operators:

==<=>=!=

less than greater than equal to less than or equal to greater than or equal to not equal to

These are binary operators, meaning they are used with two operands (values, such as a variable’s value). They are used in conditional statements, usually referred to as “if” or “if/else” statements, as well as in loops, which are programming structures that allow statements to be repeated. Both conditional statements and loops rely on boolean expressions—statements that can be evaluated as either true or false.

Here is the general form of an “if” statement:Here is the general form of an “if/else” statement:

if(boolean expression is true){if (boolean expression is true){

execute these statementsexecute these statements

..

..

..

}}

else{

execute these statements

.

.

.

}

Note that if there is only one statement inside the “if”or “else” block, the { } brackets aren’t needed (but it’soften a good idea to use them).

Here’s are 2 examples (assume “pay” is a variable that has a value stored in it):

Ex. 1if (pay > 500)

System.out.println(“That’s a good week’s pay!”);

Ex. 2if (pay > 500)

System.out.println(“That’s a good week’s pay!”);

else {

System.out.println(“I need to work more hours.”);

System.out.println(“I should ask the boss .”);

} //notice the use of the { } brackets when there is more than one statement inside the “else” block

if/then Exercises

  1. Write a program that inputs a person’s age. If the age is greater than or equal to 16, the program should print a message stating that the person is old enough to obtain a learner’s permit to drive a car. Otherwise, a message should print stating how many years until the person is eligible.
  1. Amend the Payroll program so that if the number of hours input is greater than 40, the employee receives “time-and-a-half”. This means that for the hours worked beyond 40, the employee is paid at a rate that is 1.5 times his or her regular rate. The first 40 hours are paid at the regular rate. The output should display

Regular hours workedRegular pay rate

Overtime hours workedOvertime pay rate

Regular payOvertime pay

Total pay

For example, if the Bob’s regular rate is $10/hr and he works 42 hours one week, the output would be

Regular hours worked: 40Regular pay rate: $10/hr

Overtime hours worked: 2Overtime pay rate: $15/hr

Regular pay: $400Overtime pay: $30

Total pay: $430

Another example: if Sue’s regular rate is $12/hr and she worked 30 hours, the output would be

Regular hours worked: 30Regular pay rate: $12/hr

Overtime hours worked: 0Overtime pay rate: $18/hr

Regular pay: $360Overtime pay: $0

Total pay: $360

Be sure to include the dollar signs and the “/hr” in your output.

  1. Write a program that inputs an integer and determines whether the number is odd or even. You will need to use the modulus operator (%), which returns the remainder of an integer division.
  1. A certain taxi company charges $3.50 for the first 2 minutes of a ride and $1.50 for each additional minute. Write a program that inputs the total duration of a ride and displays the correct total charge.
  1. A school club is planning a field trip that requires bus transportation. A bus can hold 50 students. Write a program that inputs the number of students going on the trip and outputs the total number ofbusses needed, the number of full buses, and the number of students who will be on the last (not full) bus. For example, if there are 170 students, there will be 3 full buses with 20 kids left over. The output would be:

Total busses: 4

Full busses: 3

Students on last bus: 20

  1. Write a program that inputs the lengths of the three sides of a triangle. The program should display whether or not the triangle is a right triangle. (Use the Pythagorean Theorem.) Make the computer determine which side is the longest side (and therefore the potential hypotenuse). The user must only enter the three sides, in no particular order—do not ask the user to enter the longest side.