COP2800 : Java Programming Chapter 4

Input, Selection, and Repetition

Section A Input and Decision Making

1) Interactive: A program that accepts values at run time is interactive because it exchanges communications, or interacts with the user. (p135)

2) Exception: is an error situation

3) throwing the Exception: this is required when; is used.
The code throws Exception after the main()header passes the error from keyboard input to the operating system. Without it, the program will not compile.
Exception must be written with uppercase E.

4) Prompt: a message requesting user input commonly is called a prompt because it prompts or coaches the user to enter an appropriate response.

5) Dissection of: userInput = (char);

6) Echoes: to repeat the input into a println statement
Example: System.out.println("You entered "+ userInput);
variable userInput is printed.

7) Boolean value: The value upon which a decision is made is always 1 of 2 values-true or false.

8) if statement: 1 statement you can use to make a decision.
if (condition)

9) single-alternative if: only perform an action based on 1 alternative.

10) dual-alternative if: you require 2 options for the next course of action.

11) if...else statement: provides the mechanism to perform 1 action when a Boolean statement evaluates as true and perform a different action when it is false.

12) Nested if statements: an if inside another if. Nested if statements are particularly useful when
2 conditions must be met before some action is taken.

Back to the Top

Section B Special Operators, the switch Statement, and Precedence

1) AND operator: && within a Boolean statement to determine whether 2 expressions are both true.
Using nested if statements always achieves the same result, but using && often makes your code more concise, less error-prone, and easier to understand.
If you do not have complete statement on each side of the &&, the code will not compile.
Example: if (value>1000 && value<5000) NOT if(value>1000 && <5000).

2) OR operator: || -> when you want some action to occur even if only 1 of 2 conditions is true.

3) Switch statement: alternative to a series of nested if statements. The switch statement is useful when you need to test a single variable against a series of exact integer or character values. The switch structure uses 4 new keywords:

Example of switch:
switch (value)
case 1:
case 2:
case 3:
default: Statement

4) conditional operator: requires 3 expressions separated with a question mark and a colon, and it is used as an abbreviated version of the if...else structure.
syntax example: teststatement ? true Result: false Result;

5) NOT operator: which is written as the exclamation point (!), too negate the result of any Boolean statement.

6) Precidence:

Precidence Operator(s) Symbols
Highest Multiplication, division *
  Addition, subtraction +
  Relational >
  Equality ==
  Logical AND &&
  Logical OR ||
  Conditional ?:
Lowest Assignment =

Back to the Top

Section C Looping and Shortcut Arithmetic

1) Loop: a structure that allows repeated execution of a block of statements.

2) Loop body: within a looping structure, a Boolean statement is evaluated. If it is true, then a block of statements, called the loop body, executes and then the Boolean statement is evaluated again.

3) while loop: to execute a body of statements continuously while some condition continues to be true. A while loop consist of the keyword while followed by a boolean statement within parentheses, fallowed by the body of the loop, which can be 1 or many statement surrounded by curly brackets.

4) Infinite loop: a non-terminating loop

5) To prevent a while loop from being infinite:

6) empty body: IF a semicolon mistakenly is place at the end of the partial statement
while(loopCount < 3);
the loop is also infinite.
This loop has an empty body, or a body with no statements in it, so the boolean statement is evaluated, and because it is true, the loop body is entered.--- infinite loop occurs.

7) Incrementing or decrementing: This is a common way to alter the value of the control variable by either adding (incrementing) or subtracting (decrementing) by one.

8) Sentinel value: A value that a user must supply to stop a loop.

9) Shortcut Arithmetic Operations:
+=, -=, *=, /= (performs operation and assigns the result in 1 step)
To increase a variable's value by exactly one: prefix++ postfix++
You can use this on a variable only... can't change a constant.
Prefix++ the result is calculated and stored, and then the value is used.
Example: if b=4; then c=++b, results b=5, c=5.
Postfix++ the variable is used and then the result is calculated and stored.
Example: if b=4; then c=++b, results b=4, c=4 then after the assignment b=5.
Example: if d-8, e=8, ++d==9, e++==8 is true statement.
To Decrease a variable's value by exactly one: prefix-- postfix-- same is true for decrement as for increment.

10) unary: prefix and post fix increment operations are unary operations because you use them with 1 value.

11) Binary: most arithmetic operations, like those used in addition or multiplication, are binary operators that operate on 2 values.

12) definite loop or counted loop: A loop that executes a specific number of times.
To write a definite loop, you initialize a loop control variable, and while the loop control variable doesn't pass a limit, you continue to execute the body of the while statement. To avoid an infinite loop, you must include in the body of the while loop a statement that alters the loop control variable.

13) for loop: There are 3 Sections with the parentheses of a for loop that are separated by exactly 2 semicolons.
Example: for (int val=1; val < 11; ++val)
int val=1;     Initializing the loop control variable
val < 11;     Testing the loop control variable
++ val      Updating the loop control variable

14) do...while: will execute at least once.

15) To break from a loop: press Ctrl+C or Ctrl + Break.

Back to the Top

Links to COP2800 Java Programming Chapters:
Links to the to help with Java Programming

Back to the Top