Loops (Part 1) Computer Science Erwin High School Fall 2014.

Slides:



Advertisements
Similar presentations
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.
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
Computer Science 1620 Loops.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
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.
Flow of Control Loops – Chapter 3.2. Java Loop Statements: Outline the while Statement the do-while Statement the for Statement.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
1 Chapter 5 File Objects and Looping Statements (Some slides have been modified from their original format)
TODAY’S LECTURE Review Chapter 2 Go over exercises.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
By Chad Blankenbeker.  The for-loop is best used when you know how many times it is going to be looped  So if you know you want it to only loop 10 times,
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
Overview of Java Loops By: Reid Hunter. What Is A Loop? A loop is a series of commands that will continue to repeat over and over again until a condition.
Repetition Statements while and do while loops
1 Chapter 9. To familiarize you with  Simple PERFORM  How PERFORM statements are used for iteration  Options available with PERFORM 2.
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
Counting Loops.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
COMP Loop Statements Yi Hong May 21, 2015.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
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.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
LOOPS CHAPTER Topics  Four Types of Loops –while –do…while –for –foreach  Jump Statements in Loops –break –continue 2.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter 5: Loops Tarik Booker CS 201 California State University, Los Angeles.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II
Week 4 – Repetition Structures / Loops
Programming Fundamentals
Loops The loop blocks you've used in Scratch can all be recreated in C! Loops allow for the repetition of code until a condition is met. There are three.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Arrays, For loop While loop Do while loop
Outline Altering flow of control Boolean expressions
Introduction to pseudocode
Java Programming Loops
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
© A+ Computer Science - What is a LOOP? © A+ Computer Science -
Module 4 Loops.
Chapter 6: Repetition Statements
CHAPTER 21 LOOPS 1.
PROGRAM FLOWCHART Iteration Statements.
Looping and Repetition
Chapter 4: Loops and Iteration
Presentation transcript:

Loops (Part 1) Computer Science Erwin High School Fall 2014

How to Use the Slides As you go through the slides, answer the questions that appear on a separate piece of paper. You will turn this paper in when you complete the slideshow. If you have questions as you read, list these on your paper as well.

What is a Loop? A loop allows us to repeat a block of code. As long as the condition associated with the loop is true, the block of code will continue to be repeated.

Main Types of Loops A “while” loop A “do-while” loop A “for” loop

The “while” Loop The “while” loop is a very simple loop. As long as the condition associated with the “while” loop is true, the block of code will be executed and the loop will continue to run.

“while” Loop Layout while ( condition goes here ) { do something; do something else; }

“while” Loop Example int count = 0; while ( count < 5 ) { count = count + 1; System.out.println(count); }

“while” Loop Example int count = 0; while ( count < 5 ) { count = count + 1; System.out.println(count); } An integer variable named count is declared and set equal to zero.

“while” Loop Example int count = 0; while ( count < 5 ) { count = count + 1; System.out.println(count); } A while loop is created with the condition, count < 5, and the condition is checked.

“while” Loop Example int count = 0; while ( count < 5 ) { count = count + 1; System.out.println(count); } As long as count remains less than 5, the loop will run.

“while” Loop Example int count = 0; while ( count < 5 ) { count = count + 1; System.out.println(count); } Inside the loop, the value of count is increased by one, (from 0 to 1).

“while” Loop Example int count = 0; while ( count < 5 ) { count = count + 1; System.out.println(count); } The current value of count is printed to the console. Output:1

“while” Loop Example int count = 0; while ( count < 5 ) { count = count + 1; System.out.println(count); } The brace indicates we have reached the end of the block and the end of the 1st run or “iteration” through the loop. Output:1

“while” Loop Example int count = 0; while ( count < 5 ) { count = count + 1; System.out.println(count); } The condition is checked again with the current value of count (which is 1). Output:1

“while” Loop Example int count = 0; while ( count < 5 ) { count = count + 1; System.out.println(count); } Since count < 5 (1 < 5), the loop will run again. Output:1

“while” Loop Example int count = 0; while ( count < 5 ) { count = count + 1; System.out.println(count); } Inside the loop, the value of count is increased by one, (from 1 to 2). Output:1

“while” Loop Example int count = 0; while ( count < 5 ) { count = count + 1; System.out.println(count); } The current value of count is printed to the console. Output:1 2

“while” Loop Example int count = 0; while ( count < 5 ) { count = count + 1; System.out.println(count); } The brace indicates we have reached the end of the block and the end of the 2nd iteration of the loop. Output:1 2

“while” Loop Example int count = 0; while ( count < 5 ) { count = count + 1; System.out.println(count); } The condition is checked again with the current value of count (which is 2). Output:1 2

“while” Loop Example int count = 0; while ( count < 5 ) { count = count + 1; System.out.println(count); } The process continues and our output continues to update each time the loop is run (until the value of count is 5). Output:

“while” Loop Example int count = 0; while ( count < 5 ) { count = count + 1; System.out.println(count); } After the 5 th iteration, count = 5, which means the condition is not true (5 < 5 is false). Output:

“while” Loop Example int count = 0; while ( count < 5 ) { count = count + 1; System.out.println(count); } The loop is complete and the program will continue to any code given after the block statement (after the second brace). Output:

Problem 1 What is the output of the loop? (Pay attention to the small change.) int count = 0; while ( count < 5 ) { System.out.println(count); count = count + 1; }

Problem 2 What is the OUTPUT of the loop and the FINAL VALUE of run? int run = 7; while ( run < 10 ) { System.out.println(run); run++; }

Problem 3 What is the output? int x = 25; while ( x >= 10 ) { System.out.println(x); x = x - 5; }

Problem 4 What is the output? int total = 0, x = 1; while ( x < 6 ) { total = total + x; x++; } System.out.println(total);

Problem 5 What is the problem with the following loop? int count = 0; while ( count < 5 ) { System.out.println(count); }

Problem 6 What is the problem with the following loop? while ( run < 5 ) { int run = 0; System.out.println(run); run++; }

Problem 7 What happens as a result of the following code segment? int run = 10; while ( run < 5 ) { System.out.println(run); run++; }

Problem 8 AP Level Question: Consider the following method. public String mystery(String input) { String output = “”; int k = 1; while (k < input.length()) { output += input.substring(k, k + 1); k++; } return output; } What is returned as a result of the call mystery(“computer”)? ** Hint: Draw a table to keep track of the values of k and ouput. **

Problem 9 AP Level Question: Consider the following code segment. int sum = 0; int k = 1; while (sum < 12 || k < 4) { sum += k; } System.out.println(sum); What is printed as a result of executing the code segment?

The “do-while” Loop The “do-while” loop is very similar to the “while” loop, however there is one main difference. The block statement is executed once before the condition is ever checked; therefore, the loop is guaranteed to run at least once. If the condition is true after the first iteration, the loop is executed again and the condition is checked once more. This continues until the condition is not met and the loop is exited.

“do-while” Loop Layout do { do something; do something else; } while ( condition goes here );

“do-while” Example int count = 0; do { count++; System.out.println(count); } while ( count < 4 ); An integer variable named count is declared and set equal to zero.

“do-while” Example int count = 0; do { count++; System.out.println(count); } while ( count < 4 ); The do-while loop is entered.

“do-while” Example int count = 0; do { count++; System.out.println(count); } while ( count < 4 ); count is incremented (from 0 to 1).

“do-while” Example int count = 0; do { count++; System.out.println(count); } while ( count < 4 ); The value of count is printed to the console. Output:1

“do-while” Example int count = 0; do { count++; System.out.println(count); } while ( count < 4 ); The 1 st iteration is complete and the condition is checked. Since 1 < 4, the loop will run again. Output:1

“do-while” Example int count = 0; do { count++; System.out.println(count); } while ( count < 4 ); The loop continues to run until the condition is false (i.e. count < 4 is false). This first occurs when count = 4 at the end of the loop, but it’s value would have already been printed out. Output:

Problem 10 What is the output of the loop? int run = 4; do { System.out.println(run); run--; } while (run > 0);

Problem 11 What is the output of the loop? int x = 25; do { System.out.println(x); x -= 5; } while (x >= 10);

Problem 12 What is the output of the loop? int count = 0; do { System.out.println(count + 1); count = count + 2; } while (count < 10);

Problem 13 What are the errors in this loop? ** Hint: there are 2 of them ** int count = 0; do { System.out.println(count); } while (count < 5)