Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.

Slides:



Advertisements
Similar presentations
Introduction to Computing Science and Programming I
Advertisements

1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
11.3 Function Prototypes A Function Prototype contains the function’s return type, name and parameter list Writing the function prototype is “declaring”
CS 280 Data Structures Professor John Peterson. Big O Notation We use a mathematical notation called “Big O” to talk about the performance of an algorithm.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Chapter 4 Making Decisions
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Looping Exercises Deciding Which Loop to Use At this.
Loop Exercise 1 Suppose that you want to take out a loan for $10,000, with 18% APR, and you're willing to make payments of $1,200/month. How long will.
General Programming Introduction to Computing Science and Programming I.
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Designing Programs with Branches CSIS 1595: Fundamentals of Programming and Problem Solving 1.
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
Textbook Problem Java – An Introduction to Problem Solving & Programming, Walter Savitch, pp.219, Problem 08.
Selection Control Structures. Simple Program Design, Fourth Edition Chapter 4 2 Objectives In this chapter you will be able to: Elaborate on the uses.
More While Loop Examples CS303E: Elements of Computers and Programming.
Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
CSCI-100 Introduction to Computing
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
11. EXCEPTION HANDLING Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Count Controlled Loops (Nested) Ain’t no sunshine when she’s gone …
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
CS 141 Computer Programming 1 Branching Statements.
Iterative Structures (Loops) CS 1401 Spring 2013 Shirley Moore, Instructor February 28, 2013.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
General Condition Loop A general condition loop just loops while some condition remains true. Note that the body of the loop should (eventually) change.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Introduction to Computing Science and Programming I
More on Functions (Part 2)
CS 1430: Programming in C++ No time to cover HiC.
C++ Programming: CS150 For.
Thinking about programming
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
More Repetition While and For Loops Sentinel-Controlled Loops
Intro to Computer Science CS1510 Dr. Sarah Diesburg
More Nested Loops and Lab 5
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Nested Looping
Types, Truth, and Expressions (Part 2)
String Encodings and Penny Math
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Types, Truth, and Expressions (Part 2)
Count Controlled Loops (Nested)
Intro to Nested Looping
Introduction to Computer Programming
Types, Truth, and Expressions (Part 2)
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
The while Looping Structure
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
More on Functions (Part 2)
More on Functions (Part 2)
Intro to Nested Looping
Types, Truth, and Expressions (Part 2)
Programming Concepts and Database
Thinking about programming
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Thinking about programming
Intro to Nested Looping
Types, Truth, and Expressions
Introduction to Strings
REPETITION Why Repetition?
Presentation transcript:

Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg

Some Things From PA02 BMI  These were continuous regions. Set up your code to handle all areas…

Some Things From PA02 BMI  While this is valid mathematically, it is bad form in programming languages.  And it causes real problems when not set up properly.

Some Things From PA02 BMI  Recognize that these are four related categories. This makes it much easier to use if/elif/else.

Some Things From PA02 BMI  Recognize that these are four related categories. This makes it much easier to use if/elif/else.

Some Things From PA02 BMI  Recognize that these are four related categories. This makes it much easier to use if/elif/else.

Some Things From PA02 Triangle  Watch for messages that can only be printed under certain circumstances 4,3,5 is not in order “If the triangle is legal it also checks to see if the triangle is a right triangle.”

Nesting Nesting means to place one code structure inside of another Nested if statements If (a<1): if (a==0): print(“Hey, this is zero”) else: print(“This is a negative number”) 8

Nested Loops We can also nest loops Can be very powerful, but tricky! 9

The idea of nested looping for x in range(1,4): for y in range(1,5): print (x, '*', y, '=', (x*y)) How many times will this print??

Finding Out if a Number is Prime An integer, N, is prime if it is not evenly divisible by any of the numbers from 2 to N-1

Finding Out if a Number is Prime Is 41 a prime number? Is 77 a prime number?

Finding Out if a Number is Prime Is 413 a prime number? Is 419 a prime number?

Let’s Write a Design Paragraph Inputs? Steps to calculate result Outputs? 14

Finding Out if a Number is Prime Write a program that:  Asks for an integer greater than 2  Keeps prompting until it gets one  Checks the numbers from 2 to N-1 to see if N is evenly divisible by that number  Prints a message either way.

So how would we change this… To print information about all of the numbers from 2 to 100?  Nested Looping