Learning Unit 7 Programming Theory Flashcards
(13 cards)
Coding Programs with If Statements
- If and else are reserved words, IDE may indicate this by putting them in a different color.
- Pseudocode
begin
_____________________
if ____________________
then_________________
else_________________
end if
_____________________
end
Relational Operators
- Double equals in Java is used to check equality.
- Single equals sign is used to assign/store a value in a variable.
- ! used for Not.
Important points on the If statement
- If…else is 1 statement in Java
- Condition in an if statement can either be:
True
False
Important that the layout of an if statement
Makes it easy to read.
If there is one statement in a block
Curly brackets can be left off.
Objects of string data type cannot be compared using ==:
String is not a primitive data type
Else is optional:
No else, program continues with the next instruction after the end of if statement block.
When to use a Nested if:
- Use an else if only one of the conditions can be true at the same time, so the program doesn’t check every if statement unnecessarily.
- Do not use an else: there is more that one condition to be tested and the conditions are independent of each other.
Why are ‘a’ and ‘A’ not the same in Java?
- When Java compares the 2 letters, it compares their Unicode code values and not the actual characters.
- Since ‘a’ and ‘A’ don’t have the same Unicode values, the computer sees them as 2 totally different characters.
How are conditions evaluated in Java
Arithmetic and logical expressions are evaluated as sums by the Arithmetic Logic Unit (ALU) in the processor.
The NOT(!) operator
&& makes your program more efficient.
|| Conditional OR:
Java will always carry out logical operators in the following order:
- Brackets
- NOT
- AND
- OR
A problem when using If Statement - A Valueless Variable
- When a variable is only given a value in the if statement blocks: if none of the if statement are true Java knows that the variable will not get a value.
- Any variable that is only assigned a value in if statements must be initialized before the if statement so that it definitely gets a value in the program.
- No.s are usually assigned to 0
- Strings are initialized to an empty string.
- Characters cannot be initialized to nothing: must be character.