COMP 110: Introduction to Programming Tyler Johnson Feb 11, 2009 MWF 11:00AM-12:15PM Sitterson 014.

Slides:



Advertisements
Similar presentations
COMP 110: Introduction to Programming Tyler Johnson Feb 18, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Advertisements

COMP 110: Introduction to Programming Tyler Johnson Feb 25, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Mar 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Mar 23, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 20, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 13, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Feb 23, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson January 12, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Mar 25, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 1, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson January 28, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 27, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Feb 4, 2009 MWF 11:00AM-12:15PM Sitterson 014.
February 12, 2013 COMP Introduction to Programming For Statement and More Loops Haohan Li TR 11:00 – 12:15, SN 011 Spring 2013.
CS0007: Introduction to Computer Programming
Branching Constructs Review l what are branching constructs? what type of branching constructs have we studied? l what is nested if? l what is multiway.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Lecture 6: for Loops Yoni Fridman 7/6/01 7/6/01. OutlineOutline  The for statement  The for statement’s format  The for statement’s flow  Comparing.
Loops – While, Do, For Repetition Statements Introduction to Arrays
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
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.
Flow of Control Loops – Chapter 3.2. Java Loop Statements: Outline the while Statement the do-while Statement the for Statement.
COMP 110 Switch Statements and Loops Tabitha Peck M.S. February 6, 2008 MWF 3-3:50 pm Philips
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
COMP 110: Introduction to Programming Tyler Johnson Feb 2, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
CPS120 Introduction to Computer Science Iteration (Looping)
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
© The McGraw-Hill Companies, 2006 Chapter 3 Iteration.
COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Flow of Control: Loops Module 4. Objectives Design a loop Use while, do, and for in a program Use the for-each with enumerations Use assertion checks.
COMP Loop Statements Yi Hong May 21, 2015.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Lecture 4b Repeating With Loops
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Loop Structures.
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.
CiS 260: App Dev I Chapter 4: Control Structures II.
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Conditinoal Constructs Review
Conditinoal Constructs Review
PROGRAM FLOWCHART Iteration Statements.
Announcements Lab 3 was due today Assignment 2 due next Wednesday
Presentation transcript:

COMP 110: Introduction to Programming Tyler Johnson Feb 11, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110: Spring Announcements Lab 3 due tomorrow by midnight Program 1 has been graded

COMP 110: Spring Questions?

COMP 110: Spring Today in COMP 110 Programming with Loops Programming Demo Using the Debugger

COMP 110: Spring Review The while statement can be used to construct a loop in Java while(Boolean_Expression) { Statement_1 Statement_2 … Statement_N } As long as Boolean_Expression is true, the statements in the loop body are executed

COMP 110: Spring Review Do-While Syntax do { Statement_1 Statement_2 … Statement_N } while (Boolean_Expression); //note the semicolon! Execute Statements_1…N If Boolean_Expression is true, repeat

COMP 110: Spring Review For Loop Syntax for(Initializing_Action; Boolean_Expression; Update_Action) { Statement_1 Statement_2 … Statement_N } Initializing_Action is used to initialize a counter variable Boolean_Expression is the stopping condition for the loop Update_Action is used to update a counter variable after the loop body is executed

COMP 110: Spring Review Choosing a loop Do-While When you want the body to be executed at least once Useful for checking user input For More convenient/readable when the number of iterations is known beforehand, e.g. stored in some counter variable While Safest choice, can be used to create any kind of loop When it might be necessary for the loop to iterate zero times

COMP 110: Spring The Break Statement Causes the immediately enclosing switch statement or loop to end The remainder of the loop (or switch) is not executed int i = 0; while (i < 5) { if(i == 3) break; i++; } //value of i is 3

COMP 110: Spring The Continue Statement Causes the current loop iteration to end immediately Execution resumes at the loop condition for (int i = 0; i < 5, i++) { if(i == 3) continue; System.out.println(i + ", "); } 0, 1, 2, 4, Output

COMP 110: Spring Break & Continue Avoid using break and continue inside loops Can make your programs difficult to understand!

COMP 110: Spring Nested Loops Its possible, and sometimes necessary to place loops inside other loops This is called nesting Just like with nested if-statements

COMP 110: Spring Nested Loops Example int sum = 0; int i = 0; while(i < 10) { for(int j = 0; j < 20; j++) sum = sum + j; //executes 10 * 20 times i++; //executes 10 times, i = [0, 9] }

COMP 110: Spring Commas in For Statements We can perform multiple initializations or updates using the comma operator int n, c; int product; for (n = 1, c = 1; n <= 5; c = c + 2, n++) { product = product * c * n; } System.out.println(product);

COMP 110: Spring Designing Loops Creating a loop involves designing three things Initialization of Variables Loop Body Stopping Condition

COMP 110: Spring Designing a Loop Body How to determine what statements should be inside the body of a loop? Example Calculate the sum of numbers entered by the user

COMP 110: Spring Designing a Loop Body Output instructions to the user Initialize variables Prompt user for input Read a number into variable next sum = sum + next; Prompt user for input Read a number into variable next sum = sum + next; Prompt user for input Read a number into variable next sum = sum + next;... Output sum Repeated statements become your loop body Statements that are only done once are not part of your loop body

COMP 110: Spring Pseudocode with Loop Body Output instructions to the user Initialize variables Do the following for the appropriate number of times: { Prompt user for input Read a number into variable next sum = sum + next; } Output sum Initializing statements How do we end the loop? How many iterations?

COMP 110: Spring Initializing Statements Variables used in your loop need to be initialized (set to a value) before the loop next Read a number into variable next We read a new value for next before using it during each iteration of the loop so we do not need to initialize it sum sum = sum + next; sum is on the right side of an assignment statement. sum MUST have a valid value before the loop starts.

COMP 110: Spring Initialize Sum What should sum be initialized to? Consider the first iteration After executing the first iteration, the expected value of sum should be sum == next (the first input value) The assignment statement is sum = sum + next; Therefore, initial value of sum is sum = 0;

COMP 110: Spring Loop with Initializations Output instructions to the user sum = 0; Do the following for the appropriate number of times: { Prompt user for input Read a number into variable next sum = sum + next; } Output sum Initializing statements

COMP 110: Spring Ending Loops How to choose a stopping condition for your loop? Count-controlled loops When you know number of loop iterations for(count = 0; count < iterations; count++) User-controlled loops Ask-before-iterating Sentinel value Boolean The value of a boolean indicates whether the loop should stop

COMP 110: Spring Count-Controlled Loops A program to average exam scores input by the user We know the size of the class is "numStudents" double next, sum = 0, average; int i; for(i = 0; i < numStudents; i++) { //# iterations is fixed next = keyboard.nextDouble(); sum = sum+next; } average = sum/numStudents;

COMP 110: Spring Off-by-One errors Loop repeats one too many or one too few times How many times do these loops iterate? for (count = 1; count < 10; count++); Loop for count = [1,9] (9 iterations) for (count = 0; count <= 10; count++); Loop for count = [0,10] (11 iterations) for (count = 1; count <= 10; count++); Loop for count = [1,10] (10 iterations)

COMP 110: Spring Ask-Before-Iterating Sometimes it is useful to have the user decide when a loop should end String answer; do { //perform some computations System.out.print("Continue? yes/no"); answer = keyboard.next(); } while (answer.equalsIgnoreCase("yes"));

COMP 110: Spring Sentinel Value Signal end of input System.out.print("Enter a negative number to end the loop"); int next = keyboard.nextInt(); int sum = 0; while (next >= 0) { sum = sum + next; System.out.print("Enter a number: "); next = keyboard.nextInt(); }

COMP 110: Spring Booleans int next, sum = 0; boolean moreNumbers = true; Scanner keyboard = new Scanner(System.in); while(moreNumbers ) { next = keyboard.nextInt(); if (next < 0) moreNumbers = false; //this will be the last iteration else sum = sum + next; } System.out.print("The sum is " + sum);

COMP 110: Spring Exercise What does the following display? int product = 1; int max = 20; for(int i = 0; i <= max; i++) product = product * i; System.out.println("The product is " + product); The product is 0 Output

COMP 110: Spring Example Problem Find the lowest and highest ages in the class

COMP 110: Spring Loop Body Get age of student 1 Update min/max ages Get age of student 2 Update min/max ages... Get age of student 37 Update min/max ages End loop Output min and max ages

COMP 110: Spring Min/Max Ages int min = 2000; // initialize to large value int max = 0; // initialize to small value for (int count = 1; count <= 37; count++) { //Ask student #count for age if (age > max) max = age; if (age < min) min = age; } //Output min and max ages

COMP 110: Spring Min/Max Ages if (age > max) max = age; if (age < min) min = age; Agesmaxmin …

COMP 110: Spring Debugging What to do if theres something wrong with your program, but youre not sure what? Trace the variables Watch the variables change as the program executes

COMP 110: Spring Tracing Variables Two ways to trace variables Manually Using print statements Automatically Using the debugger

COMP 110: Spring Manual Tracing Insert print statements to output the values of variables System.out.print("Enter a negative number to end the loop"); int next = keyboard.nextInt(); int sum = 0; while (next >= 0) { System.out.println(next); sum = sum + next; System.out.println(sum); System.out.print("Enter a number: "); next = keyboard.nextInt(); }

COMP 110: Spring Using a DEBUG flag Use a DEBUG flag to disable the tracing of variables when no longer needed final boolean DEBUG = true; //trace variables? … if(DEBUG) { //print the values of variables }

COMP 110: Spring Automatic Tracing Use a debugger jGRASP provides an integrated debugger Allows you to step through your program to see how variables change with each line of code

COMP 110: Spring Programming Demo Hand-shaking problem Given a group of n people, everyone shakes hands with everyone else How many handshakes?

COMP 110: Spring Programming Demo Example, n = 4 Person 1 shakes hands with Person 2 Person 1 shakes hands with Person 3 Person 1 shakes hands with Person 4 Person 2 shakes hands with Person 3 Person 2 shakes hands with Person 4 Person 3 shakes hands with Person 4 6 handshakes

COMP 110: Spring Programming Demo Example, n = 4 Person 1 shakes hands with Person 2 shakes hands with Person 3 shakes hands with Person 4 Person 2 shakes hands with Person 3 shakes hands with Person 4 Person 3 shakes hands with Person 4 6 handshakes

COMP 110: Spring Handshake Problem XXXXX 2XXXX 3XXX 4XX 5X 6 n = 6 Outer Loop Inner Loop

COMP 110: Spring Programming Demo Implementation using nested loops

COMP 110: Spring Tutorial Using the jGRASP Debugger

COMP 110: Spring Friday Recitation Bring Laptops (fully charged) Textbook Questions about Program 2