3.1 The if Statement

Ace your homework & exams now with Quizwiz!

How does the character 'A' compare to the character 'B'?

'A' is less than 'B'

What are 2 style rules to adopt when writing "if" statements:

-The conditionally executed statement should appear on the line after the if statement. -The conditionally executed statement should be indented one level from the if statement.

Which relational operators test for two relationships?

>= and <=

Meaning: Is x not equal to y?

Expression: x != y

Meaning: Is x less than y?

Expression: x < y

True or False: In a decision structure's simplest form, a specific action is taken only when a specific condition exists, and if the condition does not exist, the action is not performed.

True

True or False: The == operator determines whether a variable is equal to another value, whereas the = operator assigns the value on the operator's right to the variable on its left.

True

decision structure

a specific action is taken only when a condition exists

All of the relational operators are _______.

binary

To create a block of statements, you enclose the statements in _______.

braces {}

symbol: ">"

greater-than

Assume hour is an int variable. Write an if statement that displays the message "Time for lunch" only if hour is equal to 12.

if(hour == 12) { System.out.println("Time for lunch"); }

boolean expression

is one that is either true or false.

True or False: A conditionally executed statement should be indented one level from the if clause.

true

True or False: Even though if statements usually span more than one line, they are technically one long statement.

true

Meaning: Is x less than or equal to y?

Expression: x <= y

Meaning: Is x equal to y?

Expression: x == y

Meaning: Is x greater than y?

Expression: x > y

Meaning: Is x greater than or equal to y?

Expression: x >= y

Given a double variable called average, write an expression that is TRUE if and only if the variable's value is less than 60.0.

average < 60.0

sequence structure

because the statements are executed in sequence, without branching off in another direction.

relational operator

determines whether a specific relationship exists between two values.

In a flowchart, which symbol represents a yes/no question or a true/false condition?

diamond

symbol: "=="

equal-to

True or False: If you inadvertently put a semicolon after the if (expression) portion of an if statement, the statement following the if will never execute.

false

A boolean variable that signals when some condition exists in the program is known as a _______.

flag

symbol: ">="

greater-than or equal to

What is the general format of the if statement?

if (BooleanExpression) statement;

Assume that the variables gpa, deansList and studentName, have been declared and initialized.Write a statement that adds 1 to deansList and prints studentName to standard out if gpa exceeds 3.5.

if (gpa > 3.5) { deansList++; System.out.println(studentName); }

Write an if statement that that decreases the variable shelfLife by 4 if the variable outsideTemperature is greater than 90. Assume the variables shelfLife and outsideTemperature have already been declared and assigned values.

if (outsideTemperature > 90) shelfLife -= 4;

Write an if statement that assigns the Boolean value true to the variable fever if the variable temperature is greater than 98.6.

if (temperature > 98.6) fever = true;

Write an if statement that assigns 0 to the variable b and assigns 1 to the variable c if the variable a is less than 10.

if(a < 10) { b = 0; c = 1; }

Write an if statement that assigns 10,000 to the variable bonus if the value of the variable goodsSold is greater than 500,000. Assume the variables bonus and goodsSold have already been declared.

if(goodsSold>500000) {bonus=10000;}

Write an if statement that multiplies payRate by 1.5 if hours is greater than 40.

if(hours > 40) { payRate *= 1.5; }

Write an if statement that sets the variable fees to 50 if the boolean variable max is true.

if(max) { fees = 50; }

Write an if statement that displays "Goodbye" if the variable myCharacter is equal to the character 'D'. ________ { System.out.println("Goodbye"); } Fill in the blank.

if(myCharacter == 'D')

Write an if statement that assigns 0.2 to commission if sales is greater than or equal to 10000.

if(sales >= 10000) { commission = 0.2; }

Write an if statement that assigns 20 to the variable y and assigns 40 to the variable z if the variable x is greater than 100.

if(x > 100) { y = 20; z = 40; }

Write an if statement that assigns 0 to x when y is equal to 20.

if(y == 20) { x = 0; }

A flag

is a boolean variable that signals when some condition exists in the program.

Unicode

is an international encoding system that is extensive enough to represent all the characters of all the world's alphabets.

if Statement

is used to create a decision structure, which allows a program to have more than one path of execution. The if statement causes one or more statements to execute only when a boolean expression is true.

symbol: "<"

less-than

symbol: "<="

less-than or equal to

symbol: "!="

not equal-to

An empty statement that does nothing is known as a _______ statement.

null

conditionally executed

performed only when a certain condition is true

Numeric data is compared in Java by using _______ operators.

relational

Code with a _______ structure contains statements that are executed in order, without branching off in another direction.

sequence

True or False: Indentation and spacing are for the human readers of a program, not the compiler.

true

Relational expressions are also known as boolean expressions, which means their value can only be _______.

true or false

Write an expression that evaluates to TRUE if and only if the value of the integer variable x is equal to zero.

x==0

Write an expression that evaluates to TRUE if the variable x is greater than or equal to the variable y.

x>=y

Write an expression that evaluates to TRUE if and only if the variable x is greater than the variable y.

x>y


Related study sets

Business Finance - Chapter 3 Quiz

View Set

Chapter 6 Terms of Trade or Incoterms

View Set

BIO Chapter 5 Practice - Tommy Minar

View Set

Valence Electrons and Lewis Dot Diagrams*

View Set