WARM-UP: MON, APR 7 What are loops used for in programming? What are at least 2 different kinds of loops?

Slides:



Advertisements
Similar presentations
Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
Computer Science 1620 Loops.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Summary of Loops Programming. COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand basic loop concepts: ■ pretest loops and post-test loops ■ loop.
Copyright © Texas Education Agency, Computer Programming For Loops.
COMP 110 Switch Statements and Loops Tabitha Peck M.S. February 6, 2008 MWF 3-3:50 pm Philips
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
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
* What kind of loop would I use to complete the following: A. Output all of the prime numbers that are less than 100,000 B. Output the Fibonacci sequence.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
Flow Control in Java. Controlling which instruction to execute next Sequential  Similar to walking, one step after another Branching  Similar to a fork.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
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.
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.
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.
Chapter 4: Control Structures II
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
Repetition Statements while and do while loops
Chapter 5: Control Structures II
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
CS 100 Introduction to Computing Seminar October 7, 2015.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
1 Class Chapter Objectives Use a while loop to repeat a series of statements Get data from user through an input dialog box Add error checking.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Program Structures Chapter 5. 5 Branching Allows different code to execute based on a conditional test. if, if-else, and switch statements.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
COMP Loop Statements Yi Hong May 21, 2015.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Java Fundamentals 4. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
Identify the Appropriate Method for Handling Repetition
Chapter 9 Repetition.
Chapter 5: Control Structures II
Chapter 5: Control Structures II
Chapter 5: Control Structures II
CHAPTER 5A Loop Structure
CiS 260: App Dev I Chapter 4: Control Structures II.
Repetition-Sentinel,Flag Loop/Do_While
Chapter 5: Control Structures II
Chapter 5 Repetition.
Chapter 9 Control Structures.
LOOPS BY: LAUREN & ROMEO.
Control Statements Loops.
Introduction to Repetition Structures
The structure of programming
do/while Selection Structure
Control Statements Loops.
PROGRAM FLOWCHART Iteration Statements.
Building Java Programs
Presentation transcript:

WARM-UP: MON, APR 7 What are loops used for in programming? What are at least 2 different kinds of loops?

CHAPTER 5: LOOPS PRE-AP COMPUTER SCIENCE CYCLE 6

REVIEW: IF STATEMENTS Used to make decisions/answer questions Formed using a conditional statement Conditional operators IF  IF-ELSE  IF – ELSE-IF – ELSE

IF STRUCTURE if (condition1) { //statements } else if (condition2) { //statements } else { //statements }

LOOPS Used to repeat tasks Benefits Reduce amount of code required Reduce copy-pasting / repetition Increase code efficiency (speed) Makes code more readable / easier to understand

TYPES OF LOOPS WHILE Loops Tasks with unknown stopping points “infinite” loops DO-WHILE Loops Tasks with unknown stopping points that MUST execute at least once FOR Loops Repeat ‘x’ times

TYPES OF LOOPS WHILE Loops Tasks with unknown stopping points “infinite” loops DO-WHILE Loops Tasks with unknown stopping points that MUST execute at least once FOR Loops Repeat ‘x’ times

WHILE LOOPS - STRUCTURE while (condition) { //statements //counter/environment change }

WHILE LOOPS - STRUCTURE while (condition) { //statements //counter/environment change } Statements – the tasks you want repeated Counter change – prevents infinite loops

EXAMPLE A: PRINT 1 THRU 100 int number = 1; while (number <= 100) { System.out.println(number); number++; }

EXAMPLE B: PRINT 1 THRU AN INPUTTED NUMBER int number = 1; int stop = console.nextInt(); while (number <= stop) { System.out.println(number); number++; }

EXAMPLE C: PRINT 10 MULTIPLES OF AN INPUTTED NUMBER int multiple = 1; int number = console.nextInt(); while (multiple <= 10) { System.out.println(number*multiple); multiple++; }

WARM-UP: APR 8 Write the standard structure for a WHILE loop. Why is it necessary to change the counter or environment inside of a WHILE loop? What will happen if you do not?

WARM-UP: APR 9 Correct the following code so that it finds the sum of 10 numbers. int sum = 0; while (count < 10) num = console.nextInt(); sum = sum + num; count++;

REVIEW: WHILE LOOPS Typically used for looping situations where you do not know how many times the loop will iterate Not always counter-controlled while (condition) { //statements //counter/environment change }

EXAMPLE: PRINT 1 THRU AN INPUTTED NUMBER int number = 1; int stop = console.nextInt(); while (number <= stop) { System.out.println(number); number++; }

FOR LOOPS Typically used in looping situations where the number of iterations is known Always counter controlled

FOR LOOPS - STRUCTURE for (start; stop; change) { //statements } Start, stop, change refer to the counter

EXAMPLE A: PRINT THE NUMBERS 1 THRU 100 int counter; for (counter=1; counter<=100; counter++) { System.out.println(counter); }

EXAMPLE B: PRINT THE WORD “HELLO” 5 TIMES int i; for (i=1; i<=5; i++) { System.out.println(“Hello”); }

EXAMPLE C: PRINT OUT ALL EVEN NUMBERS BETWEEN 0 AND 1000 for (int num=0; num<=1000; num=num+2) { System.out.println(num); }

FOR VS WHILE WHILE int i=0; while (i < 100) { System.out.print(i); i++; } FOR for (int i=0; i < 100; i++) { System.out.print(i); }

WARM-UP: APR 10 Write a FOR loop that would loop 25 times with a counter that begins at 12. QUIZ TOMORROW!