Computer Science 12 Mr. Jean May 2 nd, 2013. The plan: Video clip of the day Review of common errors in programs 2D Arrays.

Slides:



Advertisements
Similar presentations
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
Objectives In this chapter, you will learn about:
 2002 Prentice Hall. All rights reserved Control Structures 3 control structures –Sequential structure Built into Python –Selection structure The.
Computer Science 1620 Loops.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Loops – While, Do, For Repetition Statements Introduction to Arrays
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Moving To Code 3 More on the Problem-Solving Process §The final step in the problem-solving process is to evaluate and modify (if necessary) the program.
 Pearson Education, Inc. All rights reserved Arrays.
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.
Fundamentals of Python: From First Programs Through Data Structures
COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Fundamentals of Python: First Programs
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Fundamentals of Java Text by: Lambert and Osborne Slides by: Cestroni.
4.1 Additional Operators Extended Assignment Operators –The assignment operator can be combined with the arithmetic and concatenation operators to provide.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
6.2 For…Next Loops General Form of a For…Next Loop
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
Lesson 4: Introduction to Control Statements
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Chapter 4 Introduction to Control Statements
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2005 Pearson Education, Inc. All rights reserved Arrays.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
1 Looping Chapter 6 2 Getting Looped in C++ Using flags to control a while statement Trapping for valid input Ending a loop with End Of File condition.
Chapter 4 – C Program Control
Lecture 7: Repeating a Known Number of Times
Chapter 5: Control Structures II
Ch 7: JavaScript Control Statements I.
JavaScript: Control Statements.
Arrays, For loop While loop Do while loop
Outline Altering flow of control Boolean expressions
Chapter (3) - Looping Questions.
Introduction To Programming Information Technology , 1’st Semester
3 Control Statements:.
Repetition Statements (Loops) - 2
Presentation transcript:

Computer Science 12 Mr. Jean May 2 nd, 2013

The plan: Video clip of the day Review of common errors in programs 2D Arrays

Review Notes: –Keywords Keywords or reserved words cannot be employed as user- defined symbols because they have special meaning in Java. Keywords are also case sensitive. “import” is a reserved word but “Import” and “IMPORT” are not.

Basic Java Syntax Table 3-7 displays a list of Java’s reserved words

Logic Operations: Relational Operators Table 4-3 shows the complete list of relational operators available for use in Java.

When using if and if-else Statements –The double equal signs (==) distinguish the equal to operator from the assignment operator (=). –In the not equal to operator, the exclamation mark (!) is read as not.

The while Statement –The while statement provides a looping mechanism that executes statements repeatedly for as long as some condition remains true. while (condition)//loop test statement;//one statement inside the loop body while (condition) {//loop test statement;//many statements statement;//inside the...//loop body }

The while Statement –If the condition is false from the outset, the statement or statements inside the loop never execute. Figure 4-2 uses a flowchart to illustrate the behavior of a while statement.

The while Statement Compute 1+2+…+100 (Count-controlled Loops) –The following code computes and displays the sum of the integers between 1 and 100, inclusive: // Compute … int sum = 0, cntr = 1; while (cntr,= 100) { sum += cntr; //point p (we refer to this location in Table 4-4) cntr++; // point q (we refer to this location in table 4-4) } System.out.println (sum);

The while Statement –The variable cntr acts as a counter that controls how many times the loop executes. –The counter starts at 1. –Each time around the loop, it is compared to 100 and incremented by 1. –The code inside the loop is executed exactly 100 times, and each time through the loop, sum is incremented by increasing values of cntr. –The variable cntr is called the counter.

Errors in Loops A loop usually has four component parts: –Initializing Statements. These statements initialize variables used within the loop. –Terminating condition. This condition is tested before each pass through the loop to determine if another iteration is needed. –Body Statements. these statements execute on each iteration and implement the calculation in question.

Errors in Loops –Update statements. These statements, which usually are executed at the bottom of the loop, change the values of the variables tested in the terminating condition. –A careless programmer can introduce logic errors into any of these components.

Errors in Loops Initialization Error –Because we forget to initialize the variable product, it retains its default value of zero. //Error – failure to initialize the variable product //Outcome – zero is printed i = 3; while (i <= 100) { product = product * i; i = i + 2; } System.out.println (product);

Errors in Loops Off-by-One Error –The off-by-one error, occurs whenever a loop goes around one too many or one too few times –This is one of the most common types of looping errors and is often difficult to detect.

Errors in Loops //Error – use of “< 99” rather than “<= 100” in the //terminating condition //Outcome – product will equal 3 * 5...* 97 product = 1; i = 3; while (i < 99) { product = product * i; i = i +2; } System.out.println (product);

Errors in Loops Debugging Loops –If you suspect that you have written a loop that contains a logic error, inspect the code and make sure the following items are true: Variables are initialized correctly before entering the loop. the terminating condition stops the iterations when the test variables have reached the intended limit.

Errors in Loops The statements in the body are correct. The update statements are positioned correctly and modify the test variables in such a manner that they eventually pass the limits tested in the terminating condition. When writing terminating conditions, it is usually safer to use one of the operators: >= than either of the operators == !=

Errors in Loops –If you cannot find an error by inspection, then use System.out.println statements to “dump” key variables to the terminal window. –Good places for these statements are: Immediately after the initialization statements. Inside the loop at the top. Inside the loop at the bottom. –You will then discover that some of the variables have values different than expected, and this will provide clues that reveal the exact nature and location of the logic error.

Back to Arrays:

Sample Arrays:

2D – Arrays: type array_name = new type[rows][cols]; Example Code: int multidim[] = new int[3][]; In a two-dimensional array, –You need to allocate memory for only the first dimension. –You can allocate the remaining dimensions separately. When you allocate memory to the second dimension, you can also allocate different number to each dimension.

Accessing Arrays You need to access various elements of an array to assign, retrieve, and manipulate the values stored in the array. Assigning values to the Elements of an Array –To access a specific array: You need to specify the name of the array and the index number of the element. The index position of the first element in the array is 0.

Accessing Array:

Accessing Various Elements: