A Decision Structure requires a Boolean expression, which is a statement that will provide you with a true or a false value for a given relation. Performing a boolean expression requires the use of relational operators. In this chapter, we will discuss statements such as the if-statement that require to have a boolean expression to perform an action.
The if statement looks as follows:
if(<boolean expression>)
{
// statements
}
Example: Suppose that we are writing a program that decides what clothes you should wear today based on the current temperature. The program shall ask the user to enter the temperature
The basic skeleton of the code starts as follows:
import java.util.Scanner;
public class DecisionExample01{
public static void main(String [] args){
Scanner input = new Scanner(System.in);
System.out.println("What is the current temperature?");
int temp = input.nextInt();
}
}
Suppose that we are in El Paso, TX, and we don't know what cold is… But we still want to decide what to wear. In case the temperature is greater than or equal to 65 you can wear shorts and a t-shirt. Otherwise, you can wear pants and jacket.
if( temp >= 65 )
{
System.out.println("wear t-shirt and shorts");
}
else
{
System.out.println("wear pants and jacket");
}
There are situations (especially when you start coding or gaining practice coding your if-statements) where you will find an error such as "else without if." One of the most common reasons is:
· You have a semicolon on the if statement
1. if( temp >= 65 );
2. {
3. System.out.println("wear t-shirt and shorts");
4. }
5. else
6. {
7. System.out.println("wear pants and jacket");
8. }
CS1
[Comparing String Objects]
130.421.c.6.k Compare objects using reference values and a comparison routine;
Design, write, test, and debug a program that effectively uses the different structured data types provided in the language like strings, arrays/lists, dictionaries, sets
Write a program that uses some language-provided libraries and frameworks (where applicable).
Read a given program, and explain what it does.
Trace the flow of control during the execution of a program (both correct and incorrect).
Use appropriate terminology to identity elements of a program (e.g., identifier, operator, operand)
Strings and string processing
Write programs that work with text by using string processing capabilities provided by the language.