Review of Previous Lesson

Slides:



Advertisements
Similar presentations
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Advertisements

CS150 Introduction to Computer Science 1
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
C programming: Variables, Expressions part II. Data Types of Arithmetic Expressions Relational Expressions Logical Expressions Multiple Assignments Compound.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.-Edited By Maysoon Al-Duwais1.
CompSci 230 S Programming Techniques
ECE Application Programming
Information and Computer Sciences University of Hawaii, Manoa
Chapter 4 – C Program Control
UNIT 5 Lesson 15 Looping.
Java Language Basics.
CprE 185: Intro to Problem Solving (using C)
Chapter 6: Loops.
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Lecture 3 Java Operators.
Chapter 12 Variables and Operators
Introduction to Computer Science / Procedural – 67130
Lecture 7: Repeating a Known Number of Times
for Repetition Structures
Lecture 4: Program Control Flow
Primitive Data, Variables, Loops (Maybe)
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Chapter 4 – Control Structures Part 1
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
OPERATORS (1) CSC 111.
Arrays, For loop While loop Do while loop
MSIS 655 Advanced Business Applications Programming
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
OPERATORS (2) CSC 111.
Outline Altering flow of control Boolean expressions
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
Chapter 7 Additional Control Structures
Introduction to Object-Oriented Programming with Java--Wu
3 Control Statements:.
Chapter 6 Control Statements: Part 2
Chapter 6: Repetition Statements
3.1 Iteration Loops For … To … Next 18/01/2019.
Associativity and Prescedence
Computing Fundamentals
Data Types and Expressions
Repetition Statements (Loops) - 2
Review of Previous Lesson
Review of Previous Lesson
Review of Previous Lesson
Chap 7. Advanced Control Statements in Java
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Review of Previous Lesson
Loops and Iteration CS 21a: Introduction to Computing I
Review of Previous Lesson
Review of Previous Lesson
Data Types and Expressions
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Looping and Repetition
Data Types and Expressions
Presentation transcript:

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 0 - 3.

11/05/2019 Loops / Iteration for

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

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.

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 1 - 5 have been covered in previous presentations. We will cover type 6 in this presentation.

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

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

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

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;

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.

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

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.

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.

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); }

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! */

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); } ?

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); } ?

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?

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).

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.

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 6 7 8 9

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.

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.

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.

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.

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 1+2+3+4 = 10 If 6 then 1+2+3+4+5+6 = 21 etc..... What happens if a zero or negative number is given? Stop a zero or a negative number being given.

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: 0 1 1 2 3 5 8

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 4+5+6+7 = 22 If 10 and 12 then 10+11+12 = 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?

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 4+5+6+1 = 16 If then 5+6+1 = 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.

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