Activity 6: Loops. Content Learning Objectives. Process Skill Goals

Similar documents
Activity 6: Loops. Content Learning Objectives. Process Skill Goals

Activity 1: Introduction

Activity 1: Introduction

Activity 4: Boolean Logic

Activity 4: Methods. Content Learning Objectives. Process Skill Goals

Activity 3: Data Types

Activity 11: Designing Classes

Activity 9: Object-Oriented

LECTURE 5 Control Structures Part 2

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

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Activity 9: Object-Oriented

Repetition, Looping CS101

Activity 7: Arrays. Content Learning Objectives. Process Skill Goals

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

Chapter 4: Control structures. Repetition

Motivations. Chapter 5: Loops and Iteration. Opening Problem 9/13/18. Introducing while Loops

Chapter 4: Control structures

Loops. CSE 114, Computer Science 1 Stony Brook University

COMP 111. Introduction to Computer Science and Object-Oriented Programming

Control Structures II. Repetition (Loops)

Introduction to the Java Basics: Control Flow Statements

Lecture 5: Methods CS2301

Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

CS1150 Principles of Computer Science Loops (Part II)

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

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

B. Subject-specific skills B1. Problem solving skills: Supply the student with the ability to solve different problems related to the topics

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

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

Mobile App:IT. Methods & Classes

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

L o o p s. for(initializing expression; control expression; step expression) { one or more statements }

McGill University School of Computer Science COMP-202A Introduction to Computing 1

7. Arrays, More Java Looping

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

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

CS112 Lecture: Repetition Statements

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Loops / Repetition Statements

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

Basic Problem solving Techniques Top Down stepwise refinement If & if else.. While.. Counter controlled and sentinel controlled repetition Usage of

While Loops A while loop executes a statement as long as a condition is true while condition: statement(s) Statement may be simple or compound Typical

BSc (Hons) Computer Science with Network Security/ BSc (Hons) Software Engineering/ BSc (Hons) Web Technologies. Examinations for 2016 Semester 1

Building Java Programs

Building Java Programs

Building Java Programs

int j = 0, sum = 0; for (j = 3; j <= 79; j++) { sum = sum + j; System.out.println(sum); //Show the progress as we iterate thru the loop.

HUDSONVILLE HIGH SCHOOL COURSE FRAMEWORK

Repetition, Looping. While Loop

CS110D: PROGRAMMING LANGUAGE I

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

Assignment 2.4: Loops

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

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

Lesson 36: for() Loops (W11D1)

Programming for Engineers Iteration

CS 170 Exam 2. Version: A Fall Name (as in OPUS) (print): Instructions:

Object-Oriented Programming

(6-1) Iteration in C H&K Chapter 5. Instructor - Andrew S. O Fallon CptS 121 (February 11, 2019) Washington State University

STUDENT LESSON A12 Iterations

Chapter Goals. Contents LOOPS

CS1150 Principles of Computer Science Methods

For loops, nested loops and scopes. Jordi Cortadella Department of Computer Science

CS112 Lecture: Loops

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Conditionals. For exercises 1 to 27, indicate the output that will be produced. Assume the following declarations:

The for Loop. Lesson 11

CP122 CS I. Iteration

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Sorting on CUDA. Providence College. Ayushi Sinha Providence College

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Repetition Structures

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

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

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

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

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

CSC 1052 Algorithms & Data Structures II: Recursion

Chapter 4 Introduction to Control Statements

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

Loops (while and for)

Outline. For loops, nested loops and scopes. Calculate x y. For loops. Scopes. Nested loops. Algorithm: repeated multiplication x x x x

Hour of Code: Teacher Guide

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

CS111: PROGRAMMING LANGUAGE II

Recap: Assignment as an Operator CS 112 Introduction to Programming

Control Statements. Musa M. Ameen Computer Engineering Dept.

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1

Chapter 3 Structured Program Development

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

Operators Questions

To become familiar with array manipulation, searching, and sorting.

Units 0 to 4 Groovy: Introduction upto Arrays Revision Guide

Fundamentals of Programming Session 7

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Building Java Programs Chapter 2

Arrays. 1 Index mapping

Transcription:

Activity 6: Loops Computers are often used to perform repetitive tasks. Running the same statements over and over again, without making any mistakes, is something that computers do very well. Content Learning Objectives After completing this activity, students should be able to: Explain what happens when re-assigning a variable. Identify the three main components of a while loop. Implement the factorial function using a for loop. Process Skill Goals During the activity, students should make progress toward: Tracing the execution of while/for loops and predict their final output. (Critical Thinking) Copyright 2017 Chris Mayfield and Helen Hu. This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Model 1 Assignment Consider the following Java statements. What is the resulting value of each variable? A: int x, y; x = 1; y = 2; y = x; x = y; B: int x, y, z; x = 1; y = 2; z = y; y = x; x = z; C: int z, y; z = 2; z = z + 1; z = z + 1; y = y + 1; Value of x: Value of x: Value of z: Value of y: Value of y: Value of y: Value of z: Questions (15 min) Start time: 1. In program A, why is the value of x not 2? 2. In program B, what happens to the values of x and y? 3. In program B, what is the purpose of the variable z? 4. If program C runs, what happens to the value of z? 5. In program C, why is it possible to increment z but not y?

6. Because increment and decrement are so common in algorithms, Java provides the operators ++ and --. For example, x++ is the same as x = x + 1, and y-- is the same as y = y - 1. Write the value of x and y next to each statement below. int x = 5; x--; x--; int y = -10; y++; y++; 7. Like the assignment operator, the ++ and -- operators replace the value of a variable. Java also has compound assignment operators for convenience. For example, the statement x = x + 2 can be rewritten as x += 2. Simplify the following assignment statements. step = step + 5; size = size - 3; total = total * 2; change = change / 10; hours = hours % 24; 8. Which of the following assignment statements can also be rewritten like the ones in #7? step = 5 + step; size = 3 - size; total = 2 * total; change = 10 / change; hours = 24 % hours; Model 2 While Loops A loop is a set of instructions that are to be repeated. All loops have three main components: initialize, test, and update. Label each of these components in the two example loops below. // pre-test loop // post-test loop number = 1; number = 1; while (number <= 10) { do { number++; number++; while (number <= 10);

Questions (15 min) Start time: 9. Which loop component always happens first? Why? 10. Explain why the while loop is called a pre-test and the do while loop is called a post-test. 11. What is output (to the screen) by each loop? 12. What is the final value of number at the end of each loop? 13. What is output if you swap the println and number++ statements? 14. What is the output if you remove the number++ statement? 15. What is output by the loop below? number = 99; do { number++; while (number <= 10);

16. What is the output of the following loop? (And what mistake was made?) i = 0; while (i < 3) System.out.println("i = " + i); i = i + 1; 17. What is the difference between a while statement and an if statement? Model 3 For Loops The for loop combines initialize, test, and update into one line of code. Label each of these components in the two example loops below. (Assume the variable number has already been declared.) // count forwards for (number = 1; number <= 10; number++) { // count backwards for (number = 10; number >= 1; number--) { Questions (15 min) Start time: 18. What do each of the for loops output to the screen? Be specific. 19. Describe how to make these loops display even numbers only (2 4 6 8 10 and 10 8 6 4 2).

20. Write a for loop that prints each character of a string on a separate line. You will need to invoke the length() and charat() methods. Assume the string variable is named word. 21. Rewrite your for loop in #20 as a while loop. 22. Write a loop that computes the factorial of a given integer n. Recall that n! = n (n 1) (n 2)... 1. Store your result in a variable named fact. 23. A nested loop is one that exists within the scope of another loop. This construct is often used when there are two variables for which all combinations must be examined. for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { System.out.printf("The product of %d and %d is %d\n", i, j, i * j); System.out.println(); Write nested loops that compute and display the factorial of each integer from 1 to 20. (Reuse your code from the previous question.) Your output should be in this format: The factorial of 1 is 1 The factorial of 2 is 2 The factorial of 3 is 6 The factorial of 4 is 24