CS140, TSU Control Statements: Part I by Li Ma In this chapter (Section 4.1 – 4.13) and Chapter 5 (Section 5.1 – 5.9), the issues on structure of Java programming are presented, the theory and principles of structuring programming are discussed. You will learn to use basic problem-solving techniques to develop algorithms through the process of top-down, stepwise refinement to use the if and if…else selection statements to choose among alternative actions to use the while repetition statement to execute statements in a program repeatedly to use counter-controlled repetition and sentinel-controlled repetition to use the assignment, increment and decrement operators the portability of primitive types Algorithms and Pseudocode An algorithm is a procedure for solving a problem in term of 1. the actions to execute 2. the order in which these actions execute Specifying the order in which statements (actions) execute in a program is called program control. We use control statements to perform program control. Pseudocode is an informal language that helps you develop algorithms without having to worry about the strict details of Java language syntax. It helps you “think out” a program before attempting to write it in a programming language Formulating Algorithms To solve some problem using computer program, we need to follow these steps to develop the algorithms and implement it in a program 1. Providing the problem statement 2. Using pseudocode to illustrate the algorithms 3. Implementing in programming language 1 CS140, TSU Control Statements: Part I by Li Ma Top-down, stepwise refinement technique is essential to development of well-structured programs. 1. Top: a single statement that conveys the overall function of the program o A complete representation of a program Determine the class average for the quiz 2. Refinement process: divide some statement into a series of smaller tasks and list these in the order in which they will be performed o The first refinement: divide the top statement Initialize variables Input, sum and count the quiz grades Calculate and print the class averagez o The second refinement: further refinement Initialize total to zero Initialize counter to zero Prompt the user to enter the first grade Input the first grade (possibly the sentinel) While the user has not yet enter the sentinel Add this grade into the running total Add one to the grade counter Prompt the user to enter the next grade Input the next grade (possibly the sentinel) If the counter is not equal to zero Set the average to the total divided by the counter Print the average Otherwise Print “No grades were entered” Now we have the pseudocode for the program. Next step would be to convert this pseudocode into a program. 2 CS140, TSU Control Statements: Part I by Li Ma Control Structures Sequential execution is a process that statements are executed one after the other in order in which they are written. But sometime it is necessary to “jump” Transfer of control: next statement to execute is not the next one in sequence. In the old program structures, o goto statement o structured programming root of much difficulty in programming Java does not use goto statement to control programming Three control structures Sequence structure o Just like the program we have created so far Selection structure o Single-selection statement: if statement o Double-selection statement: if … else statement o Multiple-selection statement: switch statement Repetition structure o Looping statement: while, do … while, and for statements Selection Statements if statement is a single-entry/single-exit control statement action is performed under some condition if … else statement: two branches to go perform one of two actions regarding to the value of the conditional expression switch statement: will be discussed in Chapter 5 The conditional operator (?:) is the only ternary operator in Java. A conditional expression takes a Boolean expression as the first operand, and two values (or expressions with value) as second and third operands. For example, 3 CS140, TSU Control Statements: Part I by Li Ma string finalGrade = finalScore >= 60 ? “Passed” : “Failed”; Nested if … else statements In one of two branches, there is another if … else statement. Using nested if … else statements may cause some problem Dangling-else problem o Java compiler always associates an else with the immediately preceding if o But the nested else statement matches to the wrong if statement while Repetition Statement A repetition (looping) statement allows you to specify that a program should repeat an action while some condition remains true. Counter-controlled repetition o A counter (or control variable) is used to control the number of times a set of statements will execute o It is called definite repetition since the number of repetitions is known before the loop executes Sentinel-controlled statement o A set of statements execute for an arbitrary number of times o A sentinel value (signal value, dummy value, or flag value) is used to indicate the end of data entry o It is called indefinite statement since the number of repetitions is unknown Compound Assignment Operators The compound assignment operators abbreviate assignment statements. The following two assignment statements are equivalent (see Fig. 4.13 on page 131) variable = variable operator expression; variable operator= expression; Increment and Decrement Operators Java provides two unary operators (see Fig. 4.14 on page 131) 4 CS140, TSU Control Statements: Part I by Li Ma Increment operator ++ o Prefix increment o Postfix increment Decrement operator – o Prefix increment o Postfix increment Fig. 4.16 shows the precedence and associativity of the operators discussed so far. Primitive Types Java is referred to as a strongly typed language. There are eight primitive data types in Java. Each class in Java is a reference type. The primitive types in Java are portable across all computer platforms that support Java. For primitive type, there are two different type conversions Type casting: explicit conversion, using unary cast operator o (double) intValue1 / intValue2; Promotion: implicit conversion o For example, an integer will be promoted to a floating-point number 5