Announcements The first graded lab will be posted Sunday 2/15 and due Friday 2/27 at midnight It is brand new, so please don’t hand in last semester’s.

Slides:



Advertisements
Similar presentations
Algorithms Series of mathematical or variable manipulations Integer a,b,c a = 12 b = 22 c = a * b (what is c?) 12 * 22 = 264.
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
1 Loops. 2 Often we want to execute a block of code multiple times. Something is always different each time through the block. Typically a variable is.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
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.
Chapter 5: Loops and Files.
Computer Science 1620 Programming & Problem Solving.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
Objectives You should be able to describe:
Libraries Programs that other people write that help you. #include // enables C++ #include // enables human-readable text #include // enables math functions.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
Chapter 4 Program Control Statements
Do … while ( continue_cond ) Syntax: do { stuff you want to happen in the loop } while (continue_condition);
CPS120 Introduction to Computer Science Iteration (Looping)
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Semester Review. As we have discussed, Friday we will have in class time for you to work on a program, this program will come with instructions and you.
CONTROLLING PROGRAM FLOW
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Operations Lecture 9.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
# ACS 168 Structured Programming Using the Computer Chapter 2 Spring 2002 Prepared by Shirley White.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
CPS120 Introduction to Computer Science Iteration (Looping)
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
1 CS161 Introduction to Computer Science Topic #8.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Controlling Program Flow with Looping Structures
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
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 Looping 5. The Increment and Decrement Operators 5.1.
COMP Loop Statements Yi Hong May 21, 2015.
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
Looping Increment/Decrement Switch. Flow of Control Iteration/Switch Statements.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Introduction to Computer Programming
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
CS161 Introduction to Computer Science
C++ Programming: CS150 For.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Programming Fundamentals
Arrays, For loop While loop Do while loop
Chapter 6: Repetition Statements
Presentation transcript:

announcements The first graded lab will be posted Sunday 2/15 and due Friday 2/27 at midnight It is brand new, so please don’t hand in last semester’s lab 3 that you got from your roommate.

Functional Paragraphs another use for { }

operation ( condition ) // parentheis { action, based on condition } // curly brackets // example int x = 0; while ( x < 100 ) { cout << x << endl; x = x + 1; }

Iteration

Repeatedly execute the same, bracketed section of C++ code Execute until certain conditions are met while loops (two types) - loop until a condition is met “for” loops - loop a defined number of times

loop while a condition is “true” while ( x < 6 ) {..... bool input = false; while ( input == false ) // loops until input is changed {..... while ( true ) // loops forever {.....

while…. 3 parts int x = 0; // 1. initialize a counting variable while (x < 10) // 2. conditions to keep looping { x = x + 2; // 3. how much to increment }

write a program… calculate (and display) the factorial of a number factorial of 6 : 6 * 5 * 4 * 3 * 2 * 1 get the number from the user ask the user to continue or quit will require the use of if (….. and while (…..

size of numbers The range of integer values that can be stored is -2,147,483,648 through 2,147,483,647 1,932,053,504

"long" ints long factorial = 0; old computers (32 bit): -2,147,483,648 through 2,147,483,647 new computers (64 bit): -9,223,372,036,854,775,808 thru 9,223,372,036,854,775,807

really? different? aspects of C++ SHOULDN'T be computer- dependent solution: long long factorial = 0;

Reading Chapter 2 Sections 2.2, 2.3 Pages 56-83

Legality Variable value and variable type must be the same. Deterministic behavior - All computers yield the same result with the same variables

Computers DO NOT completely check our programs Called “Compilation” (i.e. Compile & Link) Must be successful before a program can “run” But not all errors are caught in compilation

So, Illegality is... “Compiler” Errors – however, some languages and compilers allow mis- designations of variables Indeterminate results “Run-time” Errors - only show up when you run the program

Illegal #1 – mis-defined variables int y; y = ; char q; q = "abcdef"; double z; z = "hello"; bool operatorFault; operatorFault = “true”; // !!??!!

Why “Type” Variables Deterministic behavior of variables and the numbers they represent. New definition of “Legality” : We will never perform mathematical or logical operations on variables of different types.

Illegal #2 – mixing variables in same equation int x; double y; char z; x = 12; y = ; z = x + y;

Illegal #3 - Operation wrong for variable type: char Q, R, S; Q = R / S; // remember "/" is divide

to help - casting converting INTS to DOUBLES, and DOUBLES to INTS - for the intentional translation of a counting to/from a measuring number int x = 14; double r = ; x = (int) r; // x will be 3 TRUNCATED! r = (double) x; // r will be

The while loop while ( something ) // performs test, and { // if test evaluates to true do something // performs this action } // goes back to the top

the “do-while” loop do { // repeated code here } while ( time > 0);

what's the difference while tests first, then performs do...while performs, then tests do...while will always execute at least once

loops, in general you need a looping int variable, like x you need to know the initial value e.g. int x = 1; you need to know the condition under which to keep looping e.g. while (x<10) you need to increment or decrement that variable e.g. x = x+1; or x = x-4; that variable's behavior depends on where you use it, in relation to where you change it

Try running this…. then this

for loop for (x = start; condition for x valid; increment ) { // repeated code here } for loops use an “auto-increment” INTEGER variable for ( x = 0; x < = 100; x=x+1 ) // x goes from 0 to 100 { }

int Counter = 0; for (Counter = 4; Counter < 10; Counter = Counter + 2) { } Counter is AUTOMATICALLY 4, 6, 8

for loop int x=0; int total = 0; for (x = 0; x < 10; x = x+1) { total = total + x; } cout << “x value: ” << x; cout << “total value: ” << total; on screen… x value: 9 total value: 45

Flow of Control within a program int main ( ) { int x = 1; for (x = 0; x<5; x=x+1) { } loopx = 0, 1, 2, 3,

shorthand 1 x++ means x = x+1

leaving a loop continue; // re-loops break; // program falls out of immediate // brackets // note: continue continues, break breaks (the // loop) return 0;// exits main (i.e. the PROGRAM)

build a Fahrenheit to Celsius converter C = (F - 32) * (5/9) F = ( C * (9/5) ) + 32 note: any division should be performed with doubles C = (F – 32.0) * (5.0/9.0) F = ( C * (9.0/5.0) )

cout - decimal precision Internally 15 decimal places, due to memory limitations What cout DISPLAYS is different use "cout. precision( n ); " to set displayable values to n characters (plus dec pt.).