Click here to load reader

The switch Statement, DecimalFormat , and Introduction to Looping

  • Upload
    gin

  • View
    32

  • Download
    0

Embed Size (px)

DESCRIPTION

The switch Statement, DecimalFormat , and Introduction to Looping. CS0007: Introduction to Computer Programming. Review. You can test a series of condition with the… if-else if statement Nesting is… enclosing one structure inside of another. Binary logical operators combine… - PowerPoint PPT Presentation

Citation preview

PowerPoint Presentation

CS0007: Introduction to Computer ProgrammingThe switch Statement, DecimalFormat, and Introduction to Looping1ReviewYou can test a series of condition with theif-else if statementNesting isenclosing one structure inside of another.Binary logical operators combinetwo boolean expressions into one.Binary logical operators:&&||Unary logical operator:!

2ReviewWhat does the == operator compare when the operands are strings?Their referencesWhat should you use instead of logical operators when comparing strings?equalscompareToA variable has block-level scope ifIt is declared inside of a blockA variable that is declared inside of a block has scope beginning atIts declarationA variable that is declared inside of a block has scope ending atThe end of the block in which it was declared

3The switch StatementIt is often the case that you want the value of a single variable decide which branch a program should take:if (x == 1)statement or block 1if else (x == 2)statement or block 2if else (x == 3)statement or block 3elsestatement or block 4This is tedious and not very aesthetically pleasing.Java provides a structure that lets the value of a variable or expression decide which branch to takeThis structure is called a switch statement.

The switch StatementGeneral form of a switch statement:switch (SwitchExpression) {case CaseExpression1://One or more statementsbreak;case CaseExpression2://One or more statementsbreak;default://One or more statements}switch keyword that begins a switch statementSwitchExpression a variable or expression that has to be either char, byte, short, or int.case keyword that begins a case statement (there can be any number of case statements)CaseExpression1 a literal or final variable that is of the same type as SwitchExpression .The switch StatementGeneral form of a switch statement:switch (SwitchExpression) {case CaseExpression1://One or more statementsbreak;case CaseExpression2://One or more statementsbreak;default://One or more statements}Inside a case statement, one or more valid programming statements may appear.After the statement(s) inside of a case statements block, often the keyword break appears.After all of the case statements, there is the default case, which begins with the keyword default.The switch StatementGeneral form of a switch statement:switch (SwitchExpression) {case CaseExpression1://One or more statementsbreak;case CaseExpression2://One or more statementsbreak;default://One or more statements}What this does is compare the value of SwitchExpression to each CaseExpressions.If they are equal, the statements after the matching case statement are executed.Once the break keyword is reached, the statements after the switch statements block are executed.break is a keyword that breaks the control of the program out of the current block.If none of the CaseExpressions are equal to SwitchExpression, then the statements below the default case are executed. The switch Statementif (x == 1)y = 4;if else (x == 2)y = 9; elsey = 22;Is the same asswitch (x) {case 1:y = 4;break;case 2:y = 9; break;default:y = 22;}

switch Statement ExampleNew Topics:The switch Statement The switch Statement NotesThe CaseExpression of each case statement must be unique.The default section is optional.Again, the SwitchExpression and all of the CaseExpressions must be either char, byte, short, or int.Without the break; at the end of the statements associated with a case statement, the program falls through to the next case statements statements, and executes them.If this is what you actually want, then leave out the break;, but often it isnt.Why doesnt default statement have a break; at the end?The DecimalFormat ClassJava has a default way of displaying floating-point numbers, but it is often the case you want to display them in a particular formatThe Java API provides a class to do this called DecimalFormat.You create a DecimalFormat object much like how you created a Scanner object.DecimalFormat identifier = new DecimalFormat(formattingPattern);Also like when you create Scanner object, you must include an import statement before the class header:import java.text.DecimalFormat;The DecimalFormat ClassformattingPattern is an argument passed to the DecimalFormat constructor that tells it what format to display the floating-point numbers.Each character in the corresponds with a position in the number# - specifies that a digit should be displayed if present0 - specifies that a digit should be displayed if present, but if not 0 should be displayed% - placed at the end to multiply the number by 100 and place the % character at the end.To apply the format to a number, you must use the format method of the DecimalFormat object.decimalformatIdentifier.format(floatingPointNumber)

DecimalFormat ExampleNew Topics:DecimalFormat classThe printf methodAnother way to format output is with the printf method from the out object in the System class.System.out.printf(FormatString, ArgumentList);This is a formatting method taken from old C formattingIt is very powerful, and somewhat easy to use.The book goes over some examples of how it can be used.Also there are many, many examples of how to use this online.

LoopsSo far we have used decision structures to determine which statements are executed and which are not depending on a condition.We used this for:ValidationGeneral control flowMore specifically, weve used decision structures to execute statements that follow the condition one or zero times.What if we want the user to keep trying to put in valid input until she succeeds?How would we do this with decision structures?Can we?Answer: NoSolution: LoopsA loop is a control structure that causes a statement or group of statements to repeat.We will discuss three (possibly four) looping control structures.They differ in how they control the repitition.The while LoopThe first looping control structure we will discuss is the while loop.General Form:while (BooleanExpression) Statement or BlockFirst, the BooleanExpression is testedIf it is true, the Statement or Block is executedAfter the Statement or Block is done executing, the BooleanExpression is tested againIf it is still true, the Statement or Block is executed againThis continues until the test of the BooleanExpression results in false.Note: the programming style rules that apply to decision statements also apply.The while Loop FlowchartBoolean ExpressionStatement or BlockTrueFalsewhile Loop ExampleNew Topics:while Loop

Here, number is called a loop control variable.A loop control variable determines how many times a loop repeats.Each repetition of a loop is called an iteration.The a while loop is known as a pretest loop, because it tests the boolean expression before it executes the statements in its body.Note: This implies that if the boolean expression is not initially true, the body is never executed.

number