Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.

Slides:



Advertisements
Similar presentations
Do-while Loops Programming. COMP102 Prog Fundamentals I: do-while Loops /Slide 2 The do-while Statement l Syntax do action while (condition) l How it.
Advertisements

If Statements & Relational Operators Programming.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Branching Constructs Review l what are branching constructs? what type of branching constructs have we studied? l what is nested if? l what is multiway.
Computer Science 1620 Loops.
Iterative Constructs – chapter 4 pp 189 to 216 This lecture covers the mechanisms for deciding the conditions to repeat action. Some materials are from.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
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/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
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.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
CS150 Introduction to Computer Science 1
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Loops. COMP104 Loops / Slide 2 Shortcut Assignment * C++ has a set of operators for applying an operation to a variable and then storing the result back.
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
Functions:Passing Parameters by Value Programming.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Summary of Loops Programming. COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated.
Iterative Constructs Mechanisms for deciding under what conditions an action should be repeated JPC and JWD © 2002 McGraw-Hill, Inc.
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 C++ Control Statements ~ Selection and Iteration.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
CONTROLLING PROGRAM FLOW
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
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.
1 COMS 261 Computer Science I Title: String Class Date: October 3, 2005 Lecture Number: 14.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Overview Go over parts of quiz? Another iteration structure for loop.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Algorithms JPC and JWD © 2002 McGraw-Hill, Inc. 2 Algorithms 2 An Algorithm is a finite set of precise instructions for performing a computation or for.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
Introduction to Computer Programming
Programming Loops (continued).
C++ Iterative Constructs
Chapter 2 Assignment and Interactive Input
Branching Constructs Review
Chapter 5: Loops and Files.
Repetition Structures (Loops)
Programming Fundamentals
Conditinoal Constructs Review
COMS 261 Computer Science I
Intro to Programming Week # 4 Control Structure Lecture # 8
Alternate Version of STARTING OUT WITH C++ 4th Edition
Conditional Construct
Conditinoal Constructs Review
Counting Loops.
Miscellaneous Flow Control
Summary Two basic concepts: variables and assignments Basic types:
CS150 Introduction to Computer Science 1
Control Structures Part 1
Let’s all Repeat Together
Iterative Constructs Mechanisms for deciding under what conditions an action should be repeated JPC and JWD © 2002 McGraw-Hill, Inc.
Presentation transcript:

Loops Programming

COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing the result back into the variable Shortcut assignments: *=, /=, +=, -=, %= l Examples int i = 3; i += 4; // i = i + 4 cout << i << endl; // i is now 7 double a = 3.2; a *= 2.0; // a = a * 2.0 cout << a << endl; // a is now 6.4 int change = 1265; change %= 100; // change = change % 100 cout << change << endl; // change is now 65

COMP104 Lecture 9 / Slide 3 Increment and Decrement l C++ has special operators for incrementing or decrementing an object by one l Examples int k = 4; ++k; // k=k+1 : k is 5 k++; // k=k+1 : k is 6 cout << k << endl; int i = k++; // i is 6, k is 7 cout << i << " " << k << endl; int j = ++k; // j is 8, k is 8 cout << j << " " << k << endl;

COMP104 Lecture 9 / Slide 4 Increment and Decrement l What is the difference between k++ and ++k? n ++k increments first, and the incremented value is used in the expression n k++ uses the initial value of k in the expression, and increments afterwards l Examples int a, b, c, d, k; k = 3; a = ++k;// k=4, a=4 b = --a;// a=3, b=3 c = b++;// c=3, b=4 d = c--;// d=3, c=2

COMP104 Lecture 9 / Slide 5 Iterative Constructs l Provide n Ability to control how many times a statement list is executed l Three constructs while statement for statement do-while statement

COMP104 Lecture 9 / Slide 6 The while Statement l Syntax while (Expression) Action l How it works: n If Expression is true then execute Action n Repeat this process until Expression evaluates to false l Action is either a single statement or a group of statements within braces Expression Action truefalse

COMP104 Lecture 9 / Slide 7 N! ( while ) int number, factorial, n; cout << "Enter number: "; cin >> number; factorial = 1; n = 1; while(n <= number){ factorial *= n; n++; } cout << "The factorial of " << number << " is " << factorial << endl;

COMP104 Lecture 9 / Slide 8 2 N ( while ) int number, result, n; cout << "Enter number: "; cin >> number; result = 1; n = 1; while(n <= number){ result *= 2; n++; } cout << "Two raised to the " << number << " power is " << result << endl;

COMP104 Lecture 9 / Slide 9 Maximum ( while ) int value=0;//input value int max=0;//maximum value while(value!=-1){ cout << "Enter a value (-1 to stop): "; cin >> value; if(value > max) max = value; } cout << "The maximum value found is " << " is " << max << endl;

The value of the input operation corresponds to true only if a successful extraction was made #include using namespace std; int main() { int listSize = 0; int value; double sum = 0; double average; cout << "Provide a list of numbers (CTRL-D to stop) " << endl; while (cin >> value) { sum += value; listSize++; } if(listSize > 0){ average = sum / listSize; cout << "Average: " << average << endl; } else cout << "No list to average" << endl; return 0; }