Week #3
- Escape sequences in "String literals"
\\ - Denotes a backslash () character.
' - Denotes a simgle-quote (') character.
" - Denotes a double-quote (") character.
\n - Denotes a line feed (LF) character.
\r - Denotes a carriege return (CR) character.
\t - Denotes a horizontal tab (TB) character.
\f - Denotes a form feed (FF) character.
\b - Denotes a backspace (BS) character.
- Assignment Operators
int num1,num2,num3,num4,num5,num6;
// = equal assignment operator
num 1 = 5;
// += addition assignment
num2 = 1
num2 += 4;
// the result will be 5 as this is a shortened num2 = num2 + 4;
// -= subtraction assignment
num3 = 10;
num3 -= 7;
// the result will be 3 as this is a shortened num3 = num3 - 7;
// *= multiplication assignment
num4 = 2;
num4 *= 2;
// the result will be 4 as this is a shortened num4 = num4 * 2;
// /= division assignment
num5 = 12;
num5 /= 2;
// the result will be 6 as this is a shortened num5 = num5 / 2;
// %= modulo assignment
num6 = 3;
num6 %= 2;
// the result will be 5 as this is a shortened num6 = num6 % 2;
- Relational Operators
Note: this can be used in multiple datatypes. Using int for simplicity sake.
int num1, num2, num3;
num1 = 5
num2 = 9
num3 = 5
// == IS EQUAL - Compares the values of two variables for equality
System.out.println(num1 == num3);
// true
// != NOT EQUAL - Compares the values of two variables for inequality
System.out.println(num1 != num3);
// false
// > GREATER THAN - Checks if one value is greater than the other value
System.out.println(num1 > num2);
// false
// < LESS THAN - Checks if one value is lesser than the other value
System.out.println(num1 < num2);
// true
// >= GREATER THAN OR EQUAL - Checks if one value is greater or equal than the other
value
System.out.println(num2 >= num3);
// true
// <= LESS THAN OR EQUAL - Checks if one value is lesser or equal than the other value
System.out.println(num1 <= num3);
// true
- Logical Operators
Note: Look up "Truth Table" lesson on MMW.
int num1, num2, num3;
num1 = 5
num2 = 9
num3 = 5
// && Logical AND - Returns True if both the conditions mentioned are true.
System.out.println(num1 == num3 && num2 >= num3);
// true
// || Logical OR - Returns True if one or both the conditions mentioned are true.
System.out.println(num1 != num3 || num1 < num2);
// true
// ! Logical NOT - Returns True if the condition mentioned is false and vice versa.
System.out.println(!(num1 <= num3));
// false
- Decision Making
Pseudocode - It is a detailed yet readable description of what a computer program or
algorithm must do, expressed in a formally - styled natural language rather than in a
programming language.
Flowchart - It is a diagram that depicts a process, system or computer algorithm. It can also
be defiened as a diagrammatic representation of an algorithm or a step-by-step guide to
solving a task.
- Flow of Control
Flow of Control is the order in which a program performs actions. \
A branching statement or selection statement chooses one action from a list of two or
more possible actions.
A loop statement or iteration statements repeats an action again and again until some
stopping condition is met.
Control Structures are just a way to specify flow of control in programs
Sequence Structure may contain any number of steps, but when one task follows another
with no chance to branch away or skip a step, you are using a sequence.
Selection Structure simply involves a number of conditions or parameters which decides
one out of several written modules. Also called Conditional Structures
Looping Structure enables to control the flow of execution by repetitively performing a set
of statements as long as the continuation condition remains true. These 3 looping
statements are called for, while, and do...while statements.
- Flowchart
Flowline - Shows the process's direction by conneting two blocks with one another.
Termina/Terminator - Represents the start or end points of a flowchart process.
Process - most common component of a flowchart; indicates a step in the process.
Comment/Annotation - Indicate additional information about a step with a comment or
annotation.
Decision - This symbol represents a decision you or your team need to make to get to the
next step of the process; Yes or No/ True or False.
Stored Data - This symbolizes a data file or database.
"Or" Symbol - This indicated that the process flow continues in three or more branches.
Input/Output - Represents the process of in - or outputting external data.
Off - Page Connector - This symbol is used to connect two symbols that are of different
pages.
On - Page Connector - This dot can connect two symbols and replace long lines which
allows for a cleaner flow chart.
Java's Selection Statements (IF and Switch)
- "If - Else" Statement
Used to route program execution through 2 different paths
Uses boolean value; True or False.
Example
public static void IfStatement() {
Scanner input = new Scanner(System.in);
int grade;
System.out.print("Input Grade: ");
grade = input.nextInt();
if (grade >=85) {
System.out.println("Conratulations you passed!");
else
Output that satisfies the If statement condition.
Output:
Input Grade: 89
Congratulations you passed
- If..Else If
Example
public static void elseIf() {
Scanner in = new Scanner(System.in);
System.out.println("POSITIVE, NEGATIVE OR ZERO\nEmter a number: ");
int num1 = in.nextInt();
if (num1 > 0) {
System.out.println("The number is POSITIVE");
else if (num1 < 0) {
System.out.println("The number is NEGATIVE");
else {
System.out.println("The number is ZERO");
- Compare String
Example
public static void compareString() {
Scanner in = new Scanner(System.in);
System.out.print("GUESS MY NAME\nEnter your Guess: ");
String name = in.nextLine();
if (name.equalsIgnoreCase("Jhon Benedict") ||
name.equalsIgnoreCase("Jhon") ||
name.equalsIgnoreCase("Benedict") ||
name.equalsIgnoreCase("Ben") ||
name.equalsIgnoreCase("Jhon Benedict Garcia")) {
System.out.println("Woah tama ka, galing mo naman");
else if (name.equalsIgnoreCase("Panda")) {
System.out.println("Oy sino ka? Jowa ko lang ang nakakaalam nyan xD");
else if (name.equalsIgnoreCase("John Benedict") ||
name.equalsIgnoreCase("John")) {
System.out.println("mali po spelling, Jhon po hindi John hehe xD");
else {
System.out.println("Ay mali, I'm Jhon Benedict Garcia bro ang aking
pangalan\nkung tama naman, case sensitive to bro\n (dont type panda)");
- Nested If
public static void nestedIf() {
Scanner in = new Scanner(System.in);
String name, address;
int age;
double GWA;
System.out.println("Scholarship fo free woah!");
System.out.println("Answer the Form: ");
System.out.print("Name: ");
name = in.nextLine().toLowerCase();
System.out.print("Address: ");
address = in.nextLine();
if (address.contains("valenzuela")) {
System.out.print("Age: ");
age = in.nextInt();
if (age >= 17 && age <=21) {
System.out.print("GWA (hindi jowa): ");
GWA = in.nextDouble();
if (GWA >= 85 && GWA <=100 ) {
System.out.println("We are please to inform that " + name + "
is elligible for the scholarship");
} else {System.out.println("Sorry but GWA did not reach the
intended score");
}
} else {System.out.println("only 17-21 years old");
} else {System.out.println("Sorry no Scholarship");