COMP 110 Augustus Gloop, Augustus Gloop… Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014.

Slides:



Advertisements
Similar presentations
Lecture 10 Flow of Control: Loops (Part 2) COMP1681 / SE15 Introduction to Programming.
Advertisements

Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
CS 106 Introduction to Computer Science I 09 / 25 / 2006 Instructor: Michael Eckmann.
Mock test review Revision of Activity Diagrams for Loops,
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 6, 2005.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Nested For Loops It is also possible to place a for loop inside another for loop. int rows, columns; for (rows=1; rows
COMP 110 Loops Tabitha Peck M.S. February 11, 2008 MWF 3-3:50 pm Philips
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
COMP 110 Designing Programs Tabitha Peck M.S. January 16, 2008 MWF 3-3:50 pm Philips
COMP 110 Branching Statements and Boolean Expressions Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
CS305j Introduction to ComputingNested For Loops 1 Topic 6 Nested for Loops "Complexity has and will maintain a strong fascination for many people. It.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Karel J Robot An introduction to BlueJ and Object- Oriented Programming.
Introduction to Computer Programming Counting Loops.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
1 Debugging. 2 A Lot of Time is Spent Debugging Programs Debugging. Cyclic process of editing, compiling, and fixing errors. n Always a logical explanation.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Program Errors and Debugging Week 10, Thursday Lab.
1 Operators and Expressions Instructor: Mainak Chaudhuri
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
Lec 20 More Arrays--Algorithms. Agenda Array algorithms (section 7.5 in the book) – An algorithm is a well-defined specification for solving a problem.
USING UNITY JAVASCRIPT. CONVENTIONS AND SYNTAX IN JAVASCRIPT Case Sensitivity All keywords like var or function must be in lowercase. All variable names,
COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
1 BUILDING JAVA PROGRAMS CHAPTER 2 Pseudocode and Scope.
Review, Pseudocode, Flow Charting, and Storyboarding.
COMP 110 Objects and references Luv Kohli October 8, 2008 MWF 2-2:50 pm Sitterson 014.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
COMP 110 Worksheet review, debugger Luv Kohli September 29, 2008 MWF 2-2:50 pm Sitterson 014.
COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.
Introduction to Methods Shirley Moore CS 1401 Spring 2013 cs1401spring2013.pbworks.com April 1, 2013.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
Nested for loops.
The for loop.
COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
COMP 110 Branching Statements and Boolean Expressions Luv Kohli September 8, 2008 MWF 2-2:50 pm Sitterson
COMP Loop Statements Yi Hong May 21, 2015.
1 Week 9 Loops. 2 Repetition: Loops l Structure: »Usually some initialization code »body of loop »loop termination condition l Several logical organizations.
Catie Welsh February 14,  Program 2 Due Tonight by 11:59pm  Program 3 Assigned 2.
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
COMP 110 Arrays Luv Kohli November 5, 2008 MWF 2-2:50 pm Sitterson 014.
COMP 110 More about classes Luv Kohli October 3, 2008 MWF 2-2:50 pm Sitterson 014.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
01/05/100 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Loop Structures.
The nested repetition control structures & Continue Statements
Repetition Chapter 6 12/06/16 & 12/07/16 1 1
Decision statements. - They can use logic to arrive at desired results
For Loops October 12, 2017.
Introduction to Computer Programming Counting Loops 2
COMP 110 Loops, loops, loops, loops, loops, loops…
Michele Weigle - COMP 14 - Spr 04
Suggested self-checks:
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 14, 2011
Building Java Programs
Announcements Lab 3 was due today Assignment 2 due next Wednesday
Chapter 3 Flow of Control Loops in Java.
Presentation transcript:

COMP 110 Augustus Gloop, Augustus Gloop… Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014

Announcements Program 2 due Friday, 2pm ◦ Remember to also print out your code and hand it in I will be out of town on Friday ◦ Recitation will still be held by another COMP110 instructor at the usual time ◦ No office hours Friday morning  Ask me questions before 6pm Thursday 2

Some notes about printing your code Print from jGRASP if you can Print with a fixed-width font (such as Consolas or Courier New ) Print double-sided if you can 3

Questions? 4

Today in COMP 110 Nested loops Some comments on Lab 3 5

Nested loops Just like we have nested if/else statements, we can have nested loops 6

Nested loops example: friendly greetings 7

Student 1 shakes Student 4’s hand Student 1 shakes Student 5’s hand Student 1 shakes Student 6’s hand Student 2 shakes Student 4’s hand Student 2 shakes Student 5’s hand Student 2 shakes Student 6’s hand Student 3 shakes Student 4’s hand Student 3 shakes Student 5’s hand Student 3 shakes Student 6’s hand 8

Nested loops example: friendly greetings Student 1 shakes Student 4’s hand shakes Student 5’s hand shakes Student 6’s hand Student 2 shakes Student 4’s hand shakes Student 5’s hand shakes Student 6’s hand Student 3 shakes Student 4’s hand shakes Student 5’s hand shakes Student 6’s hand 9

Nested loops example: friendly greetings for (every student in line A) { Student in line A shakes every Student’s hand in line B } 10

Nested loops example: friendly greetings for (every student in line A) { for (every student in line B) { (Student in line A) shakes (Student in line B)’s hand } 11 Inner loop Outer loop

Nested loops example: friendly greetings for (int stdLineA = 1; stdLineA <= 3; stdLineA++) { for (int stdLineB = 4; stdLineB <= 6; stdLineB++) { System.out.println(“Student ” + stdLineA + “ shakes Student ” + stdLineB + “’s hand.”); } 12 stdLineA stdLineB 4 5 6

Nested loops example: friendly greetings for (int stdLineA = 1; stdLineA <= 3; stdLineA++) { for (int stdLineB = 4; stdLineB <= 6; stdLineB++) { System.out.println(“Student ” + stdLineA + “ shakes Student ” + stdLineB + “’s hand.”); } 13 Inner loop Outer loop

Nested loops You can nest different kinds of loops inside other loops, or put if/else statements inside loops, or put loops inside if/else statements, or… ExamAverager example in jGRASP 14

Loop bugs Infinite loops – already talked about these Off-by-one errors 15

Off-by-one errors Loop repeats one too many or one too few times for (count = 1; count < 10; count++) // loop 9 times: 1, 2, 3,... 9 for (count = 1; count <= 10; count++) // loop 10 times: 1, 2, 3, for (count = 0; count < 10; count++) // loop 10 times: 0, 1, 2,

Finding errors Trace your variables ◦ Put output statements in your code to see what values are stored in your variables  System.out.println(variable);  Check whether the values are correct and what you expect ◦ Remove these extra output statements after the program runs correctly ◦ Read example in the book, p. 188 (4 th edition), p. 218 (5 th edition) Use a debugger 17

What does this code do? int a = 0; int b = 0; for (int i = 1; i <= 6; i++) { if (i % 2 == 0) { b = b + i;} else { a = a + i; } 18

What does this code do? int oddSum = 0; int evenSum = 0; for (int i = 1; i <= 6; i++) { if (i % 2 == 0) { evenSum = evenSum + i;} else { oddSum = oddSum + i; } 19

What does this code do? int oddSum = 0; int evenSum = 0; for (int i = 1; i <= 6; i++) { if (i % 2 == 0) { evenSum = evenSum + i; } else { oddSum = oddSum + i; } 20

What is wrong with this code? int oddSum = 0; int evenSum = 0; for (int i = 1; i <= 6; i++) { if (i % 2 == 0) evenSum = evenSum + i;} else { oddSum = oddSum + i; } 21

What is wrong with this code? int oddSum = 0; int evenSum = 0; for (int i = 1; i <= 6; i++) { if (i % 2 == 0) evenSum = evenSum + i; } else { oddSum = oddSum + i; } 22

Indentation Indentation ◦ Makes code easier to read ◦ Helps with finding syntax and logic errors ◦ Indent code that goes between { and } Be consistent! 23

Scope Variables declared in outer scopes are visible to code inside inner scopes public static void main(String[] args) { int total = 15; int n = 5; if (n <= 10) { total = total + n; } System.out.println(total); } outer inner 24

Scope Variables declared in inner scopes are NOT visible to outer code public static void main(String[] args) { int n = 5; if (n <= 10) { int total = 15 + n; } System.out.println(total); // ERROR!!! } outer inner 25

else if (inputString.equals(“BLUE”)) eyeColor = Color.BLUE; else if (inputString.equals(“GREEN”)) eyeColor = Color.GREEN; else if (inputString.equals(“RED”)) eyeColor = Color.RED; else if (!inputString.equals(“BLUE”) && !inputString.equals(“GREEN”) && !inputString.equals(“RED”)) eyeColor = Color.WHITE; 26

else if (inputString.equals(“BLUE”)) eyeColor = Color.BLUE; else if (inputString.equals(“GREEN”)) eyeColor = Color.GREEN; else if (inputString.equals(“RED”)) eyeColor = Color.RED; else eyeColor = Color.WHITE; 27

else not needed when empty if (inputString.equals(“MOUTH”)) { mouthStartAngle = 0; } else { } 28

Multi-line comments /* This is a multi-line comment. What do you think of it? */ // You can also have multi-line comments // this way. 29

Too many comments Don’t need to comment every line of code, especially when it is obvious what your code is doing // set count to 12 count = 12; Only comment to enhance understanding and to explain why your code does what it does 30

Friday Program 2 due, 2pm Lab 4 help 31