February 12, 2013 COMP 110-003 Introduction to Programming For Statement and More Loops Haohan Li TR 11:00 – 12:15, SN 011 Spring 2013.

Slides:



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

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 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 28, 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.
Slide 1 Insert your own content. Slide 2 Insert your own content.
How many hours have passed from 6:00 P.M. to 9:00 P.M.? C. 3 hours B. 6 hoursA. 9 hours.
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
0 - 0.
22 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming
Introduction to Programming Java Lab 5: Boolean Operations 8 February JavaLab5 lecture slides.ppt Ping Brennan
Introduction to Programming
Lecture 10 Flow of Control: Loops (Part 2) COMP1681 / SE15 Introduction to Programming.
Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.
Lesson 6A – Loops By John B. Owen All rights reserved ©2011.
For(int i = 1; i
Energy & Green Urbanism Markku Lappalainen Aalto University.
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
FINAL EXAMINATION SCHEDULE SPRING 2009 MAY 15 – MAY 22 NOTE: A class that meets at more than one of the times on this final examination schedule will take.
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
June 10, 2015ICS102: while & do-while1 while and do-while Statements.
COMP 14 Introduction to Programming Miguel A. Otaduy May 21, 2004.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
COMP 110 Loops Tabitha Peck M.S. February 11, 2008 MWF 3-3:50 pm Philips
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Week 11 Recap CSE 115 Spring Want to write a programming language? You’ll need three things: You’ll need three things: –Sequencing –Selection Polymorphism.
1 Fall 2008ACS-1903 Ch 4 Loops and Files while loop do-while loop Other topics later on …
COMP 110 Switch Statements and Loops Tabitha Peck M.S. February 6, 2008 MWF 3-3:50 pm Philips
COMP 1001: Introduction to Computers for Arts and Social Sciences Programming in Scratch Monday, May 16, 2011.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
Chapter 6 Looping CS185/09 - Introduction to Programming Caldwell College.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Nested for loops.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Chapter 9 Control Structures.
COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
1 Fall 2009ACS-1903 Ch 4 Loops and Files while loop do-while loop for loop Other topics later on …
CPSC 233 Tutorial 5 February 2 th /3 th, Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Follow up from lab See Magic8Ball.java Issues that you ran into.
Chapter 9 Repetition.
Loops in Java.
الحلقات التكرارية وجمل الدوران (loops)
Chapter 5 Repetition.
For Loops October 12, 2017.
Looping and Repetition
Loops October 10, 2017.
Iteration with While You can say that again.
Chapter 9 Control Structures.
Lecture Notes – Week 3 Lecture-2
LOOPS BY: LAUREN & ROMEO.
Chapter 8: More on the Repetition Structure
Loops.
Flow of Control.
Seating “chart” Front - Screen rows Back DOOR.
Repetition Statements (Loops) - 2
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 14, 2011
General Condition Loop
PROGRAM FLOWCHART Iteration Statements.
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
Presentation transcript:

February 12, 2013 COMP Introduction to Programming For Statement and More Loops Haohan Li TR 11:00 – 12:15, SN 011 Spring 2013

Review: While Statement While statement – while (boolean condition expression) { body statements; } Do-while statement – do { body statements; } while (boolean condition expression);

Review: While Statement If we want to repeat some actions for a given number of times – We need to count using a while loop – The code will look like this: number = keyboard.nextInt(); int count = 1; while (count <= number) { // all the actions count++; }

Review: While Statement If we want to repeat some actions for a given number of times – We need to count using a while loop – The code will look like this: – There are three lines referring count number = keyboard.nextInt(); int count = 1; while (count <= number) { // all the actions count++; }

For Statement Is there a better way to organize the code? For statement (or usually called for loop) – Used to executes the body of a loop a fixed number of times number = keyboard.nextInt(); count = 1; while (count <= number) { // all the actions count++; } number = keyboard.nextInt(); int count; for (count = 1; count<=number; count++) { // all the actions }

For Statement Syntax: – for (Initializing_Action; Boolean_Expression; Update_Action){ Body; } for (count = 1; count <= number; count++) { // all the actions }

For Statement Flow chart – for (Initializing_Action; Boolean_Expression; Update_Action){ Body; }

For Statement Unrolled code for (count = 1; count<= 2; count++) { // all the actions } count = 1; // initialize for only once if (count <= 2) {// count == 1, so yes // all the actions count++; } if (count <= 2) {// count == 2, yes again // all the actions again count++; } if (count <= 2) {// count == 3, so no // no action again; // no count++ again; } // stop

Local Variable The counter can be defined in the for loop – That counter variable will be only available in this loop – It is undefined out of the loop – Although you can, it is really bad idea to name the count variable the same as other variable out of the loop for (int i = 1; i <= 100; i++) { // all the actions } System.out.println(i); //WRONG! i is not available out of the loop

Local Variable Local variables can also be defined in if and while statements – These variables are invalid out of the statement if (true) { int temp = 0; } do { int temp2 = 2; } while (false);

For Loop: Don’t Overcount Repeat 3 times Repeat 4 times! for (int count = 1; count <= 3; count++) { // all the actions } for (int count = 0; count < 3; count++) { // all the actions } for (int count = 0; count <= 3; count++) { // all the actions }

For Loop: Infinite Loop Still, if you get things wrong, it may never end int num = 3; // initializing action; boolean expression; update action for (count = 5; count >= num; count++) { System.out.print(count + “, ”); }

For Loop: Case Study Let the user input 10 numbers, then output the sum of those numbers Write your program – It’s suggested that you don’t use eclipse – Don’t run the program until I ask you to

For Loop: Case Study Let the user input 10 numbers, then output the sum of those numbers Sum = 0 Get user input sum = sum + input Get user input sum = sum + input Get user input sum = sum + input Output sum WHY?

For Loop: Case Study Let the user input 10 numbers, then output the sum of those numbers – 1111, 2222, 3333, 4444, 5555, 6666, 7777, 8888, 9999, 12345

For Loop: Case Study Let the user input 10 numbers, then output the sum of those numbers – 1111, 2222, 3333, 4444, 5555, 6666, 7777, 8888, 9999, – The sum should be 62340

For Loop: Case Study Let the user input 10 numbers, then output the sum of those numbers import java.util.Scanner; public class input { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int sum = 0; for (int i = 1; i <= 10; i++) { System.out.println("Please enter a new number (" + i + " of 10):"); int input = keyboard.nextInt(); sum += input; } System.out.println("Total sum is: " + sum); }

For Loop: Case Study Let the user input 10 numbers, then output the sum of those numbers import java.util.Scanner; public class input { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int sum = 0; for (int i = 1; i <= 10; i++) { System.out.println("Please enter a new number (" + i + " of 10):"); int input = keyboard.nextInt(); sum += input; } System.out.println("Total sum is: " + sum); }

For Loop: Case Study What’s wrong with this piece of code? import java.util.Scanner; public class input { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); for (int i = 1; i <= 10; i++) { int sum = 0; System.out.println("Please enter a new number (" + i + " of 10):"); int input = keyboard.nextInt(); sum += input; } System.out.println("Total sum is: " + sum); }

For Loop: Case Study What’s wrong with this piece of code? import java.util.Scanner; public class input { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); for (int i = 1; i <= 10; i++) { int sum = 0; System.out.println("Please enter a new number (" + i + " of 10):"); int input = keyboard.nextInt(); sum += input; } System.out.println("Total sum is: " + sum); } Syntax error & logical error

For Loop: Case Study Let the user input 10 numbers, then output the product of those numbers import java.util.Scanner; public class input { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int product = 0; for (int i = 1; i <= 10; i++) { System.out.println("Please enter a new number (" + i + " of 10):"); int input = keyboard.nextInt(); product *= input; } System.out.println("Total product is: " + product); }

For Loop: Case Study Let the user input 10 numbers, then output the product of those numbers import java.util.Scanner; public class input { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int product = 1; for (int i = 1; i <= 10; i++) { System.out.println("Please enter a new number (" + i + " of 10):"); int input = keyboard.nextInt(); product *= input; } System.out.println("Total product is: " + product); }

Ending a Loop If you know number of loop iterations? – Count-controlled loops – for(count = 0; count < iterations; count++) User controlled ending – Ask-before-iterating (e.g. “yes/no”) – Sentinel value

Count-Controlled Loop We’ve seen it for (count = 1; count <= number; count++) { // all the actions }

Ask-Before-Iterating Quit loop when the input is not yes – Why don’t we initialize answer? String answer; do { // do stuff in your code here System.out.print("Continue? yes/no"); answer = keyboard.next(); } while (answer.equalsIgnoreCase("yes"));

Ask-Before-Iterating Quit loop when the input is not yes – Using a while loop – answer has to be initialized String answer = "YES"; while (answer.equalsIgnoreCase("yes")) { // do stuff in your code here System.out.print("Continue? yes/no"); answer = keyboard.next(); }

Ask-Before-Iterating Quit loop when the input is negative – Think about initializations – is this the best way to do it? 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(); }

Nested Loop There can be a loop inside another loop – Mostly for loops int count, count2; for (count = 0; count <= 3; count++) for (count2 = 0; count2 < count; count2++) System.out.println(count2);

What is the Output? int count, count2; for (count = 0; count <= 3; count++) for(count2 = 0; count2 < count; count2++) System.out.println(count2); Count = Count2 = Output will be 0, 0, 1, 0, 1, 2 Intuition: exhaustively list all possibilities

Understanding a For Loop int count, count2; for (count = 0; count <= 3; count++) for(count2 = 0; count2 < count; count2++) System.out.println(count2); Think it in this way: – count goes from 0 to 3 – count2 goes from 0 to count-1 (for every count’s value) – What values of count2 will be visited, or printed?

Another For Loop Example Still, pay attention to brackets for (int i = 0; i < 10; i++) for (int j = 0; j < 10; j++) System.out.print(i * 10 + j + ", "); System.out.println("100."); for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++){ System.out.print(i * 10 + j + ", "); } System.out.println("100.");

Another For Loop Example What does this piece of code do? Run it and see if your guess is good for (int i = 1; i <= 10; i++) for (int j = 1; j <= 10; j++) System.out.print(i * 10 + j + ", ");