WHILE STATEMENTS
This is the common structure of While statements initialize variables //initialize while (condition) { // test perform calculations and //loop change variables involved in the condition // body }
TRACING WHILE STATEMENT //Compute _ int sum = 0; cntr = 1; while (cntr <= 100) { sum += cntr; // point p cntr++; // point q } System.out.println(sum);
FOR STATEMENT The for statement combines initialization, condition test and update into a single expression. for (initialize counter; test counter; update counter) statement; //one statement inside the loop body
To understand the loop fully, we must analyze the way which the variables change on each iteration through the loop. On the 100 th iteration, cntr is increased to 101, so there is never a 101 st iteration, and we are confident that the sum is computed correctly. Iteration NumberValue of CNTR at point PValue of Sum at point PValue of CNTR at point Q …….…… …
TRACING PROBLEM //Display the sum of the integers between a startingValue // and an endingValue, using a designated increment. int cntr, sum, startingValue, endingValue, increment; startingValue = 10; endingValue = 100; increment = 7; sum = 0; cntr = startingValue; while (cntr <= endingValue){ sum += cntr; cntr += increment; } System.out.println(sum);
COUNTING BACKWARDS PROBLEM Work in BlueJ and turn in. You must use a While statement. Run the counter backward, to display the square roots of the number 25, 20, 15 and 10. Call the variable number.
REWRITE THE FOLLOWING PROBLEM USING A FOR LOOP //Display the sum of the integers between a startingValue // and an endingValue, using a designated increment. int cntr, sum, startingValue, endingValue, increment; startingValue = 10; endingValue = 100; increment = 7; sum = 0; cntr = startingValue; while (cntr <= endingValue){ sum += cntr; cntr += increment; } System.out.println(sum);
This is a rewrite of the program we traced using the chart. //Computer _ int sum = 0; cntr; for (cntr = 1; cntr <= 100; cntr++) sum += cntr; System.out.println(sum);
DECLARING THE LOOP CONTROL VARIABLE IN A FOR LOOP The loop control variables in the examples shown thus far have been declared outside of and above the loop structure. You can declare the variable inside the loop header. for (int r = 1; r <= 10; r ++) System.out.println(r);
IF STATEMENT The if statement is a conditional control statement of a decision statement, which executes a set of statements when a condition is true. if( ) { }
The condition of an if statement is a Boolean expression, which evaluates to either true or false.
IF-ELSE STATEMENT if (some condition){ do stuff; } else { do stuff; } What and does this mean? It means if some condition is true, do stuff 1, and if it is false, do stuff 2.
IF-ELSE IF STATEMENT The if-else if statement is used to decide among three or more actions and looks like this: if (condition) { statement } else if (condition) { statement } else { statement }
The logic used in developing an if-else if statement is important. For example, when testing a range of numbers, if conditions must be properly ordered because statements are executed for the first true condition only and then the program flow continues to the next statement after the if-else if.
Once upon a time in a faraway land, Jack visited his cousin Jill in the country and offered to milk the cow. Here are the instructions Jill gave Jack: Fetch the cow from the fields; Tie her in the stall; Milk her into the bucket; Pour the milk into the bottles; Drive her back into the field; Clean the bucket;
A year later, Jack visited again. In the meantime, Jill had acquired a herd of cows, some red and some black. This time, when jack offered to help, Jill gave him a more complex list of instructions. Herd the cows from the field into the west paddock; while (there are any cows left in the west paddock); fetch a cow from the west paddock: tie her in the stall; if (she is red){ milk her into the red bucket; pour the milk into red bottles; }else{ milk her into the black bucket; pour the milk into black bottles; } put her into the east paddock; } Herd the cows from the east paddock back into the fields; Clean the buckets;
ASSIGNMENT 1. Why does Jill use a while statement in her instructions to Jack? 2. Why does Jill use an if-else statement in her instructions to Jack?
GROUP ASSIGNMENT
Write pseudocode control statements that are similar in style to the farm example for your assigned statement. Also create a flow chart using the white paper. a. If a checker piece is red, then put it on a red square; otherwise, put it on a black square. b. If your shoes are muddy, then take them off and leave them outside the door. c. Pick up all the blocks on the floor and put them into a bag.
ADDITIONAL RELATIONAL OPERATORS OperatorWhat it Means >Greater than >=Greater than or equal to <Less than <=Less than or equal to ==Equal to !=Not equal to
Suppose a = 3c = 10 b = 7d = -20 Then based on the chart: a < b is true a <= b is true a == b is false a != b is true a – b > c + d is true a < b < c is invalid a == b ==c is invalid