Learning Unit 8 Programming Theory Flashcards
(10 cards)
String Classes
- To Uppercase();
- To Lowercase();
- charAt();
- Substring();
- Equals ();
- Equals IgnoreCase();
- Compare To();
- Compare to IgnoreCase();
- Length();
Equals and equals IgnoreCase Methods:
- Instead of operators: methods of string class are used to compare strings.
- Equals method takes the second string as a parameter
- Equals IgnoreCase is useful when: you don’t want to worry about the case the user has entered the data.
toLowerCase and toUpperCase Methods:
Changes all the characters in the string to either uppercase or lowercase.
How Strings are compared:
- Computer looks at each character individually .
- Computer compares Unicode values (numbers).
- If the words are not the same length, it will add the necessary spaces on the end of the shorter word before comparing words.
Alphabetic Order using the compare to method:
- +ve result: 2nd value is alphabetically first
- -ve result: 1st value is alphabetically first
- Computer checks Unicode value of each characters in each word, the subtracts values to determine if the difference in values is -ve/+ve.
How a switch statement works/When a switch statement executes:
- Interpreter computes the value of expression in brackets after the word switch.
- Looks for a case label that matches value.
- If it finds a match: interpreter starts executing block of code at 1st statement of case label.
- If it doesn’t find a match: interpreter starts execution at 1st statement following default.
- If no default: interpreter skips body of switch block and carries on with the rest of the program.
Investigation of the break statements:
- The absence of the break statements: statement begins executing code at 1st statement after matching case label and continues executing statements until it reaches the end of the switch block.
Important Points on the Switch Statement/Case Statement
- If one value has the same output: each value must have case in front of it and a colon after.
- Break causes switch statement to be exited.
- No default and none of case labels match input value: program will jump to the line after the end of switch statement and continue.
- Relational operators may not be used: case labels and switch statements only check constant values.
Why is the ‘shortened’ version of declaring a String allowed?
Strings are commonly used, it makes it easier to declare them like the other data types.
Use of a break keyword
Each case clause causes interpreter to exit switch statement when it has executed the statements in that case. End of case must be specified using a break statement.