Module 3: Decision Structure Flashcards
An operator that determines whether a specific relationship exist between two
values.
The Relational Operator
Operator for greater than
>
Operator for less than
<
Operator for greater than or equal to
> =
Operator for less than or equal to
<=
Operator for equal to
==
Operator for not equal to
!=
Explain the difference between == and =
The double equal(==) is a relational operator which can use for comparison. The
boolean expression have a value of true or false. While the single equal (=) is an
assignment operator that uses to assign a value to the variable
What is the value of the following expression?
int a = 5
a == 10
False
int a = 5
a = 10
The value of a is simply changed to 10.
How many variables does the left side of an expression must have when using the assignment operator?
Only single variable
Which one is valid?
a. c = 5
b. c+d= 5
a is valid, b is invalid.
Also known as Boolean operators, operate on Boolean values to
create a new Boolean value.
Logical Operator
Sometimes, a statement is executed is determined by a combination of
several conditions. What can you use to combine these conditions?
Logical Operator
What is the name of the ! operator?
not
Rule of the ! operator
Opposite of the expression
Name of && operator
And
Rules of && operator
One false, all false
Name of || operator
Or
Rules of || operator
One true, all true
What would be the result?
20 > 18 && 49 < 50
20 > 18 = True
49 < 50 = True
The expression uses the && operator (one false, all false). Therefore the result is true.
What would be the result?
kg = 49
age = 20
20 > 30 || 49 < 51
20 > 30 = False
49 < 51 = True
The expression uses the || operator (one true, all true). Therefore, the result is true.
What is the precedence of the logical operators?
AND (&&) - Highest
OR (||)
NOT (!) - Lowest
What would be the result of the following expression?
!(3>4)|| !(12>4) && !(4>1)
!(3>4)|| !(12>4) && !(4>1)
!(False) || !(True) && !(True)
True || False && False
True || False
True
Can relational operators (==, >=, etc) be used to test character data?
Yes
What will you use to compare String objects?
A String method
To compare the contents of two String objects correctly, use the String class’s equals method.
StringReference1.equals(StringReference2)
The String class also provides the compareTo method, which can be used to
determine whether one string is greater than, equal to, or less than another string.
StringReference.compareTo(OtherString)
What does the compareTo method return?
It returns an integer value.
What does it mean if the method’s return value is negative?
StringReference is less than Other String.
What does it mean if the method’s return value is zero?
The two strings are equal.
What does it mean if the method’s return value is positive?
StringReference is greater than OtherString
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.
The If Statement
import java.util.scanner;
To import the scanner class.
Scanner input = new Scanner(System.in);
String color = input.nextLine;
“input” is the name of the class
Lets the value of a variable or expression determine where the
program will branch to.
The Switch Statement
Is a multiple alternative decision structure. It allows you
to test the value of a variable or an expression and then use that value to determine which statement or set of statements to execute.
The Switch Statement
Allows you to format output in a variety of ways.
System.out.printtf(FormatString, ArgumentList);
String that contains text, special formatting specifiers, or both.
Optional and can be used to perform actions when none of
the specified cases matches the switch-expression.
Statement that immediately ends the switch statement.
break
optional, can be used to perform actions when none of
the specified cases matches the switch-expression.
default
What is the general format for the system.out.printtf method?
System.out.printf(FormatString, ArgumentList)
Allows you to format output in a variety of ways.
System.out.printtf
What would the output be?
int dogs = 2;
int cats = 4;
System.out.printtf(“We have %d dogs and %d cats \n”, dogs, cats)”
Output: We have 2 dogs and 4 cats
All format specifiers begin with what?
%
They cause the value to be formatted in a variety of ways in a format specifier.
Flags
You can optionally specify the minimum field width for
the value.
Width
All format specifiers must end with a conversion character, such as f (%f)
for floating-point, or d (%d) for decimal integer
Conversion
You can change the number of decimal points that are displayed
Precision
What would the output be?
double temp = 78.42819;
System.out.printf(“The temperature is %2f degrees.\n”, temp);
Output: The temperature is 78.4 degrees.
What will happen if you specify a precision with the %d format specifier?
An error at runtime will occur because precision can only be specified with floating-point values
The minimum
number of spaces that should be used to display the value
Minimum Field Width
What would the output be?
double number = 12345.6789;
System.out.printf(“The number is:%20f\n”, number);
Output: The number is: 12345.678900
Comma Separator
What would the output be?
double amount = 1234567.89;
System.out.printf(“The value is %,f\n”, amount);
Output : The value is 1,234,567.890000
Adding numbers with Leading Zeros
What would the output be?
double number = 123.4;
System.out.printf(“The number is:%08.1f\n “, number);
Output: The number is: 1,234,567.890000
%[flags][width][.precision]conversion
General format for format specifiers.
What happens if the break statement is omitted?
The program would execute all of the lines from the matching case statement to the end other block.
can be assigned a value only once, and then it cannot be
changed later in the program.
Named Constant
What is the keyword preceding a named constant in its declaration statement?
final