Unit 5 decision structures part 2 Flashcards

(9 cards)

1
Q

What are logical operators

A

-Has three main logical operators that work with boolean values
-&&, ||, !

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

&&

A

-Meaning: AND
-It connects two Boolean expressions into one. Both expressions must be true for the overall expression to be true

Example:

expression 1 true, expression 2 true expression 1 && expression 2 therefore is true

Example:
expression 1 true, expression 2 false expression 1 && expression 2 therefore is false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

The || operator

A

-Means OR
-The logical OR (||) combines two boolean expressions and the result is true if at least one of the expressions is true
-It is false only when both expressions are false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

The ! Operator

A

-Means: NOT
-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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is short circuiting

A

-Short circuiting means java stops checking a logical condition early if it already knows the answer
-happens with && and ||

example for &&:
if (x > 0 && y > 0)
Java checks the first condition and if it is false, && anything will always be false. For &&, java stops when it sees false first

example for ||:
if (x>0 || y> 0)
Java checks the first condition and if its true || anything is always true. Java stops when it sees true first

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What do you use when you are comparing string objects?

A

-Use (name1.equals(name2))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is ignoring case in string comparisons

A

-Java gives two special methods for case insensitive comparision
-.equalsIgnoreCase() checks if two strings are the same, ignoring case

example:
“HELLO”. equalsIgnoreCase(“hello”)

-.compareToIgnoreCase() compares two strings alphabetically, ignoring case

example:
“apple”.compareTo IgnoreCase(“APPLE”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the conditional operator

A

-Allows you to write a short version of an if else statement all in one line
-it uses three operands thats why it can also be called a ternary

can write like:
condition ? expressionIfTrue : expressionIfFalse

example:
z= (x>y) ? 10:5;
Means: if x is greater than y, set z to 10. otherwise set x to 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a switch statement

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly