Java Chapter 3

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

&&

and

boolean expression

any variable or calculation that results in a true or false condition.

If the SwitchExpression matches the CaseExpression, the Java statements

between the colon and the break statement will be executed.

•A flag is a boolean variable that monitors some condition in a program.

boolean highScore;

Each case statement will have a corresponding CaseExpression that must be unique.

case CaseExpression: // place one or more statements here break;

•Characters can be tested with relational operators. •Characters are stored in memory using the Unicode character format. •Unicode is stored as a sixteen (16) bit number. (ASCII 8 bits) •Characters are ordinal, meaning they have an order in the Unicode character set. Since characters are ordinal, they can be compared to each other.

char c = ′A′; //deci for A is 65 or 0041 if(c < ′Z′) //deci for Z is 90 or 005A System.out.println("A is less than Z");

Care must be used since •match up with the immediately preceding unmatched if statement.

else statements

The default section is optional and will be

executed if no CaseExpression matches the SwitchExpression

The if statement decides whether a section of code

executes or not

•The ! operator has a order of precedence than the && and || operators.

higher

If an expression is true, !expression will be false.

if (!(temperature > 100)) System.out.println("Below the maximum" + " temperature.");

•An if statement can span more than one line; however, it is still one statement.

if (average > 95) grade = ′A′; is functionally equivalent to if(average > 95) grade = ′A′; if (average > 95) { grade = ′A′; System.out.println(grade); } //end if

•When a condition is true, the flag is set to true. •The flag can be tested to see if the condition has changed.

if (average > 95) highScore = true; //highScore is a flag •Later, this condition can be tested: if (highScore) System.out.println("That′s a high" + "score!");

•The if statement uses a boolean to decide whether the next statement or block of statements executes.

if (boolean expression is true) execute next statement. if ( xInt = = 70) System.out.println( "it's a C"); else System.out.println( xInt += 10);

•Remember that when the curly braces are not used, then only the next statement after the if condition will be executed conditionally.

if (expression) statement1; statement2; statement3;

•The if-else statement adds the ability to conditionally execute code when the if condition is false.

if (expression) statementOrBlockIfTrue; else statementOrBlockIfFalse;

using curly braces {} to enclose them

if (expression) { statement1; statement2; }

Logical operators

&& || !

order of precedence

(unary negation) ! */% + - < > <= >= == != && || = += -= *= /= %=

Logical AND will evaluate to false as soon as it sees that one of its

operands is a false expression

The break statement is

optional

||

or

•can be used to force the precedence to be changed.

parenthesis

If a case does not contain a break, then

program execution continues into the next case

However, sometimes curly braces can help make the

program more readable

Additionally, makes it much easier to match up else statements with their corresponding if statement.

proper indentation

In most cases, the boolean expression, used by the if statement, uses

relational operators.

The nested if is executed only if the outer if statement

results in a true condition

Logical AND and logical OR operations perform

short-circuit evaluation of expressions

switch

statement allows you to use an ordinal value to determine how a program will branch can evaluate an integer type or character type variable and make decisions based on the value.

•The switch statement takes the form:

switch (SwitchExpression) { case CaseExpression: // place one or more statements here break; case CaseExpression: // place one or more statements here break; // case statements may be repeated //as many times as necessary default: // place one or more statements here }

case statement

that matches that value, program execution will be transferred to that case statement

Curly brace use is not required if there is only one statement

to be conditionally executed

Unary negation !

unary negation logical NOT

Conditionally executed statements can be grouped into a block by

using curly braces {} to enclose them

•Rules of thumb:

§The conditionally executed statement should be on the line after the if condition. §The conditionally executed statement should be indented one level from the if condition. §If an if statement does not have the block curly braces, it is ended by the first semicolon encountered after the if condition. if (expression) statement;

•In the String class the equals and compareTo methods are case sensitive. •In order to compare two String objects that might have different case, use:

§equalsIgnoreCase, or §compareToIgnoreCase

•The format of the operators is: BooleanExpression ? Value1 : Value2 •This forms a conditional expression.

•If BooleanExpression is true, the value of the conditional expression is Value1. •If BooleanExpression is false, the value of the conditional expression is Value2.

In most cases, you cannot use the relational operators to compare two

•String objects.

•The logical AND operator (&&) takes two operands that must both be boolean expressions.

•The resulting combined expression is true if (and only if) both operands are true.

If an if statement appears inside another if statement (single or block) it is called

•a nested if statement.

The scope of a local variable

•begins at the point it is declared and terminates at the end of the method.

Nested if statements

•can become very complex.

The break statement ends the

•case statement.

Reference variables

•contain the address of the object they represent.

In Java, a local variable

•does not have to be declared at the beginning of the method.

The if-else-if statement makes certain types of nested decision

•logic simpler to write.

The ! operator performs a

•logical NOT operation.

Logical OR will evaluate to true as soon as it sees that one of its

•operands is a true expression.

You can use the System.out.printf method to

•perform formatted console output.

if-else

•statement allows you to make true / false branches.

The conditional operator is a

•ternary (three operand) operator.

When a program enters a section of code where a variable has scope, that variable has come into scope, which means

•the variable is visible to the program.

relational operators (6of them)

> < >= <= == !=

•Many times the conditional operator is used to supply a value. number = x > y ? 10 : 5; •This is functionally equivalent to:

if(x > y) number = 10; else number = 5;

==

is equal to

>

is greater than

>=

is greater than or equal to

<

is less than

<=

is less than or equal to

!=

is not equal to

•The String.format method works exactly like the System.out.printf method, except that it does not display the formatted string on the screen. Instead,

it returns a reference to the formatted string

•The && and || operators have a precedence than relational operators like < and >.

lower

!

not

Unless the references point to the same object, the relational operators will

not return true


संबंधित स्टडी सेट्स

Derm Qbank: Surgical, Laser, Cosmetic

View Set

Ch. 9 emotion in relationships and society

View Set

Chapter 15: Respiratory Emergencies

View Set

PTSD: Post Traumatic Stress Disorder

View Set

Taylor Nursing Fundamentals CH40 - Fluid & Electrolytes

View Set

Chapter 50: Diabetes Mellitus and the Metabolic Syndrome

View Set

Exam 3: Nursing I: Module 6, 7, 8

View Set

Ch 1 What does it mean to be human?

View Set

HUM2020 Exam 3 Frederick Owens TCC

View Set