Java Loops and Switches

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

A switch can only test for equality using the equality operator (==). (T/F)

True.

The exclamation mark (also called the 'bang!' operator) is the logical NOT (!) operator. (T/F)

True.

The input value will "fall through" the switch cases till it finds a matching value. (T/F)

True.

The only way to get a TRUE result from a logical AND (&&) operation is for both conditions to be true. (T/F)

True.

There are three possible ways to get a TRUE result from a logical OR (||) operation. The only way to get a FALSE is for both conditions to be FALSE. (T/F)

True.

Break Keyword

Used to break out of a loop if a certain condition is met.

Sentinel Value

Will cause the LCC to return false and complete the while loop *marks the end of user input

Continue Keyword

Will stop further execution of the current iteration of a loop, and then evaluate the LCC and continue if it is true.

What would be the value of x after the following statements were executed? int x = 15; switch (x) { case 10: x += 15; break; case 12: x -= 5; break; }

15

What will be the value of x after the following code is executed? int x = 10; do { x *= 20; } while (x < 5);

200

What would be the value of x after the following statements were executed? int x = 10; switch (x) { case 10: x += 15; break; case 12: x -= 5; break; default: x *= 3; }

25

What will be the value of x after the following code is executed? int x = 10; for (int y = 5; y < 20; y +=5) x += y;

40

What would be the value of x after the following statements were executed? int x = 15; switch (x) { case 10: x += 15; break; case 12: x -= 5; break; default: x *= 3; }

45

What would be the value of x after the following statements were executed? int x = 12; switch (x) { case 10: x += 15; break; case 12: x -= 5; break; default: x *= 3; }

7

LCC

Loop Continuation Condition *Evaluates to a boolean value of T/F

Index

Position of a character in a string. Starts at 0.

Indeterminate Loop Situation:

Situations where we do NOT know in advance how many times we may have to repeat (iterate) the loop body code. *Usually a WHILE or DO WHILE loop.

Determinate Loop Situation:

Situations where we know ahead of time exactly how many times our code will have to repeat (iterate) the loop body code. *Usually a FOR loop, can also be COUNTER CONTROLLED WHILE LOOP

Flow of Control

The order in which statements are executed in a running program

Validating Data

The process of ensuring that a value falls within a specified range.

Scope

The set of statements that can access a declared variable.

for(int count = 0; count < 5; count++) ; Why is it wrong to put the semi-colon at the end?

This separates the loop header from the code in the loop body. *logic error

For Loop

This type of loop is ideal in situations where the exact number of iterations is known.

Do-While Loop

*Post-test loop The loop body code is run first and the LCC is checked after *This loop will ALWAYS run at least ONCE *Often used as a data-validation loop

While Loop

*Pretest loop The LCC is checked BEFORE trying to run the loop body code. *must include something that will eventually cause the LCC to return false and complete the loop

For Loop

*Used when we know in advance how many times we want the loop to execute Has 3 steps: 1. Setting initial conditions + creating and initializing the loop control variable 2. Specifying the LCC 3. Arranging for the loop control variable to get incremented after each iteration

Bohm and Jacopini paper proved that GOTO was not needed and that all programs could be written using the three primary control structures of:

-Sequence (lines of code execute in sequential order) -Selection (conditional structures such as 'if-then-else') -Repetition (loop structures) *SSR!*

Three main types of loops:

1. FOR loop 2. WHILE loop 3. DO WHILE loop

Unary Operators

1. Increment (++) 2. Decrement (--) *Commonly used to control loop counter variables

Two types of loop situations:

1. Indeterminate 2. Determinate

How many times will this loop be executed? int n; for(n = 0; n < 10; n++) System.out.print(n);

10

CONTINUE vs BREAK keywords

A continue keyword stops only the current iteration of the loop. Flow of control goes back to the loop condition to see if any more iterations should be done. The break keyword causes control to break out of the loop body completely, no matter how many iterations might remain. Flow of control proceeds to the next statement after the loop structure.

for : each loop

Allows you to process all the data values stored in the storage structure without having to worry about how many elements there are in the structure -was added to Java in the 1.5 update to make it easier to process data in storage structures such as arrays, ArrayLists, and Vectors

Switch Statement

Causes the executing program to follow one of several paths based on a single value (works with "break"). "default" is optional. The expression evaluated must be an "integral type" (either 'int' or 'char'), does NOT work with boolean, float or other integer type (short, long, and byte)

A switch can use relational operators (>, <). (T/F)

False.

How many times will this loop be executed? int m = 1; while(m < 10) System.out.print(m); m++;

Infinite number of times. Look carefully at the *;*

Control variables in a for loop are declared in the:

Initialization section.

The proper way to format a switch case:

case 1: statement1

Syntax of the For Loop Declaration Header

for(expression1; expression2; expression3) { //loop body statement(s) ; } expression1 declares and initializes the the loop control variable or loop counter. This part of the loop header runs just once, at the start of the loop. expression2 is the loop continuation condition expression. It is re-evaluated after each iteration of the loop. expression3 is the loop counter adjustment expression. It is the last action done in an iteration of the body code before the loop goes back to check the LCC again.


Kaugnay na mga set ng pag-aaral

Chapter 9: The Finances of Housing

View Set

Chapter- 16 HW https://quizlet.com/_3g6tak

View Set

Second quarter English exam Frankenstein

View Set

Organizational Behavior Chapter 1-4

View Set

AP EURO Chapter 16 Part 1 Quiz Answers

View Set