Coding #1 if(!(index < 0 || index >= list.size( )-1))

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Decision Structures if, if/else conditions. Selection DECISION: determine which of 2 paths to follow (1 or more statements in each path)
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
CSCI 160 Midterm Review Rasanjalee DM.
1 Features of the collection It increases its capacity as necessary. It keeps a private count: –size() accessor. It keeps the objects in order. Details.
Grouping objects Introduction to collections 5.0.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Programming with Collections Grouping & Looping - Collections and Iteration Week 7.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
M. Taimoor Khan #include void main() { //This is my first C++ Program /* This program will display a string message on.
Monday, Jan 13, 2003Kate Gregory with material from Deitel and Deitel Week 2 Questions from Last Week Control Structures Functions Lab 1.
REPETITION CITS1001. Scope of this lecture Repetition for loops while loops 2.
Palindromes revisited Here's a simpler program for checking palindromes: int nums[100]; int i = 0, a; cin >> a; while(a > 0) { nums[i++] = a; cin >> a;
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.
Grouping objects Introduction to collections 5.0.
COP-3330: Object Oriented Programming Flow Control May 16, 2012 Eng. Hector M Lugo-Cordero, MS.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
Grouping objects Collections and iterators Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
Chapter 5 Selection Statements Mr. Dave Clausen La Cañada High School.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
1 Chapter 3: Loops and Logic. 2 Control Statements If statement Example NumberCheck.java Relational operators (, >=, ==, !=) Using code blocks with If.
GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2.
Grouping objects Introduction to collections 5.0.
1 COS 260 DAY 7 Tony Gauvin. 2 Agenda Questions? 3 rd Mini quiz next class (September 28) –Chap 3 concepts Assignment 1 Corrected Assignment 2 posted.
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Structured Programming Structured Programming is writing a program in terms of only 3 basic control structures: sequence selection repetition We have already.
Grouping objects Introduction to collections 6.0.
1 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. for-each PROS easy to use access to ALL items one-by-one ability to change the state.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
Basic concepts of C++ Presented by Prof. Satyajit De
for-each PROS CONS easy to use access to ALL items one-by-one
Objects First with Java CITS1001 week 4
Lecture 4b Repeating With Loops
CSC111 Quick Revision.
Objects First with Java Introduction to collections
Agenda Warmup Finish 2.4 Assignments
Loop Structures.
COS 260 DAY 7 Tony Gauvin.
Chapter 5: Loops and Files.
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Agenda Control Flow Statements Purpose test statement
Manipulating Pictures, Arrays, and Loops part 2
Objects First with Java Introduction to collections
Arrays, For loop While loop Do while loop
Iteration with While You can say that again.
Conditionals & Boolean Expressions
Outline Altering flow of control Boolean expressions
COS 260 DAY 8 Tony Gauvin.
COS 260 DAY 8 Tony Gauvin.
Patterns to KNOW.
Objects First with Java Introduction to collections
Logical Operations In Matlab.
Fundamental Programming
Question 1a) What is printed by the following Java program? int s;
CprE 185: Intro to Problem Solving (using C)
Decision Structures if, if/else conditions
Chapter 5 Selection Statements
Presentation transcript:

Coding #1 if(!(index < 0 || index >= list.size( )-1)) Valid indices between 0…(size-1): if(!(index < 0 || index >= list.size( )-1))

Coding #1 if(!(index < 0 || index >= list.size( )-1)) Valid indices between 0…(size-1): if(!(index < 0 || index >= list.size( )-1)) if(index >= 0 && index < list.size( ))

Find 4 things to improve here: Coding #2 Find 4 things to improve here: boolean found; for(String filename : files) { if(filename.contains(searchString)) { found = true; break; } else { if(found == false) { System.out.println(“NO file found”);

Find 4 things to improve here: Coding #2 Find 4 things to improve here: boolean found = false; // initialization for(String filename : files) { if(filename.contains(searchString)) { found = true; break; // bad style } else { // empty block if(found == false) { // unary operator System.out.println(“NO file found”); !found

Coding #3 What is wrong here: boolean found = false; for(String filename : files) { if(filename.contains(searchString)) { found = true; } else { found = false; if(!found) { System.out.println(“NO file found”);

What is (1) wrong here logically: Coding #3 What is (1) wrong here logically: boolean found = false; for(String filename : files) { if(filename.contains(searchString)) { found = true; } else { // negates found=true, found = false; // if this item is AFTER } // an item already found if(!found) { System.out.println(“NO file found”);

Find 5 things to fix here: Coding #4 Find 5 things to fix here: int index; int limit; index = 0; limit = files.size() - 1; while(index < limit) { String filename = files.get(index); System.out.println(filename); }

Find 5 things to fix here: Coding #4 Find 5 things to fix here: // declare & initialize int index = 0; // at the same time int limit = files.size() - 1; if(limit < 0) { // check lower boundary System.out.println(“NO files exists”); } else { // check upper boundary while(index <= limit) { String filename = files.get(index); System.out.println(filename); index++; // increment counter

How can this be improved & compiled: Coding #5 How can this be improved & compiled: if(index < 0) { System.out.println(“Index too SMALL”); return -1; } else if(index >= size) { System.out.println(“Index is too LARGE”); else if(index >= 0 && index < size) { System.out.println(“Index is VALID”); return index;

How can this be improved & compiled: Coding #5 How can this be improved & compiled: int location = -1; // use local variable if(index < 0) { System.out.println(“Index too SMALL”); return -1; // too many returns } else if(index >= size) { System.out.println(“Index is too LARGE”); return -1; // too many returns else if(index >= 0 && index < size) { System.out.println(“Index is VALID”); location = index; // set valid index return location; // 1 return statement

How can this be improved: Coding #6 How can this be improved: if(isValid == true) { return true; } else { return false;

How can this be improved: Coding #6 How can this be improved: BETTER if(isValid == true) { return true; } else { return false; BEST return isValid;

How can this be improved or fixed: Coding #7 How can this be improved or fixed: public int sum(int a, int b) { int sum = 0; while(a <= b) { sum = sum + a; a = a + 1; }

How can this be improved or fixed: Coding #7 How can this be improved or fixed: public int sum(int a, int b) { int sum = 0; int num = a; // use local variable while(num <= b) { // not parameter sum += num; // compound operator num++; // unary operator } return sum; // missing return