Chapter 4: Conditionals and Loops

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

What is the conditional operator?

-a ternary operator (req. 3 operands); like an if-else -uses ? and : -returns the value of the first expression if the condition is true and the second expression if the condition is false -ex: total = (total > MAX) ? total + 1 : total*2; equivalent to: if(total > MAX) total = total + 1; else total = total*2;

loop (or repetition statement)

-allows us to execute a statement over and over again -a boolean expression determines how many times the statement is executed -includes: for statement, while statement, do statement

block statement

-collection of statements enclosed in braces -used w/ if-else statements so that multiple statements can correspond w/ the condition -indentation ONLY means something to the human reader

comparing characters in boolean expressions

-each character has a Unicode number, determining their value

What is the switch statement?

-evaluates an expression (must be byte, char, short, or int) to determine a value; the value then matches a case which has a statement -ex: switch(id) case '1' : id = 1; break; case '2': id = 2; break; default: id = 0; -break ends it and goes past the switch statements -default use if no case matches -could be written as nested if statement

boolean expression (or condition)

-expression that evaluates to true or false -in a conditional / loop determines what statement will be executed next

for header

-for (initialization; boolean condition; increment) -initialization: executed once before loop starts; creates variable (int count = 1) - not req'd -boolean condition: evaluated before loop starts each time; continues looping as long as condition is true -increment: executed at the end of each loop, performs a calculation on the loop variable -can have multiple initialization / update statements

for-each statement

-if you have an iterable object (bookList) containing objects (books), you can loop over each one -for (Book mybook : bookList) { statement w/ mybook; }

conditional (or selection) statement

-lets us choose which statement will be executed next -includes: if statement, if-else statement, switch statement

do statement

-like a while loop, but the condition is evaluated AFTER the body is executed do { statement; } while (condition);

while statement

-like an if statement, but keeps repeating until the condition is false -use when you don't know how many times you want to go through the loop (b/c user input, etc) -while (expression) { statement; }

iterator

-object w/ methods that allow you to process a collection of items at one time -has the boolean method hasNext which tells you whether there's another item -the Scanner is an iterator

flow of control

-order in which statements are executed in a program -starts @ first line of main method and continues on down

logical operators

-produce boolean results -! - not (gives the opposite boolean value) -&& - and (true if both sides are true; false otherwise) -|| - or (true if either side is true; false otherwise) -precedence: !, &&, ||

comparing float in boolean expressions

-two floats are only equal if they are EXACTLY equal -- try not to compare floats on equality -instead, compare the absolute value of the difference between two floats

for statement

-use when you know how many times you want to execute the statement -aka a do-n-times loop for (initialization; condition; increment) { statement; }

sentinel value

-value that signals the program to stop; not a normal input -can use in while loop -ex: "Enter a number (-1 to quit)"; -1 is the sentinel value makes while loop stop ex) while(points != -1)

delimiter

-what separates the input tokens to a scanner -scanner jumps over the text until it finds the delimiter and then returns everything it just jumped over -usually white spaces, but can set it to \ or other characters by doing scannerName.useDelimiter("/")

infinite loop

-when the condition of the loop never becomes false, so it repeats forever

variable.Equals(var) vs. ==

== works for primitive types variable.Equals works for classes (object that has actions; String) - == w/ classes sees if they are located @ same address in memory

How do you set up an if statement?

if (boolean expression) ---> statement; else ---> statement; -to do elif, nest if statements -indentation ONLY means something to the human reader

How do you sort strings alphabetically?

string1.compareTo(string2) --> gives a negative integer if string1 comes before string2; positive if vice versa -lexicographic ordering -- capitals matter

How do you tell if two strings are equal?

string1.equals(string2) --> true if they're the same, false if they're different - ==s tests if the strings have the same address / are aliases

nested loops

-a loop inside a loop -inner loop runs the number of times it runs * the number of times the outer loop runs

equality operators

- == (equal) and != (not equal) -used in boolean expressions -lower precedence than arithmetic operators

relational operators

- >, <, >=, <= -used in boolean expressions -lower precedence than arithmetic operators


Ensembles d'études connexes

Injections/ Eye and Ear procedures

View Set

RHIT Practice Exam 2 - 150 Questions

View Set

1.2 Hematology: Normocytic and Normochromic Anemias

View Set

Organization and Innervation of the Head and Neck

View Set

Chapter 33: Alterations in Cognitive and Mental Health, Chapter 29: Alterations in Integumentary Function, Chapter 27: Alterations in Musculoskeletal Function, Chapter 30: Alterations in Immune Function, Chapter 31: Alterations in Endocrine Function,...

View Set

Physics Final Exam: Test 2 Material Review (CIRCUITS)

View Set

Chapter 14 - Retailing, Direct Marketing, and Wholesaling

View Set