Week 5 - Friday CS 121.

Slides:



Advertisements
Similar presentations
Summer 2012 Instructor: Hassan Khosravi
Advertisements

CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
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.
Week 5 - Friday.  What did we talk about last time?  Repetition  while loops.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
Repetition. Examples When is repetition necessary/useful?
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Complexity (Running Time)
CHAPTER 2 ANALYSIS OF ALGORITHMS Part 2. 2 Running time of Basic operations Basic operations do not depend on the size of input, their running time is.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand basic loop concepts: ■ pretest loops and post-test loops ■ loop.
Chapter 6 – Repetition Statements : Objectives After you have read and studied this chapter, you should be able to Implement repetition control in a program.
Control Structures FOR Statement Looping.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Week 4 - Wednesday.  What did we talk about last time?  if statements  else statements  Nested selection statements.
More While Loop Examples CS303E: Elements of Computers and Programming.
Previously Repetition Structures While, Do-While, For.
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.
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
CS 100 Introduction to Computing Seminar October 7, 2015.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 5 Repetition.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
© The McGraw-Hill Companies, 2006 Chapter 3 Iteration.
Week 6 - Monday.  What did we talk about last time?  while loop examples  Lab 5.
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Computer C programming Chapter 3. CHAPTER 3 Program Looping –The for Statement –Nested for Loops –for Loop Variants –The while Statement –The do Statement.
More about Iteration Victor Norman CS104. Reading Quiz.
Week 9 - Monday CS 113.
Week 6 - Wednesday CS 113.
Week of 12/12/16 Test Review.
Week 3 - Friday CS222.
Selection and Python Syntax
CS161 Introduction to Computer Science
Week 4 - Wednesday CS 121.
Nelson Mandela Quotes.
Week 4 - Monday CS 121.
CHAPTER 5A Loop Structure
Chapter 5: Repetition Structures
Python - Iteration Iteration
Topics discussed in this section:
Repetition Chapter 6 12/06/16 & 12/07/16 1 1
Introduction to Programming
Chapter 6 Repetition Objectives ❏ To understand basic loop concepts:
Alice in Action with Java
Standard Algorithms Input validation Finding the minimum
CS212 Data Structures 2018 Second Semester.
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Chapter 8 The Loops By: Mr. Baha Hanene.
COMP 110 Loops, loops, loops, loops, loops, loops…
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Intro to Nested Looping
Lec 5 Nested Control Structures
Chapter 4 Loops While loop The for loop do… while break and continue
IST256 : Applications Programming for Information Systems
Design and Implementation
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CS150 Introduction to Computer Science 1
Intro to Nested Looping
Computer Science G10 T \2017 T shaikhah AlZeyoudi
The first number is posted telling what random number was selected, I did this for testing purposes, in the real thing it would not be there. Since the.
CS150 Introduction to Computer Science 1
Print the following triangle, using nested loops
Unit 8 Tracing Algorithms.
Creating your game.
EECE.2160 ECE Application Programming
Iteration – While Loops
Control Structures.
Week 7 - Monday CS 121.
Week 7 - Friday CS 113.
Presentation transcript:

Week 5 - Friday CS 121

Last time What did we talk about last time? Repetition while loops

Questions?

Project 2

Exam 1 Post Mortem

while Loop Examples

while( condition ) { statement1; statement2; … statementn; } Anatomy of a while loop while( condition ) { statement1; statement2; … statementn; } A whole bunch of statements

Guessing game Let’s say that you wanted to write a program to guess a number that a person had come up with The number is between 1 and 100 Every time the computer guesses a number, the person enters: H if the number is too high L if the number is too low F if the number was found

Guessing game algorithm Start with the minimum and maximum of the range Find the midpoint Ask the user if the midpoint is correct If the answer is too high, go to Step 1 using the minimum and the midpoint - 1 as the new range If the answer is too low, go to Step 1 using the midpoint + 1 and the maximum as the new range If the midpoint is correct, you’re done!

Nested loops Just as with if-statements, it’s possible to nest loops A repetitive task can be done inside of another repetitive task Be careful! You can make the computer do a lot of work

Triangular numbers Triangular numbers are 1, 3, 6, 10, … 1 = 1 3 = 1 + 2 6 = 1 + 2 + 3 10 = 1 + 2 + 3 + 4 Let’s write a program that expresses the nth triangular number by printing 1 on the first line, 1 and 2 on the second line, 1, 2, and 3 on the third line, and so on

Lab 5

Upcoming

Next time… for loops More loop examples

Reminders Keep reading Chapter 5 Keep working on Project 2