Chapter 4. 4-2 The Increment and Decrement Operators There are numerous times where a variable must...

65
Loops and Files Chapter 4

Transcript of Chapter 4. 4-2 The Increment and Decrement Operators There are numerous times where a variable must...

Loops and Files

Loops and FilesChapter 44-2The Increment and Decrement OperatorsThere are numerous times where a variable must simply be incremented or decremented.number = number + 1;number = number 1;Java provide shortened ways to increment and decrement a variables value.Using the ++ or -- unary operators, this task can be completed quickly.number++; or ++number;number--; or --number;24-3Differences Between Prefix and PostfixWhen an increment or decrement are the only operations in a statement, there is no difference between prefix and postfix notation. When used in an expression:prefix notation indicates that the variable will be incremented or decremented prior to the rest of the equation being evaluated.postfix notation indicates that the variable will be incremented or decremented after the rest of the equation has been evaluated.34-4Differences Between Prefix and Postfixint number = 4;System.out.println(number++);int number = 4;System.out.println(++number);PostfixPrefixnumber is displayed on the screen first, and then incremented.number is incremented first, and then displayed on the screenProgram output4Program output544-5Differences Between Prefix and Postfixint x = 1, y;y = x++;PostfixPrefixx is defined as an int and initialised with the value 1. y is declared an int.The value of x is assigned to the variable y and then incrementedAt the end of this statement: x = 2, y = 1int x = 1, y;y = ++x;x is defined as an int and initialised with the value 1. y is declared an int.The value of x is incremented and then assigned to variable yAt the end of this statement: x = 2, y = 254-6The while LoopJava provides three different looping structures.The while loop has the form:while(condition){statements;}While the condition is true, the statements will execute repeatedly.The while loop is a pretest loop, which means that it will test the value of the condition prior to executing the loop.64-7Care must be taken to set the condition to false somewhere in the loop so the loop will end.Loops that do not end are called infinite loops.A while loop executes 0 or more times. If the condition is false, the loop will not execute.The while Loop74-8statement(s)truebooleanexpression?falseThe while Loop Flowchart4-9The while Looppublic class WhileLoop{public static void main(String[] args){int number = 1;while (number 100){ System.out.println("That number is invalid."); System.out.print("Enter a number in the range of 1 through 100: ");

number = keyboard.nextInt();}

144-15The do-while LoopThe do-while loop is a post-test loop, which means it will execute the loop prior to testing the condition.The do-while loop (sometimes called a do loop) takes the form:do{statement(s);}while (condition);154-16The do-while Loop Flowchartstatement(s)truebooleanexpression?false4-17public static void main(String[] args){int score1, score2;double average;char repeat;String input;

Scanner keyboard = new Scanner(System.in);

do{System.out.print(Enter score #1: );score1 = keyboard.nextInt();System.out.print(Enter score #2: );score2 = keyboard.nextInt();keyboard.nextLine();

average = (score1 + score2) / 2.0;System.out.println(The average is : + average);System.out.println(Would you like to average another set of test scores?);System.out.print(Enter Y for yes or N for no: );input = keyboard.nextLine();repeat = keyboard.charAt(0);

} while (repeat == Y || repeat == y);}4-18The for LoopThe for loop is a pre-test loop.The for loop allows the programmer to initialize a control variable, test a condition, and modify the control variable all in one line of code.The for loop takes the form:for(initialization; test; update){statement(s);}184-19The for Loop Flowchartstatement(s)truebooleanexpression?falseupdate4-20The Sections of The for LoopThe initialization section of the for loop allows the loop to initialize its own control variable.The test section of the for statement acts in the same manner as the condition section of a while loop.The update section of the for loop is the last thing to execute at the end of each loop.204-21The for Loop InitializationThe initialization section of a for loop is optional; however, it is usually provided.Typically, for loops initialize a counter variable that will be tested by the test section of the loop and updated by the update section.The initialization section can initialize multiple variables.Variables declared in this section have scope only for the for loop.214-22The for Loopfor (count =1 ; count