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.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
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.
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.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Chapter 5: Control Structures II (Repetition)
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Chapter 5: Loops and Files.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Objectives You should be able to describe:
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.
Chapter 5: Control Structures II (Repetition)
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
CPS120 Introduction to Computer Science Iteration (Looping)
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
Controlling Execution Dong Shao, Nanjing Unviersity.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
CPS120 Introduction to Computer Science Iteration (Looping)
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
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.
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3.
1 1 Additional Control Structures Chapter 9 2 New and Improved... Ways to branch Ways to write loops Understanding the break and continue statements.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
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.
Flow of Control: Loops Module 4. Objectives Design a loop Use while, do, and for in a program Use the for-each with enumerations Use assertion checks.
COMP Loop Statements Yi Hong May 21, 2015.
CSE 501N Fall ’09 07: Iteration 17 September 2009 Nick Leidenfrost.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Outline Altering flow of control Boolean expressions
CSC215 Lecture Control Flow.
Computing Fundamentals
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
CSC215 Lecture Control Flow.
Presentation transcript:

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 the quadratic equation.

Last Week You used an integrated development environment (emacs) to write, compile, and run your program. You wrote C++ programs that: –Declared variables –Performed mathematical operations –Read input, wrote output –Performed boolean operations –Made decisions (branched) based on boolean expressions

This Week You will learn how emacs makes programming easier (tabify, jump to error, window control, etc). You will write C++ programs that do everything the programs from last week did plus: –Variable scoping –Iterative control structures –Other control structures such as “switch,” “break,” and “continue.” –Use all the control structures in concert

Scope of Variables The scope of a variable x is the portion of the program from which x can be “seen.” Up to this point we have only placed variable declarations at the start of the program so that we wouldn’t have to worry about scope resolution. With iterative control structures scope starts to become an issue.

Scope of Variables Braces are the most common way of defining the scope of a variable. Variables declared after an “{” are destroyed when the matching close brace “}” is encountered. This allows us to return memory allocated to variables when we don’t need them anymore.

Scope of Variables, Example 1 int main { int x = 0; } cout << x << endl; return 0; }

Scope of Variables, Example int main { int y = 0; { int x = 0; } cout << y << endl; return 0; }

Nesting Scope Scopes “{ … }” can be nested just like we have been doing with “if” statements.

Nesting Scope, Example 1 { int x = 0; { int y = 0; // x exists, y exists } // x exists, y destroyed }

Nesting Scope, Example 1 { int x = 0; { int y = 0; // x exists, y exits, z does not yet exist } { int z = 0; // x exists, y destroyed, z exists } // x exists, y destroyed, z destroyed }

Shadowing Variables Variable scope resolution can become tricky when there are two or more variables with the same name in different scopes. The compiler would not have allowed this: int x = 0; int x = 1; We would have received a “variable redefinition” warning. But…

Shadowing Variables … it does allow this: int x = 0; { int x = 1; } This is useful because we don’t want to have to use a different name for every single variable in a program. Here one x is said to “shadow” the other.

Shadowing Variables If I use “x” at different points in the program I could be getting different variables. int x = 0; // here x = 0 { int x = 1; // here x = 1 } // here x = 0

Shadowing Variables When a name is used the name is searched for in the current scope first, then the scope enclosing this one, then the one enclosing that one, etc. int x = 0; { int x = 1; { int x = 2; cout << x; } int x = 0; { int x = 1; { int x = 2; } cout << x; }

Other Kind of Variable Scope There are lots of other kinds of scope rules and exceptions that we will see throughout the semester (e.g.): Global scope (global variables) File scope Class scope Static Variables

Iteration We can execute any algorithm with just “if” statements. But often we need to repeat the same piece of code over and over. Sometimes thousands or millions of times. Writing the same code with if statements alone in this case is impractical. Instead we use iterative control structures. Iteration is simply the process of doing something over and over.

The “while” loop The “while” loop is the most general type of iteration. The “if” control structure said if a certain condition is true then execute the following block of code. The “while” loop says so long as the condition is true keep executing the block of code over and over.

The “while” loop, Syntax while( boolean_expression ) { statements… } So long as the boolean_expression is true the statements will be executed over and over.

The “while” loop, Syntax while( boolean_expression ) { statements… } Typically the the block of statements will alter the boolean_expression such that it will eventually be false. If this does not happen an “infinite loop” will occur.

Symptoms of an Infinite Loop If an infinite loop occurs the same lines of code are executed over and over forever. This is a common “run-time” error. Symptom 1: Your program prints the same thing over and over in rapid succession. Symptom 2: Your program appears to freeze and do nothing. In actuality it is working as hard as it can but getting nowhere. Be careful though since your program may just be waiting for input.

The “while” loop, Examples int n = 0, x = 15; while( n < x) { cout << n; n = n + 1; } int x = 0, n = 15; int fact = 1; while( x < n) { x = x + 1; fact = fact*x; } cout << fact;

The “do …while” loop The do … while loop is very similar to the while loop. Where the while loops condition was a pre-condition the “do … while” has a post-condition. i.e with a while loop the condition is checked at the start of the loop, a “do… while” checks at the end. The effect is that the loop always executes at least once.

The “while” loop, Syntax do { statements… } while ( boolean_expression ); The statements are executed at least once, then so long as the boolean_expression is true the statements will be executed over and over.

The “do …while” loop, Example int month = -1; do { cout << “Please input month {1..12}\n”; cin >> month; } while ((month 12));

The “do …while” loop, Example int x = 0, n = 15; int fact = 1; do { x = x + 1; fact = fact*x; }while(x < n); cout << fact; int x = 0, n = 15; int fact = 1; while( x < n) { x = x + 1; fact = fact*x; } cout << fact;

The for loop Early programmers used goto statements to accomplish the same tasks as while and for loops but there are many different ways to do the same kind of loop with goto. For and while loops constrain the sorts of code you are likely to see and make it easy to recognize right away what the programmer is trying to do.

The for loop Anything that can be done with a for loop can be done with a while loop, and vice versa. While loops and for loops are equivalent.

The for - while loop equivalence If they are equivalent whey bother with the for loop? When a programmer uses a for loop it almost always means they are counting something: elements in a list, branches in a tree, it is almost always something discrete. Programmers use a while loop when the terminating condition is not so clear, for example reading input from a user.

The for loop, syntax for ( init_variables; pre-condition; statements ) { statements… } e.g. for ( int i = 0; i < 100; i++ ) { statements… } Note: i only has scope within the loop

The for loop, syntax for ( init; pre-condition; statements ) { statements… } Init is executed only once when the loop starts. Pre-condition and statements are executed on every loop. Pre-condition is executed at the start of the loop and “statements” are evaluated at the end.

The for loop, syntax for ( init; pre-condition; statements ) { statements… } Init can have multiple declarations and statements can have many statements separated by commas: for ( int i = 0, j=10; i < j; i += 5, j++ ) { statements… }

The for - while loop equivalence for (int i = 0; i < 100; i++ ) { statements… } int i = 0; while( i < 100 ) { statements… i++; } These two sections of code do exactly the same thing

The for loop, syntax Note: Microsoft Visual C does not use standard scoping and for loop syntax. MSVC 6 allows you to initialize variables in the for loop but not declare them there. This means they have scope outside the for loop. You must write: { int i; for ( i = 0; i < 0; i++ ) { statements… } Instead of: for ( int i = 0; i < 0; i++ ) { statements… }

The for loop, example int result = 1, base = 2, power = 0; cin >> power; for ( i = 1; i <= power; i++) { result = result * base; } cout << result;

Nested Control Structures All control structures can be nested in C++. Typically a program will consist of an outer loop (usually a “while” loop), that runs the program over and over until some condition is met and… Selection control structures such as “if” statements that decide which code to run.

Nested Control Structures for ( int i = 0; i < 2; i++ ) { for ( int j = 0; j < 4; j++) { cout << i << “ ”<< j << “ ”; } cout << endl; }