CS150 Introduction to Computer Science 1

Slides:



Advertisements
Similar presentations
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Advertisements

Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
Computer Science 1620 Loops.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
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.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
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.
CS150 Introduction to Computer Science 1
CS 1400 Chapter 5, section 2. Loops! while (rel-expression)while (rel-expression) {statements statement } if the relational-expression is true, execute.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
The If/Else Statement, Boolean Flags, and Menus Page 180
1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
First steps Jordi Cortadella Department of Computer Science.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
11/10/2016CS150 Introduction to Computer Science 1 Last Time  We covered “for” loops.
1 CS 1430: Programming in C++. 2 Find Max, Min, Average of m Sections Max, Min and Average of each section Max, Min and Average of all sections together.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
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.
Chapter 05 (Part II) Control Statements: Part II.
REPETITION CONTROL STRUCTURE
CS161 Introduction to Computer Science
C++ Programming: CS150 For.
for Repetition Structures
CS150 Introduction to Computer Science 1
Chapter 2.2 Control Structures (Iteration)
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Conditional Construct
Unary Operators ++ and --
CS 1430: Programming in C++ No time to cover HiC.
Counting Loops.
Intro to Nested Looping
Repetition Control Structure
CS150 Introduction to Computer Science 1
Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.
Computing Fundamentals
Intro to Nested Looping
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
switch Selection Structure
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Functions Divide and Conquer
Life is Full of Alternatives
Let’s all Repeat Together
Understanding Conditions
Arithmetic Operations
CS150 Introduction to Computer Science 1
do/while Selection Structure
CS150 Introduction to Computer Science 1
Types of loops definite loop: A loop that executes a known number of times. Examples: Repeat these statements 10 times. Repeat these statements k times.
CS150 Introduction to Computer Science 1
Presentation transcript:

CS150 Introduction to Computer Science 1 More on Loops So far, we’ve learnt about ‘while’ loops. Today will learn about ‘for’ loops. 2/21/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 For loops 3 main things for loops: Initialization of lcv, testing of lcv, updating lcv For loops provide a concise way to do this for (count = 0; count < 5; count++) cout << count << endl; 2/21/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 General Format for (initialization expression; loop repetition condition; update expression) { statements; } 2/21/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Examples Write a for loop that outputs odd numbers less than 10 Write a program that computes the factorial of a number 2/21/2019 CS150 Introduction to Computer Science 1

Localized Declarations for (int i = 0; i < n; i++) cout << i << endl; i is declared ONLY in the loop 2/21/2019 CS150 Introduction to Computer Science 1

Rewrite using a while loop for (i = 5; i < 10; i+= 2) cout << i; What does this output? 2/21/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Formatting Output How can we generate this output? Fahrenheit Celsius 32.000 0.000 33.000 0.556 34.000 1.111 35.000 1.667 36.000 2.222 2/21/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 The Program float F,C; F = 32.0; cout << fixed << setprecision(3); cout << "Fahrenheit "; cout << "Celsius" << endl; while (F <= 212.0) { C = (5.0/9.0)*(F - 32); cout << setw(10) << F << setw(12) << C << endl; F += 1; } You must have #include<iomanip> 2/21/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Programs Write a program that will print the sum of the odd integers between 1 and 50 inclusive. Write one program using a while and the other using a for loop. 2/21/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Program Write a program that inputs 50 numbers and outputs the largest and the smallest number 2/21/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Conditional Loops Loops that execute until some condition is met Includes more than counting Use while loops 2/21/2019 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Program Write a flag controlled loop that continues to read pairs of integers until it reads a pair with the property that the first integer is evenly divisible by the second 2/21/2019 CS150 Introduction to Computer Science 1