Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.

Slides:



Advertisements
Similar presentations
Chapter 7 - Iteration. Chapter Goals Program repitiation statements – or loops – with the for, while, and do-while statements Program repitiation statements.
Advertisements

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.
Week 5 - Friday.  What did we talk about last time?  Repetition  while loops.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
June 10, 2015ICS102: while & do-while1 while and do-while Statements.
1 Control Structures (and user input). 2 Flow of Control The order statements are executed is called flow of control By default, statements in a method.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Week 3 - Friday.  What did we talk about last time?  Bitwise practice  Precedence  Control flow.
1 CS 177 Week 5 Recitation Slides Loops. 2 Announcements Project 2 due next Thursday at 9PM. Exam 1 this evening (switch and loop not covered) Old exams.
Chapter 5: Control Structures II (Repetition)
Java Programming: From the Ground Up
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
REPETITION CITS1001. Scope of this lecture Repetition for loops while loops 2.
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
© 2004 Pearson Addison-Wesley. All rights reserved February 17, 2006 The ‘while’ Statement ComS 207: Programming I (in Java) Iowa State University, SPRING.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Chapter 5 Loops.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
 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.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
CS101 Computer Programming I Chapter 4 Extra Examples.
Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.
Chapter 4: Control Structures II
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
CS 106 Introduction to Computer Science I 09 / 26 / 2007 Instructor: Michael Eckmann.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Week 6 - Monday.  What did we talk about last time?  while loop examples  Lab 5.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
COMP Loop Statements Yi Hong May 21, 2015.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
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.
Week 3 - Friday.  What did we talk about last time?  Preprocessor directives  Other C features  sizeof, const  ASCII table  printf() format strings.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
Chapter 5: Loops Tarik Booker CS 201 California State University, Los Angeles.
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Lecture 6 Repetition Richard Gesick.
Loop Structures.
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Lecture 4A Repetition Richard Gesick.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Looping and Repetition
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Outline Altering flow of control Boolean expressions
Introduction to Object-Oriented Programming with Java--Wu
Loop Strategies Repetition Playbook.
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
ICS103: Programming in C 5: Repetition and Loop Statements
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Looping and Repetition
Week 5 - Friday CS 121.
Presentation transcript:

Week 5 - Wednesday

 What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements

 All the little pieces of Java syntax are confusing  I have tried hard to tell you exactly what you need to know (and no more)  Java seems like it has an unlimited amount of stuff to learn, you can already do a lot  They designed Java so that you have just enough tools, but they don't like to have redundant functionality  The key is to think about how to put together the pieces  It's like a puzzle

 Focus on the values you have and how to turn them into the values you want  Let's look at this example of incorrect code and see how to fix it: char letter = in.nextChar(); if( letter >= 'a' && <= 'z' ) System.out.println("Lowercase letter!"); char letter = in.nextChar(); if( letter >= 'a' && <= 'z' ) System.out.println("Lowercase letter!");

 Scanner can't read a char value in directly  We have to read a String String word = in.next(); //we can do this! char letter = word.charAt(0); //and get a char from the String if( letter >= 'a' && <= 'z' ) System.out.println("Lowercase letter!"); String word = in.next(); //we can do this! char letter = word.charAt(0); //and get a char from the String if( letter >= 'a' && <= 'z' ) System.out.println("Lowercase letter!"); char letter = in.nextChar(); if( letter >= 'a' && <= 'z' ) System.out.println("Lowercase letter!"); char letter = in.nextChar(); if( letter >= 'a' && <= 'z' ) System.out.println("Lowercase letter!");

 The && operator only works between two boolean values  <= 'z' isn't a boolean, it's only part of a comparison String word = in.next(); //we can do this! char letter = word.charAt(0); //and get a char from the String if( letter >= 'a' && letter <= 'z' ) //fixed! System.out.println("Lowercase letter!"); String word = in.next(); //we can do this! char letter = word.charAt(0); //and get a char from the String if( letter >= 'a' && letter <= 'z' ) //fixed! System.out.println("Lowercase letter!"); String word = in.next(); //we can do this! char letter = word.charAt(0); //and get a char from the String if( letter >= 'a' && <= 'z' ) System.out.println("Lowercase letter!"); String word = in.next(); //we can do this! char letter = word.charAt(0); //and get a char from the String if( letter >= 'a' && <= 'z' ) System.out.println("Lowercase letter!");

 You're only allowed to do certain operations:  Try to think of everything in terms of these operations  Methods are useful too (for manipulating String values or doing advanced math), but there are too many to list here First Operand Operator Second Operand Result int+, -, *, /, %int double+, -, *, /, %double number==, !=,, >=numberboolean &&, ||boolean String+ any value String

 Java works on a set of arbitrary rules  Some regular people invented them  They are internally consistent, but they are not based on reality  They don't make any deep sense  Don't waste time trying to find meaning in the rules!  Just use the rules as a set of tools you can use to solve your problem  Liker MacGuyver

 "Those who cannot remember the past are condemned to repeat it."  George Santayana  What is our past?  Basic data types  Mathematical and String operations  Input and output  Conditional execution

 To these tools, we add repetition, the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that can be solved with a computer  Repetition leverages the most famous ability of the computer: speed

 The main way that repetition works in Java is through loops  A loop is a block of code that will be executed repeatedly some number of times (perhaps even zero)  As the statements are executed, the variables change  It isn’t just repeating the same thing over and over (unless a mistake was made)

 The simplest loop in Java is the while loop  It looks similar to an if statement  The difference is that, when you get to the end of the while loop, it jumps back to the top  If the condition in the while loop is still true, it executes the body of the loop again

while( condition ) { statement1; statement2; … statementn; } A whole bunch of statements

 A while loop will usually have multiple statements in its body  However, it is possible to make a while loop with only a single statement  Then, like an if -statement, the braces are optional while( condition ) statement;

 Let’s print the numbers from 1 to 100  We could do this with lots of cut/paste, or: int i = 1; while( i <= 100 ) { System.out.println(i); i++; } int i = 1; while( i <= 100 ) { System.out.println(i); i++; }

 The while loop executes each statement one by one  When execution gets to the bottom, it jumps to the top  If the condition is still true (i.e., i <= 100 ), it repeats the loop

//line A while( condition ) { //line B statement1; statement2; //line C … statementn; } //line D LineCondition An B C D LineCondition AUnknown B C D LineCondition AUnknown BTrue C D LineCondition AUnknown BTrue CUnknown D LineCondition AUnknown BTrue CUnknown DFalse

 We can also use while loops to help us deal with input  What if we wanted to sum all of the numbers that a person entered?  How would we know when to stop?  One solution is to keep adding numbers until the person enters a negative number  This is called using a sentinel value

 Solution: int i = 0; int sum = 0; Scanner in = new Scanner( System.in ); while( i >= 0 ) { sum += i; System.out.print("Enter number: "); i = in.nextInt(); } System.out.println("Sum: " + sum); int i = 0; int sum = 0; Scanner in = new Scanner( System.in ); while( i >= 0 ) { sum += i; System.out.print("Enter number: "); i = in.nextInt(); } System.out.println("Sum: " + sum);

 We could also find the average: int i = 0; double sum = 0; int count = 0; Scanner in = new Scanner( System.in ); while( i >= 0 ) { sum += i; count++; System.out.print("Enter number: "); i = in.nextInt(); } count--; //fixes extra count for sentinel System.out.println("Average: " + (sum / count)); int i = 0; double sum = 0; int count = 0; Scanner in = new Scanner( System.in ); while( i >= 0 ) { sum += i; count++; System.out.print("Enter number: "); i = in.nextInt(); } count--; //fixes extra count for sentinel System.out.println("Average: " + (sum / count));

 What if we wanted to find the biggest input?  Somehow we would have to check each input and see if it were larger than the current largest  Solution: use an if -statement inside of a while loop  Let’s look at Eclipse

 Let’s say that you wanted to write a program to guess a number that a person had come up  The number is between 1 and 100  Every time the computer guesses a number, the person enters:  H if the number is too high  L if the number is too low  F if the number was found

1. Start with the minimum and maximum of the range 2. Find the midpoint 3. Ask the user if the midpoint is correct 4. If the answer is too high, go to Step 1 using the minimum and the midpoint as the new range 5. If the answer is too low, go to Step 1 using the midpoint and the maximum as the new range 6. If the midpoint is correct, you’re done!

 Just as with if -statements, it’s possible to nest loops  A repetitive task can be done inside of another repetitive task  Be careful! You can make the computer do a lot of work

 Triangular numbers are 1, 3, 6, 10, …  1 = 1  3 =  6 =   Let’s write a program that expresses the n th triangular number by printing 1 on the first line, 1 and 2 on the second line, 1, 2, and 3 on the third line, and so on

 Loops can go on forever if you aren’t careful int n = 40; int i = 1; while( i <= 40 ) { System.out.println(i); //supposed to print all the numbers //less than 40, but i never increases } int n = 40; int i = 1; while( i <= 40 ) { System.out.println(i); //supposed to print all the numbers //less than 40, but i never increases }

 Overflow and underflow will make some badly written loops eventually terminate int n = 40; int i = 1; while( i <= 40 ) { System.out.println(i); i--; //whoops, should have been i++ } int n = 40; int i = 1; while( i <= 40 ) { System.out.println(i); i--; //whoops, should have been i++ }

 Being off by one is a very common loop error int n = 40; int i = 1; while( i < 40 ) //won’t //reach 40 { System.out.println(i); i++; } int n = 40; int i = 1; while( i < 40 ) //won’t //reach 40 { System.out.println(i); i++; }

 If the condition isn’t true to begin with, the loop will just be skipped int n = 40; int i = 1; while( i >= 40 ) //oops, should be <= { System.out.println(i); i++; } int n = 40; int i = 1; while( i >= 40 ) //oops, should be <= { System.out.println(i); i++; }

 A misplaced semicolon can cause an empty loop body to be executed (often infinitely) int n = 40; int i = 1; while( i <= 40 ); //semicolon is wrong { System.out.println(i); i++; } int n = 40; int i = 1; while( i <= 40 ); //semicolon is wrong { System.out.println(i); i++; }

 The condition of the while loop is not followed by a semicolon  Be careful about starting and ending conditions  When in doubt, use braces  The print statement must be inside the loop in order to get printed multiple times  There’s no magic formula; you have to think it through

1. while loops  Used when you don’t know how many times you are going to need to repeat 2. for loops  Used when you do know how many times you are going to repeat 3. do-while loops  Used never  Oh, okay, they are used whenever you need to be guaranteed the loop runs at least once

 Any problem that uses loops can use any kind of loop  The choice is supposed to make things easier on the programmer  Some loops are more convenient for certain kinds of problems

 for loops  Lab 5

 Read Chapter 5  Start Project 2