Hour 7. Using Conditional Tests to Make Decisions

What You’ll Learn in This Hour:

• Using the if statement for basic conditional tests

• Testing whether one value is greater than or less than another

• Testing whether two values are equal or unequal

• Using else statements as the opposite of if statements

• Chaining several conditional tests together

• Using the switch statement for complicated conditional tests

• Creating complicated tests with the ternary operator

When you write a computer program, you provide the computer with a list of instructions called statements, and these instructions are followed to the letter. You can tell the computer to work out some unpleasant mathematical formulas, and it works them out. Tell it to display some information, and it dutifully responds.

There are times when you need the computer to be more selective about what it does. For example, if you have written a program to balance your checkbook, you might want the computer to display a warning message if your account is overdrawn. The computer should display this message only if your account is overdrawn. If it isn’t, the message would be inaccurate and emotionally upsetting.

The way to accomplish this task in a Java program is to use a conditional, a statement that causes something to happen in a program only if a specific condition is met. During this hour, you learn how to use the conditionals if, else, and switch.

When a Java program makes a decision, it does so by employing a conditional statement. During this hour, you are checking the condition of several things in your Java programs using the conditional keywords if, else, switch, case, and break. You also use the conditional operators ==, !=, <, >, <=, >= and ?, along with boolean variables.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.172.132