CS 200 Loops Jim Williams, PhD.

Slides:



Advertisements
Similar presentations
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Advertisements

Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Looping Yong Choi School of Business CSU, Bakersfield.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
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
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Chapter 6 Iteration.  Executes a block of code repeatedly  A condition controls how often the loop is executed while (condition) statement  Most commonly,
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Chapter 5 Loops.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
Chapter 4: Control Structures II
Chapter 5: Control Structures II
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Java Prepared by Gary Langner Types of Loops u for loop (entrance controlled) –do an action for a definite number of times u while loop (entrance controlled.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Application development with Java Lecture 6 Rina Zviel-Girshin.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
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.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Structured Programming Structured Programming is writing a program in terms of only 3 basic control structures: sequence selection repetition We have already.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
UCT Department of Computer Science Computer Science 1015F Iteration
Lecture 4 CS140 Dick Steflik. Reading Keyboard Input Import java.util.Scanner – A simple text scanner which can parse primitive types and strings using.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
Information and Computer Sciences University of Hawaii, Manoa
Chapter 5: Control Structures II
Loop Structures.
Repetition-Counter control Loop
Chapter 5: Control Structures II
CS Week 14 Jim Williams, PhD.
CS 200 Arrays, Loops and Methods
CS 302 Week 15 Jim Williams, PhD.
CS 200 Branches Jim Williams, PhD.
CS Week 8 Jim Williams, PhD.
CS 200 Using Objects Jim Williams, PhD.
CS Week 6 Jim Williams, PhD.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
CS Week 7 Jim Williams, PhD.
CS 200 Arrays, Loops and Methods
Outline Altering flow of control Boolean expressions
Lesson 05: Iterations Topic: Introduction to Programming, Zybook Ch 4, P4E Ch 5. Slides on website.
CS Week 3 Jim Williams, PhD.
Week 6 CS 302 Jim Williams, PhD.
CS 200 More Primitives, Objects, Branches and Methods
CS Week 3 Jim Williams.
Control Statements Loops.
CS 200 Primitives and Expressions
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
CS 200 Arrays Jim Williams, PhD.
CS Week 4 Jim Williams, PhD.
CS 200 Primitives and Expressions
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
Control Statements Loops.
Repetition Statements
Building Java Programs
Building Java Programs
Additional control structures
Loops CGS3416 Spring 2019 Lecture 7.
CS Week 3 Jim Williams, PhD.
Week 7 CS 302 Jim Williams.
CS302 Week 6 Jim Williams.
Presentation transcript:

CS 200 Loops Jim Williams, PhD

This Week Exam 1: Thursday P5 (Eclipse & zyBooks): Thursday Team Lab: More & Maze Hours Last Week? Lecture: Loops, Review

Midterm Exam 1 What is the location of your exam? 125 Agriculture Hall 6210 Sewell Social Sciences B102 Van Vleck Other (due to conflicts) notified via email What is the location of your exam?

Midterm Exam - Thursday Bring ID and #2 pencil Exam seating directly in front/behind, 1 empty seat to each side Multiple choice - focus on tracing/explaining Java Review Questions posted on Piazza

Method Value The value of a method is the value returned. The method may also read from a Scanner, print output or have another side effect. static boolean isJava(String name){ System.out.println(name); return name.endsWith(".java"); }

Memory Areas Stack Heap frame contains local variables and parameters allocated on method call, freed on method return Heap allocated when needed, (e.g., using new) use references to access garbage collector frees memory no longer accessible review 1 passing primitive to method changing parameter in method 2 passing array to method changing contents of array in method 3 passing array to method changing parameter to another array in method 4 passing a string to method calling string method in method public class ClassNameHere { static void method1(int num) { num = 5; } static void method2(int []listA) { listA = new int[3]; listA[1] = 10; static void method3(StringBuffer name) { name //name.toUpperCase(); public static void main(String[] args) { String name = "Helen"; method3( name); System.out.println( name); // int [] listB = {4, 6, 8}; // method2( listB); // System.out.println( listB[1]); // int num = 4; // method1( num); // System.out.println("num=" + num);

Primitives and Wrapper classes Draw a picture of memory and describe each. public static void main( String []args) { int k; Integer m; k = 2; //example of ? m = 3; //example of ? k = m; //example of ? }

Strings and memory class S { public static void main( String []args) { String name; new String(“dog”); name = new String(“happy”); }

Where is memory allocated? name: heap new String("spot"): heap name: stack new String("spot"): stack public static void main( String []args) { String name; name = new String("spot"); new String("fido"); } B is the best answer A, C and D are not true

Loops https://www.wikihow.com/Race-Your-Car

Loops Various ways of repeating statements, over and over and over and over and over… Different purposes (while, for, do-while) Everything can be done with a while loop but other loops are better choices for code readability. Keywords that change execution Nesting Diagram

Common operators Increment means add 1, decrement subtract 1 prefix and postfix operators int m = 2; m = m + 1; //add one to m store in m m += 1; //compound operator m++; //increment operator (post) ++m; //increment operator (pre)

Value of i, j after this executes? int i = 5; int j = i++; 5,5 6,5 5,6 other

Value of m after this executes? int i = 5; int k = 6; int m = i++ + ++k; 11 12 other try it.

Potential Confusion: Difference between if and while boolean doIt = true; if ( doIt ) { System.out.println( doIt ); } while ( doIt ) {

3 loops - Different Purposes do { //indefinite - body executes at least once } while ( !finished); while ( !finished) { //indefinite - body may never execute } for ( int i=1; i < 5; i++) { //definite

Which loop is the better choice? int a = 1; while ( a < 5) { System.out.println("hello"); ++a; } while for other for ( int a = 1; a < 5; ++a) { System.out.println("hell o"); } for since a definite number of iterations.

UML Activity Diagram (Flow Chart)

Which is best Java construct for this? If statement for loop while loop do while loop

Arrange Code to Match Diagram //declarations... off = true; System.out.println("RING, RING”); System.out.println(“Sleeping”); } while ( !off); System.out.print(“Enter to snooze or ‘off’"); if ( response.equals("off")) { off = false; } do { //declarations are assumed... off = false; do { System.out.println(“Sleeping”); System.out.println("RING, RING”); System.out.print(“Enter to snooze or ‘off’"); response = input.nextLine(); off = response.equals("off"); } while ( !off);

How many times will this execute? 1 until num > 0 until num <= 0 Scanner input = new Scanner( System.in); int num; int sum = 0; do { num = input.nextInt(); sum += num; } while ( num > 0); try it

Body of While will execute... boolean finished = false; while ( ! finished) { String line = input.nextLine(); if ( line.equals("done")) { finished = true; } else { System.out.println("echo:" + line); } until "done" is input never forever other? try it

How many "hello"? for ( int a = 0; a <= 5; ++a) { System.out.print("hello "); } 4 5 6 other? try it

Review Topics

What does this do? for ( char ch = 'A'; ch < 'Z'; ch += 3) { System.out.print( ch); }

What is the value of i after this? int i = 4; int j = 3; while ( i <= 15) { i = 2 * -i + j; System.out.println( "i:" + i + " j:" + j); } 5 13 -23 49

Nested Loops

How many "bye"? for ( int i = 1; i <= 5; i++) { for ( int j = 2; j < 6; j++) { System.out.print("bye "); } //same output line or different? 5 9 20 other?

What does this print? for ( int i = 1; i <= 5; i++) { 1,1 1,2 1,3 2,1 2,2 2,3 3,1 3,2 3,3 4,1 4,2 4,3 5,1 5,2 5,3 1,1 2,1 3,1 1,2 2,2 3,2 1,3 2,3 3,3 1,4 2,4 3,4 1,5 2,5 3,5 for ( int i = 1; i <= 5; i++) { for ( int j = 1; j <= 3; j++) { System.out.print(i+","+j+" "); } System.out.println();

What is the value of count? int count = 0; for ( int i = 1; i < 4; i++) { for ( int j = 6; j > 3; j--) { count += 1; } System.out.println("count=" + count); 3 9 12 other

Changing Behavior of Loop continue - skip rest of body, check condition break - leave loop immediately

What is the value of sum? 3 9 12 other int sum= 0; boolean done = false; for ( int i = 1; i < 4 && !done; i++) { for ( int j = 6; j > 3; j--) { if ( j > 4) continue; sum += 1; } if ( i == 3) break; System.out.println("sum=" + sum); 3 9 12 other

How many times does this loop execute? 9 10 11 infinite for ( int i = 9; i >= 0; i--) { System.out.println( "i=" + ++i); }

Today Exam Today Time Spent