introAP_CS_A-chp2.md

introAP_CS_A-chp2.md

introduction to ap computer science a, part 2

in part 1, we learned that java programs use classes, a main method, print statements, variables, expressions, and basic arithmetic. now we will build on that by learning how programs make decisions.

most useful programs do not do the exact same thing every time. they look at information, compare values, and choose what to do next.

for example, a grading program might say:

if the score is 90 or higher, the grade is an a.
otherwise, the grade is not an a.

this kind of decision is called a conditional.

boolean expressions

a boolean expression is an expression that evaluates to either true or false.

remember that a boolean variable can only store two possible values:

boolean passed = true;
boolean absent = false;

java also lets us create boolean expressions by comparing values.

System.out.println(5 > 3);
System.out.println(5 < 3);
System.out.println(5 == 5);

this prints:

true
false
true

the expression 5 > 3 is true.

the expression 5 < 3 is false.

the expression 5 == 5 is true.

Person in costume with hat and mustache using magnifying glass
finding that one equals sign that ruined my afternoon

comparison operators

java has several comparison operators.

>    greater than
<    less than
>=   greater than or equal to
<=   less than or equal to
==   equal to
!=   not equal to

the double equals sign == is very important.

x == 5

means: is x equal to 5?

but this:

x = 5

means: store 5 inside x.

these are not the same.

a common beginner mistake is using = when you mean ==.

if statements

an if statement tells java to run code only when a condition is true.

int score = 95;

if (score >= 90) {
    System.out.println("a");
}

this prints:

a

because score >= 90 is true.

now look at this:

int score = 82;

if (score >= 90) {
    System.out.println("a");
}

this prints nothing because score >= 90 is false.

the code inside the curly braces only runs when the condition is true.

if else statements

an if else statement gives java two possible paths.

int score = 82;

if (score >= 90) {
    System.out.println("a");
} else {
    System.out.println("not an a");
}

this prints:

not an a

because the condition score >= 90 is false.

the if block runs when the condition is true.

the else block runs when the condition is false.

else if statements

sometimes there are more than two possible outcomes.

int score = 86;

if (score >= 90) {
    System.out.println("a");
} else if (score >= 80) {
    System.out.println("b");
} else if (score >= 70) {
    System.out.println("c");
} else {
    System.out.println("needs improvement");
}

this prints:

b

java checks the conditions from top to bottom.

first it checks:

score >= 90

this is false because 86 is not at least 90.

then it checks:

score >= 80

this is true, so java prints b.

after java finds a true condition, it skips the rest of the chain.

order matters

the order of conditions can change the result.

look at this code:

int score = 95;

if (score >= 70) {
    System.out.println("c or better");
} else if (score >= 80) {
    System.out.println("b or better");
} else if (score >= 90) {
    System.out.println("a");
}

this prints:

c or better

that may seem wrong, but java is doing exactly what it was told.

since 95 is greater than or equal to 70, the first condition is true. java runs that block and skips the rest.

the better version is:

int score = 95;

if (score >= 90) {
    System.out.println("a");
} else if (score >= 80) {
    System.out.println("b");
} else if (score >= 70) {
    System.out.println("c");
}

when writing an if else if chain for ranges, usually check the most specific or highest condition first.

logical operators

sometimes we need to combine conditions.

java has three major logical operators:

&&   and
||   or
!    not

and

&& means both sides must be true.

int score = 92;
boolean submittedProject = true;

if (score >= 90 && submittedProject) {
    System.out.println("honors");
}

this prints:

honors

because both conditions are true.

if one side is false, the whole expression is false.

int score = 92;
boolean submittedProject = false;

if (score >= 90 && submittedProject) {
    System.out.println("honors");
}

this prints nothing.

or

|| means at least one side must be true.

int score = 68;
boolean extraCredit = true;

if (score >= 70 || extraCredit) {
    System.out.println("pass");
}

this prints:

pass

even though score >= 70 is false, extraCredit is true. for ||, one true side is enough.

not

! flips a boolean value.

boolean absent = false;

if (!absent) {
    System.out.println("present");
}

this prints:

present

because absent is false, so !absent is true.

nested if statements

an if statement can go inside another if statement. this is called nesting.

int score = 94;
boolean submittedProject = true;

if (score >= 90) {
    if (submittedProject) {
        System.out.println("a with project credit");
    }
}

this prints:

a with project credit

the inner if only gets checked if the outer if is true.

nested conditionals are useful, but they can become hard to read. when possible, a logical operator may be cleaner.

this:

if (score >= 90) {
    if (submittedProject) {
        System.out.println("a with project credit");
    }
}

can be written as:

if (score >= 90 && submittedProject) {
    System.out.println("a with project credit");
}

both versions mean the same thing.

comparing strings

strings are objects, so we should not compare them with ==.

this is a common ap cs a rule.

wrong:

String name = "alan";

if (name == "alan") {
    System.out.println("same name");
}

this may seem like it works sometimes, but it is not the correct way to compare strings.

correct:

String name = "alan";

if (name.equals("alan")) {
    System.out.println("same name");
}

to compare strings for equality, use .equals.

another example:

String password = "java123";

if (password.equals("java123")) {
    System.out.println("access granted");
} else {
    System.out.println("access denied");
}

this prints:

access granted

the mod operator and conditionals

the remainder operator % is very useful with conditionals.

to check if a number is even:

int x = 14;

if (x % 2 == 0) {
    System.out.println("even");
} else {
    System.out.println("odd");
}

this prints:

even

why?

14 % 2 gives the remainder after dividing 14 by 2.

since 14 divides evenly by 2, the remainder is 0.

therefore, x % 2 == 0 is true.

to check if a number is divisible by 5:

int x = 35;

if (x % 5 == 0) {
    System.out.println("divisible by 5");
}

this prints:

divisible by 5

tracing conditionals

when tracing conditionals, do not guess. go line by line.

look at this code:

int x = 8;

if (x > 10) {
    System.out.println("big");
} else if (x > 5) {
    System.out.println("medium");
} else {
    System.out.println("small");
}

start with:

x = 8

check the first condition:

x > 10

this is false.

check the next condition:

x > 5

this is true.

so java prints:

medium

then java skips the final else.

common conditional mistakes

one common mistake is confusing = and ==.

wrong:

if (x = 5) {
    System.out.println("five");
}

correct:

if (x == 5) {
    System.out.println("five");
}

another common mistake is writing conditions in the wrong order.

bad:

if (score >= 60) {
    System.out.println("passed");
} else if (score >= 90) {
    System.out.println("excellent");
}

a score of 95 prints passed, not excellent, because the first condition is already true.

better:

if (score >= 90) {
    System.out.println("excellent");
} else if (score >= 60) {
    System.out.println("passed");
}

another common mistake is using == with strings.

bad:

if (word == "yes") {
    System.out.println("confirmed");
}

better:

if (word.equals("yes")) {
    System.out.println("confirmed");
}

practice example 1

predict the output:

int age = 16;

if (age >= 18) {
    System.out.println("adult");
} else {
    System.out.println("minor");
}

answer:

minor

because 16 is less than 18.

practice example 2

predict the output:

int x = 12;

if (x % 2 == 0) {
    System.out.println("even");
}

if (x % 3 == 0) {
    System.out.println("divisible by 3");
}

answer:

even
divisible by 3

both if statements are checked separately.

12 is divisible by 2, so the first message prints.

12 is divisible by 3, so the second message also prints.

practice example 3

predict the output:

int x = 12;

if (x % 2 == 0) {
    System.out.println("even");
} else if (x % 3 == 0) {
    System.out.println("divisible by 3");
}

answer:

even

this time, the second condition is part of an else if.

since the first condition is true, java prints even and skips the rest.

this is an important difference.

separate if statements can both run.

an if else if chain only chooses one path.

man in gray crew neck shirt making thumbs up
congratulations, you now speak beginner java conditionals

main ideas to remember

a boolean expression is either true or false.

comparison operators compare values.

== checks equality.

= assigns a value.

an if statement runs code only when its condition is true.

an else gives a backup path.

an else if allows multiple possible paths.

conditions are checked from top to bottom.

&& means and.

|| means or.

! means not.

use .equals to compare strings.

use % to check remainders and divisibility.

when tracing conditionals, go line by line and stop when the correct branch has been chosen.