Presentation is loading. Please wait.

Presentation is loading. Please wait.

Review of Previous Lesson

Similar presentations


Presentation on theme: "Review of Previous Lesson"— Presentation transcript:

1 Review of Previous Lesson
11/05/2019 Review of Previous Lesson State as many Vocabulary words and Learning Objectives that you remember from the last lesson as you can. Remember to grade yourself from

2 11/05/2019 Loops / Iteration for

3 Language Features and other Testable Topics
11/05/2019 Tested in the AP CS A Exam Notes Not tested in the AP CS A Exam, but potentially relevant/useful Operators Increment/Decrement: ++, −− 1, 2 Control Statements for break, continue

4 Language Features and other Testable Topics
11/05/2019 Notes: 1. Students are expected to understand the operator precedence rules of the listed operators. 2. The increment/decrement operators ++ and −− are part of the AP Java subset. These operators are used only for their side effect, not for their value. That is, the postfix form (for example, x++) is always used, and the operators are not used inside other expressions. For example, arr[x++] is not used.

5 Types of Operator in Java
11/05/2019 Types of Operator in Java Basic Arithmetic Operators Assignment Operators Unary +, - Relational (comparison) operators Logical Operators Unary Auto-increment & Auto-decrement Operators Concatenation Bitwise Operators Ternary Operator Types have been covered in previous presentations. We will cover type 6 in this presentation.

6 6. Unary Auto-increment & Auto-decrement Operators
11/05/2019 6. Unary Auto-increment & Auto-decrement Operators int x = 1, y = 2; x++; // x is now 2 y --; // y is now 1

7 Operator Precedence Rules
11/05/2019 Operator Associativity unary pre-increment/decrement ++ -- + - (logical) ! not associative cast () right to left multiplicative * / % left to right additive + - relational < <= > >= equality == != logical && || assignment = += -= *= /= %= Most programmers do not memorize these, and even those that do still use parentheses for clarity, so I am not sure why the AP syllabus mentions them. Associativity. When an expression has two operators with the same precedence, the expression is evaluated according to its associativity. e.g. x = y = z = 17 is treated as x = (y = (z = 17)), leaving all 3 variables with the value 17, since the = operator has right-to-left associativity (and an assignment statement evaluates to the value on the right hand side). On the other hand, 72 / 2 / 3 is treated as (72 / 2) / 3 since the / operator has left-to-right associativity. Some operators are not associative: for example, the expressions (x <= y <= z) and x++-- are invalid. Decreasing Precedence

8 Ranks of Operators 11/05/2019 ! -(unary) ++ -- (cast) * / % + -
Highest ! -(unary) (cast) * / % < <= > >= == != && || Lowest

9 Post-Increment/prefix (++a) Vs Pre-Increment/postfix (a++)
11/05/2019 Post-Increment/prefix (++a) Vs Pre-Increment/postfix (a++) However, you may see these equivalent methods on an AP exam: int i = 1; int x = i++; //x is 1, i is 2 int y = ++i; //y is 3, i is 3 i++ does the increment after returning i. ++i does the increment before returning i. However, the AP Computer Science states: These operators are used only for their side effect, not for their value. That is, the postfix form (for example, x++) is always used, and the operators are not used inside other expressions. For example, arr[x++] is not used. Therefore, this issue is irrelevant to your AP exam as only pre-increment will be used and it will never be used inside expressions. Note that due to the above, for simple increments/decrements, it doesn’t actually matter whether the pre or post form is used. int i = 1; equivalent int x = i; i++; equivalent i++; y = i;

10 11/05/2019 Loops Used to execute a set of statements repeatedly until a particular condition is satisfied. In Java we have three types of basic loops: for while do while In this presentation we will learn how to use “for loop” in Java.

11 Syntax of for loop: 11/05/2019 for(initialization; condition ; increment/decrement) { statement(s); } Each execution of a loop body is called an iteration. ‘counter’ variable Output: ? int i=10 initialization expression i>1 condition(Boolean expression), loops stops when this is False i-- Decrement operation

12 Infinite for loop When will this loop end?
11/05/2019 When will this loop end? The loop will never end as the condition would never return false. The initialization step is setting up the value of variable i to 1, since we are incrementing the value of i, it would always be greater than 1 (the Boolean expression: i>1). This is called an infinite loop. It is important to see the co-ordination between Boolean expression and increment/decrement operation to determine whether the loop would terminate at some point of time or not.

13 11/05/2019 Scope The scope of a variable is the region in which the variable is visible/can be accessed. This is inside the {} pair in which it is declared. Therefore, variables declared inside a loop {}, for example, have a scope limited to that loop (meaning that they don’t exist outside the loop if they were declared inside a loop). This includes the counter variable i in the example below.

14 Scope So can you see a problem below?
11/05/2019 Scope So can you see a problem below? for (int i = 1; i <= 4; i++) { System.out.println(i); }

15 Scope So can you see a problem below?
11/05/2019 Scope So can you see a problem below? for (int i = 1; i <= 4; i++) { System.out.println(i); } /* The following line will not compile as Javac will complain that i has not been declared! */

16 break 11/05/2019 Used to come out of a loop instantly.
Whenever a break statement is encountered inside a loop, the control directly comes out of loop and the loop gets terminated for rest of the iterations. It is used along with if statement, whenever used inside loop so that the loop gets terminated for a particular condition. Output: class ForLoopExample { public static void main(String args [ ] ){ for (int x =10; x < 50; x += 10){ if (x == 30) break; System.out.println(i); } ?

17 continue 11/05/2019 Causes the loop to immediately jump to the next iteration of the loop. Useful when you want to continue the loop but do not want the rest of the statements (after the continue statement) in the loop body to execute for that particular iteration (equivalent to ‘skip’/’miss out’). Output: class ForLoopExample { public static void main(String args [ ] ){ for (int x =10; x < 50; x += 10){ if (x == 30) continue; System.out.println(i); } ?

18 Commenting on for loops
11/05/2019 Commenting on for loops Your comments MUST explain: What are you looping for? What are conditions for the loop? e.g. How many times does it loop? Why does it start there and end there? When (after and before what) are you looping and why does it have to be there?

19 11/05/2019 Write your own programs: Write your own programs from “scratch”. Of course you should use previous programs for reference, but write your code from “scratch” (do not copy and paste).

20 Multiplication Table Specification: 11/05/2019
For a given number, output the multiplication table for their number. Stop numbers other than those from 2 to 12 inclusive.

21 Between Two Numbers 1 Write a program that: 11/05/2019
For two different numbers. Shows all the numbers from the first value to the second value in a list box. e.g. For 6 and 9, show

22 Between Two Numbers 2 Write a program that: 11/05/2019
For two different numbers. Show only numbers between two values not the values themselves.

23 Between Two Numbers 3 Write a program that: 11/05/2019
For two different numbers. What happens if the second number is lower than the first number? Disallow the second number to be lower than the first number.

24 Between Two Numbers 4 Write a program that: For two different numbers.
11/05/2019 Between Two Numbers 4 Write a program that: For two different numbers. 2 options: If number1 > number2, display numbers from number1 to number2. If number2 > number1, display numbers from number2 to number1.

25 11/05/2019 Factorial Write a program to work out the factorial of a given number. e.g. If 4 is entered then the program should calculate 1*2*3*4 = 24 This is the number of combinations e.g. for 4 letters abcd there are 24 ways of arranging these letters. Hints: ‘Declare the variable Result as an integer but give it an initial value of 1 (instead of 0 as is normally the case). Start your loop from 2 to the number entered as Result is already 1 to begin with so: 1*2*…. to the number entered. ‘Carry on the Result from last time e.g. 1*2*3*4 …. by using the following line: Result *= What happens if the given number is negative? Stop a negative number being given. Make sure you allow for the factorial of 1 though but avoid an infinite loop.

26 Adding Numbers from 1 to a chosen number
11/05/2019 Adding Numbers from 1 to a chosen number Write a program to “add all numbers from 1 to a chosen number”. e.g. If 4 then = 10 If 6 then = 21 etc..... What happens if a zero or negative number is given? Stop a zero or a negative number being given.

27 11/05/2019 Fibonacci Series The Fibonacci sequence is a series of numbers where a number is the sum of previous two numbers. Starting with 0 and 1, the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on. For a given number, give the Fibonacci Series e.g. Fibonacci Series of 7 numbers:

28 Adding Numbers from one chosen number to another chosen number
11/05/2019 Adding Numbers from one chosen number to another chosen number Write a program to “add all numbers between two chosen numbers”. e.g. If 4 and 7 then = 22 If 10 and 12 then = 33 etc..... What happens if a negative number is given? Should you stop a zero or a negative number being given? What should you stop?

29 Adding Numbers from one chosen number to 6 then add 1
11/05/2019 Adding Numbers from one chosen number to 6 then add 1 Write a program to “one chosen number to 6 then add 1”. e.g. If 4 then = 16 If then = 12 Make sure 7 can be entered as the result should = 1 etc..... What happens if 8 or more is entered? Do not allow the user to do this.

30 5/11/2019 Grade yourself Grade yourself on the vocabulary and learning objectives of the presentation.


Download ppt "Review of Previous Lesson"

Similar presentations


Ads by Google