Handout 5 cs180 - Programming Fundamentals Spring 15 Page 1 of 8. Handout 5. Loops.

Similar documents
Loops. Repeat after me

Loops. CSE 114, Computer Science 1 Stony Brook University

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Chapter 4: Control structures. Repetition

Chapter 4: Control structures

Controls Structure for Repetition

Control Structures II. Repetition (Loops)

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Iteration statements - Loops

Java Classes: Math, Integer A C S L E C T U R E 8

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

Handout 4 Conditionals. Boolean Expressions.

Repetition, Looping. While Loop

CompSci 125 Lecture 11

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Control Structures: if and while A C S L E C T U R E 4

CS1150 Principles of Computer Science Loops (Part II)

Computational Expression

Introduction to the Java Basics: Control Flow Statements

Repetition CSC 121 Fall 2014 Howard Rosenthal

C++ Programming: From Problem Analysis to Program Design, Third Edition

Example. Write a program which sums two random integers and lets the user repeatedly enter a new answer until it is correct.

Full file at

Flow of Control: Loops. Chapter 4

STUDENT LESSON A12 Iterations

Chapter 3. Flow of Control. Branching Loops exit(n) method Boolean data type and expressions

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

Flow of Control. Branching Loops exit(n) method Boolean data type and expressions

CS 112 Introduction to Programming

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

COMP 202. Programming With Iterations. CONTENT: The WHILE, DO and FOR Statements. COMP Loops 1

Iteration: Intro. Two types of loops: 1. Pretest Condition precedes body Iterates 0+ times. 2. Posttest Condition follows body Iterates 1+ times

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

CSC 1051 Data Structures and Algorithms I

CSC 1051 Data Structures and Algorithms I

Pull Lecture Materials and Open PollEv. Poll Everywhere: pollev.com/comp110. Lecture 12. else-if and while loops. Once in a while

Repetition and Loop Statements Chapter 5

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

CP122 CS I. Iteration

Menu Driven Systems. While loops, menus and the switch statement. Mairead Meagher Dr. Siobhán Drohan. Produced by:

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

Java Coding 3. Over & over again!

Arrays. Eng. Mohammed Abdualal

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

Final Exam Practice. Partial credit will be awarded.

Example: Monte Carlo Simulation 1

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

CS111: PROGRAMMING LANGUAGE II

Programming Constructs Overview. Method Call System.out.print( hello ); Method Parameters

Array. Lecture 12. Based on Slides of Dr. Norazah Yusof

Repetition, Looping CS101

Loops! Step- by- step. An Example while Loop. Flow of Control: Loops (Savitch, Chapter 4)

Top-Down Program Development

CSC 1051 Data Structures and Algorithms I

CS 177 Week 5 Recitation Slides. Loops

Recap: Assignment as an Operator CS 112 Introduction to Programming

Loops / Repetition Statements

Object Oriented Programming. Java-Lecture 6 - Arrays

AP Computer Science Unit 1. Programs

Give one example where you might wish to use a three dimensional array

Computer Science is...

Java Control Statements

COMP-202: Foundations of Programming. Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016

Repetition with for loops

CSE 114 Computer Science I

Flow Control. Key Notion. Statement Categories. 28-Oct-10

Chapter 4 Loops. Lecture notes for computer programming 1 Faculty of Engineering and Information Technology Prepared by: Iyad Albayouk

Repetition. Chapter 6

Repetition. Chapter 6

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

Bjarne Stroustrup. creator of C++

St. Edmund Preparatory High School Brooklyn, NY

Flow Control. Boaz Kantor Introduction to Computer Science, Fall semester IDC Herzliya

204111: Computer and Programming

Warmup : Name that tune!

Problem Solving With Loops

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Definite Loops. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting

Final Examination Semester 2 / Year 2011

CS1004: Intro to CS in Java, Spring 2005

Introduction to Computer Science, Shimon Schocken, IDC Herzliya. Lectures Control Structures

Chapter 6. Repetition Statements. Animated Version The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Lesson 36: for() Loops (W11D1)

University of Palestine. Mid Exam Total Grade: 100

Computer Programming I - Unit 5 Lecture page 1 of 14

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

Question: Total Points: Score:

Question: Total Points: Score:

CMPT 125: Lecture 4 Conditionals and Loops

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Chapter 3. Selections

1 Short Answer (15 Points Each)

IST 297D Introduction to Application Programming Chapter 4 Problem Set. Name:

AP COMPUTER SCIENCE A

Programming in the Small II: Control

Transcription:

Handout 5 cs180 - Programming Fundamentals Spring 15 Page 1 of 8 Handout 5 Loops. Loops implement repetitive computation, a k a iteration. Java loop statements: while do-while for 1. Start with the while-loop. Syntax while(condition) Statement; //body of loop or while(condition) //body of loop First_Statement;... Last_Statement; true Body of loop condition false Several logical loop organizations 1. counting loops know exactly how many times to repeat a set of actions usually done with a use of a counter variable counter is initialized before the loop starts executing counter is updated after each iteration of the loop Example: print numbers from 0 to m. public class SimpleCountingLoop public static void main(string[] args) int i = 0; // the counter variable - to keep track of // number of times gone through the loop int m = 5; while (i < m) System.out.println("i is " + i + " now."); i++; System.out.println("Done. i is " + i ); Question: What will happen if m = -5? If the i++; in the loop body is omitted? Programming and Debugging Pitfalls: infinite loops, off by one - 1 -

Handout 5 cs180 - Programming Fundamentals Spring 15 Page 2 of 8 Practice problems: for all the problems, assume Scanner kb = new Scanner (System.in); 1. Write a code segment that allows the user to enter 10 numbers and computes and prints out their sum, average, product. 2. Write a code segment that asks the user how many numbers there are in a list, then allows the user to enter the numbers and computes and prints out the smallest and the largest number. 3. Write a code segment that prints out all characters of a string one character per line. - 2 -

Handout 5 cs180 - Programming Fundamentals Spring 15 Page 3 of 8 2.condition controlled loops repeat while a certain condition is true o sentinel-controlled loops stop repeating when a certain sentinel value is encountered Examples: 1. Let user input numbers until user enters 0. 0 is the sentinel in this case. Practice problems: Scanner kbrd = new Scanner (System.in); anum = kbrd.nextint(); while ( anum!= 0 ) System.out.print(aNum); anum = kbrd.nextint() ; 1. Write a code segment that continues to read a number from its user, until an even number is entered. 2. Write a code segment that continues to read user input until the user enters a number between 0 and 9. - 3 -

Handout 5 cs180 - Programming Fundamentals Spring 15 Page 4 of 8 o boolean flag controlled loops stop when a boolean variable that reflects a certain state is false Example: describe what happens, when the following code segment is executed. Scanner kb = new Scanner(System.in); System.out.println("Please enter a number:"); int prevnum = kb.nextint(); int currnum; boolean nomatch = true; // boolean flag while (nomatch) // same as nomatch == true System.out.println("Please enter next number:"); currnum = kb.nextint(); if (currnum == prevnum) nomatch = false; prevnum = currnum; Practice problem: How to make the above loop terminate in case no match occurred after 100 numbers were entered? - 4 -

Handout 5 cs180 - Programming Fundamentals Spring 15 Page 5 of 8 2. do-while loop: similar to while-loop, but condition is checked at the end of each iteration. The body of the loop is going to be executed at least once. do Statement; //body of loop while(condition); do //body of loop First_Statement;... Last_Statement; while(condition); true Body of loop condition false Practice problem: 1. Write a code segment that continues to read a number from the user, until the user enters an even number. Use a do-while loop. - 5 -

Handout 5 cs180 - Programming Fundamentals Spring 15 Page 6 of 8 3. for loops especially useful for counting loops. Syntax: for (init-statements; loop-condition; update-statements) //body of loop First_Statement; init-statements... Last_Statement; true Loopcondition Body of loop false Example: count down from 9 to 0 for( int count = 9; count >= 0; count--) System.out.print("T = " + count); System.out.println(" and counting"); Update-statements System.out.println("Blast off!"); Practice problems. 1. Write a code segment that prints out a reverse of the string entered by the user, e.g. given Walter it would print retlaw. 2. Write a code segment that reads in a string and a character from the user, and prints out the number of occurrences of the character inside that string. - 6 -

Handout 5 cs180 - Programming Fundamentals Spring 15 Page 7 of 8 4. Nested loops: When one loop is placed within the body of another, the entire construct is called nested loops. Example: for (int m = 1; m <= 10; m++) count = 1; // initialize the counter variable while ( count <= m ) System.out.print(count); count = count + 1; // same as count++ System.out.println( \n *** ) ; Practice problems 1. Print out a triangular pattern based on value stored in variable rows. The pattern below shows what s printed for value of rows equal to 7. 1 22 333 4444 55555 666666 7777777 2. What output is produced by the following code segment: int k, s; for (int j = 1; j <= 10; j++) if (j % 3 == 0) for (k = j, s = 0; k>=0; k--) s += k; System.out.println("j is "+ j+ " k is " +k+ " s is " + s); - 7 -

Handout 5 cs180 - Programming Fundamentals Spring 15 Page 8 of 8-8 -