Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Conditional Statements: if, else and Compound Conditions, Exams of Computer Science

Learn about java's conditional statements, including if-else and compound conditions using and, or, and not. Understand how to write and test if statements, avoid dangling elses, and handle multiple conditions.

Typology: Exams

Pre 2010

Uploaded on 08/09/2009

koofers-user-szl
koofers-user-szl 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS121/IS223
Conditional Statements
In a program, we frequently have to make choices. Given some condition, we can have the
program do one thing or another. Programming languages have if – else statements that do this.
We will look at the one for Java.
The form for the conditional in Java is
if (some condition is true)
do something
else
do something else
An example of this is a statement that decides which of two numbers is larger.
int age1 = 3, age2 = 6;
if (age1 > age2)
System.out.println ("The first animal is older.");
else
System.out.println ("The second animal is older.");
In the case above, the second age is greater, so the second statement will be displayed.
Sometimes we want to do several things if a condition is true. In that case, we have to enclose
the statements in curly braces. The if – else statement only does one thing otherwise.
if (age1 >= age2)
{
System.out.print ("The first animal is older");
System.out.println (" or the animals are the same age.");
}
else
{
System.out.println ("The second animal is older.");
}
The curly braces were not needed in the else part (called a clause), but putting them in does not
hurt.
We can also have if – else statements inside of if – else statements.
if (age1 > age2)
System.out.println ("The first animal is older.");
else
if (age1 == age2)
System.out.println ("The animals are the same age.");
else
System.out.println ("The second animal is older.");
The computer will execute the first correct condition and not test the remaining ones.
A different example has an entire cascade of if – else statements.
// Method to analyze a patient's temperature.
public void classifyTemperature ()
{
System.out.print ( "Enter your temperature. ");
temp = scan.nextInt ();
pf3

Partial preview of the text

Download Java Conditional Statements: if, else and Compound Conditions and more Exams Computer Science in PDF only on Docsity!

CS121/IS

Conditional Statements In a program, we frequently have to make choices. Given some condition, we can have the program do one thing or another. Programming languages have if – else statements that do this. We will look at the one for Java. The form for the conditional in Java is if (some condition is true) do something else do something else An example of this is a statement that decides which of two numbers is larger. int age1 = 3, age2 = 6; if (age1 > age2) System.out.println ("The first animal is older."); else System.out.println ("The second animal is older."); In the case above, the second age is greater, so the second statement will be displayed. Sometimes we want to do several things if a condition is true. In that case, we have to enclose the statements in curly braces. The if – else statement only does one thing otherwise. if (age1 >= age2) { System.out.print ("The first animal is older"); System.out.println (" or the animals are the same age."); } else { System.out.println ("The second animal is older."); } The curly braces were not needed in the else part (called a clause), but putting them in does not hurt. We can also have if – else statements inside of if – else statements. if (age1 > age2) System.out.println ("The first animal is older."); else if (age1 == age2) System.out.println ("The animals are the same age."); else System.out.println ("The second animal is older."); The computer will execute the first correct condition and not test the remaining ones. A different example has an entire cascade of if – else statements. // Method to analyze a patient's temperature. public void classifyTemperature () { System.out.print ( "Enter your temperature. "); temp = scan.nextInt ();

if (temp >= 105) System.out.println ( "Medical emergency, go to the hospital." ); else if (temp >= 101) System.out.println ( "High temperature, see a doctor." ); else if (temp >= 99) System.out.println ( "Mild fever, take two aspirin and go to bed." ); else if (temp >= 97) System.out.println ( "Temperature normal." ); else System.out.println ( "Your temperature is low, take a warm bath." ); } // classifyTemperature So far we have seen simple conditions that used one of the relational operators: < , >, <=, >=, !=, == We can also make compound conditions that use and, or, and not. The Java symbols for these are && - and || – or ! - not An example would be if (age1 < age2 && age1 != age2) { System.out.print ("The first animal is older"); System.out.println (" and they are not the same age."); } There is a problem called a dangling else. In this case, it may not be clear which if an else goes with. The answer is the closest one. if (sleepy) if (grumpy) System.out.println ("Sleepy and grumpy"); else System.out.println ("Sleepy but not grumpy"); else System.out.println ("Not sleepy, but who knows about grumpy"); This can be rewritten using a compound condition. if (sleepy && grumpy) System.out.println ("Sleepy and grumpy"); else if (sleepy) System.out.println ("Sleepy but not grumpy"); else System.out.println ("Not sleepy, don't know about grumpy"); When testing a program with if – else statements, you have to test all possible conditions. In lab 8, you are to determine an animal’s age given its birthday and today’s date. We can get today’s date from the computer. The birthday must be entered into the program either by assignment or reading. The age is not just the difference between the years. It also depends upon whether it is before or after the animal’s birthday. Before writing the if – else statement, you should list all the possible conditions.