What is the if statement?
The “if” statement is used to create a decision structure, which allows a program to have more than one path of execution. The “if” statement causes one or more statements to execute only when a boolean expression is true.
In a decision structure’s simplest form, a specific action is taken only when a condition exists. If the condition does not exist, the action is not performed.
What is one way to code with the if statement?
One way to code a decision structure in Java is with the if statement. Here is the general format of the if statement:
if (BooLeanExpression)
statement;
The if statement is simple in the way it works:
The BooleanExpression that appears inside the parentheses must be a boolean expression.
A boolean expression is one that is either true or false. If the boolean expression is true, the very next statement is executed. Otherwise, it is skipped.
The statement is conditionally executed because it executes only under the condition that the expression in the parentheses is true.
What is used in an boolean expression?
Typically, the boolean expression that is tested by an if statement is formed with a relational operator.
A relational operator determines whether a specific relationship exists between two values. For example, the greater than operator (>) determines whether one value is greater than another. The equal to operator (==) determines whether two values are equal.
What are the relational operators?
> Greater Than
< Less Than
= Greater Than or Equal to
<= Less Than or equal to
== Equal to
!= Not equal to
What does enclosing a group of statements inside braces create?
The previous examples of the if statement conditionally execute a single statement. The if statement can also conditionally execute a group of statements, as long as they are enclosed in a set of braces. Enclosing a group of statements inside braces creates a block of statements.
What does this code execute?
double sales = 55000, bonus, commissionRate, daysOff = 0;
if (sales > 50000)
{
bonus = 500.0;
commissionRate = 0.12;
daysOff += 1;
}If sales is greater than 50,000, this code will execute all three of the statements inside these braces, in the order they appear.
If the braces are accidentally left out, however, the if statement conditionally executes only the very next statement. In this case, only “bonus = 500.0” will execute.
What is a flag?
A flag is a boolean variable that signals when some condition exists in the program. When the flag variable is set to false, it indicates the condition does not yet exist. When the flag variable is set to true, it means the condition exists.
Flag variable — boolean data type and we have to monitor it to see if the variables is holding true or false
Used to signal if something else is true or not
What is used to test character data?
You can use the relational operators to test character data as well as numbers. Example 1 uses the == operator to compare it to the character ‘A’:
String userInput;
char ch;
System.out.println("Please enter a character: ");
userInput = keyboard.nextLine();
ch = userInput.charAt(0);Example 1
if (ch == ‘N’)
System.out.println(“The letter is N.”);
The != operator can also be used with characters to test for inequality.
You can also use the >, <, >=”, and <= operators to compare characters.
How do computers store charcaters?
Computers do not actually store characters, such as A, B, C, and so forth, in memory.
Instead, they store numeric codes that represent the characters. Java uses Unicode, which is a set of numbers that represents all the letters of the alphabet (both lowercase and uppercase), the printable digits 0 through 9, punctuation symbols, and special characters.
When a character is stored in memory, it is actually the Unicode number that is stored.
When the computer is instructed to print the value on the screen, it displays the character that corresponds with the numeric code.
What is unicode?
Unicode is an international encoding system that is extensive enough to represent all the characters of all the world’s alphabets.
In Unicode, letters are arranged in alphabetic order. Because ‘A’ comes before ‘B’, the numeric code for the character ‘A’ is less than the code for the character ‘B’.
In Unicode, the uppercase letters come before the lowercase letters, so the numeric code for ‘A’ (65) is less than the numeric code for ‘a’ (97). In addition, the space character (code 32) comes before all the alphabetic characters.
What is the if-else statement?
The if-else statement will execute one group of statements if its boolean expression is true, or another group if its boolean expression is false.
Like the if statement, a boolean expression is evaluated.
If the expression is true, a statement or block of statements is executed.
If the expression is false, however, a separate group of statements is executed.
The if-else statement is an expansion of the if statement.
See the following format:
if (BooleanExpression)
Statement or block
else
statement or blockThe first one that the else if statement is true will print the block of code underneath and won’t test the rest – ignore the rest of the code after
If you take out the else from else if – can print out multiple because multiple statements can be true
else if – exclusive
Only one trailing else possible with else if statements – if none of the above statements are found to be true
What happens when division by zero in Java occurs?
The program below uses the if-else statement to handle a classic programming problem: division by zero. In Java, a program crashes when it divides an integer by 0. When a floating-point value is divided by 0, the program doesn’t crash. Instead, the special value “Infinity” is produced as the result of the division.
What does this code do:
double number1, number2; // Division operands
double quotient; // Result of division
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Get the first number.
System.out.print("Enter a number: " );
number1 = keyboard.nextDouble();
// Get the second number.
System.out.print("Enter another number: ");
number2 = keyboard.nextDouble();
if (number2 == 0)
{
System.out.println("Division by zero is not possible.");
System.out.println("Please run the program again and ");
System.out.println("enter a number other than zero.");
}
else
{
quotient = number1 / number2;
System.out.print("The quotient of " + number1);
System.out.print(" divided by " + number2);
System.out.println(" is " + quotient);
}The value of number2 is tested before the division is performed. If the user enters 0, the block of statements controlled by the if clause executes, displaying a message that indicates the program cannot perform division by zero. Otherwise, the else clause takes control, which divides number1 by number2 and displays the result.
What are nested if statements?
To test more than one condition, an if statement can be nested inside another if statement. Sometimes an if statement must be nested inside another if statement.
For example, consider a banking program that determines whether a bank customer qualifies for a special interest rate on a loan. To qualify, two conditions must exist: (1) the customer’s salary must be at least $30,000, and (2) the customer must have held his or her current job for at least two years.
How would you code asks the user to enter a numeric test score and displays a letter grade for the score. The program uses nested decision structures to determine the grade.
int testScore; // Numeric test score
String input; // To hold the user’s input
// Get the numeric test score.
input = JOptionPane.showInputDialog("Enter your numeric " +
"test score and I will tell you the grade: ");
testScore = Integer.parseInt(input);
// Display the grade.
if (testScore < 60)
{
JOptionPane.showMessageDialog(null, "Your grade is F.");
}
else
{
if (testScore < 70)
{
JOptionPane.showMessageDialog(null, "Your grade is D.");
}
else
{
if (testScore < 80)
{
JOptionPane.showMessageDialog(null, "Your grade is C.");
}
else
{
if (testScore < 90)
{
JOptionPane.showMessageDialog(null, "Your grade is B.");
}
else
{
JOptionPane.showMessageDialog(null, "Your grade is A.");
}
}
}
}
System.exit(0);
}What is an if-else-if statement?
The if-else-if statement tests a series of conditions. It is often simpler to test a series of conditions with the if-else-if statement than with a set of nested if-else statements.
Example:
if (testScore < 60)
JOptionPane.showMessageDialog(null, “Your grade is F.”);
else if (testScore < 70)
JOptionPane.showMessageDialog(null, “Your grade is D.”);
else if (testScore < 80)
JOptionPane.showMessageDialog(null, “Your grade is C.”);
else if (testScore < 90)
JOptionPane.showMessageDialog(null, “Your grade is B.”);
else
JOptionPane.showMessageDialog(null, “Your grade is A.”);
What is the AND operator?
Connects two boolean expressions into one. Both expressions must be true for the overall expression to be true.
&&
The && operator is known as the logical AND operator. It takes two boolean expressions as operands and creates a boolean expression that is true only when both subexpressions are true.
What is the OR operators?
Connects two boolean expressions into one. One or both expressions must be true for the overall expression to be true. It is only necessary for one to be true, and it does not matter which one.
The || operator is known as the logical OR operator. It takes two boolean expressions as operands and creates a boolean expression that is true when either subexpression is true.
||
What is the NOT operator?
The ! operator reverses the truth of a boolean expression. If it is applied to an expression that is true, the operator returns false. If it is applied to an expression that is false, the operator returns true.
The ! operator performs a logical NOT operation. It is a unary operator that takes a boolean expression as its operand and reverses its logical value. In other words,
if the expression is true, the ! operator returns false, and if the expression is false, it returns true.
!
What logical operators does Java provide?
Java provides two binary logical operators, && and ||, which are used to combine two boolean expressions into a single expression. It also provides the unary ! operator, which reverses the truth of a boolean expression.
How does this code work:
double temperature;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a temperature: ");
temperature = keyboard.nextDouble();
if (!(temperature > 100))
{
System.out.println("This is below the maximum temperature.");
}
else
{
System.out.println("This is above the maximum temperature.");
}First, the expression (temperature > 100) is tested and a value of either true or false is the result. Then the ! operator is applied to that value. If the expression (temperature > 100) is true, the ! operator returns false. If the expression (temperature > 100) is false, the ! operator returns true. The previous code is equivalent to asking: “Is the temperature not greater than 100?”
What do you use to determine a range?
Sometimes you will need to write code that determines whether a numeric value is within a specific range of values or outside a specific range of values. When determining whether a number is inside a range, it’s best to use the && operator.
How do you determine whether a number is outside a range?
When determining whether a number is outside a range, it’s best to use the || operator. It’s important not to get the logic of these logical operators confused
What do you use to compare String objects?
You cannot use relational operators to compare String objects. Instead you must use a String method.
Remember that a string object is referenced by a variable that contains the object’s memory address. When you use a relational operator with the reference variable, the operator works on the memory address that the variable contains, not the contents of the string object.