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.

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 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Do/while Structure (L15) * do/while structure * break Statement * continue Statement * Loop Programming Techniques - Interactive input within a loop -
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.
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.
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.
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.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
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.
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.
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
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.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
 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.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
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.
Chapter 6.  Control Structures are statements that are used to perform repetitive tasks and to make decisions within a program. Loop repeats a sequence.
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.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
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.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Chapter five exercises. a. false; b. true; c. false; d. true; e. true; f. true; g. true; h. false.
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
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.
Introduction to Computer Programming
Programming Loops (continued).
C++ Iterative Constructs
REPETITION CONTROL STRUCTURE
while Repetition Structure
Chapter 2 Assignment and Interactive Input
Control Statements Kingdom of Saudi Arabia
Programming Fundamentals
Conditinoal Constructs Review
COMS 261 Computer Science I
Intro to Programming Week # 4 Control Structure Lecture # 8
Conditional Construct
Loops (iterations, repetitions)
Conditinoal Constructs Review
Counting Loops.
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.
do/while Selection Structure
Repetition Statements (Loops) - 2
Presentation transcript:

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 to a variable and then storing the result back into that variable. l Shortcut assignments (a and b are integers): shortcut same as *=a *= b;a = a*b; /=a /= b;a = a/b; +=a += b;a = a+b; -=a -= b;a = a-b; %=a %= b;a = a%b;

COMP102 Prog Fundamentals I: while Loops/Slide 3 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

COMP102 Prog Fundamentals I: while Loops/Slide 4 Loops (Iterative Constructs) l Loops allow a code segment to be executed many times. l Three constructs n while statement n for statement n do-while statement

COMP102 Prog Fundamentals I: while Loops/Slide 5 The while Statement l Syntax while (condition) action l How it works: n if condition is true then execute action n repeat this process until condition evaluates to false l action is either a single statement or a group of statements within braces. condition action true false

COMP102 Prog Fundamentals I: while Loops/Slide 6 The while Loop while (it's raining){ }

COMP102 Prog Fundamentals I: while Loops/Slide 7 Compute N! n! (n factorial) is defined as the product of all the integers from 1 to n. n! = 1*2*3*...*n or n! = (n-1)!*n Example: 5! = 1 x 2 x 3 x 4 x 5 = 120 How to compute 5! ? 1! = 1 ( store the temporary result in T, T is 1! ) 2! = 1! x 2 = T x 2 = 2 ( store the temporary result in T, T is 2! ) 3! = 2! X 3 = T x 3 = 6 ( store the temporary result in T, T is 3! ) 4! = 3! X 4 = T x 4 = 24 ( store the temporary result in T, T is 4! ) 5! = 4! X 5 = T x 5 = 120 ( final result )

COMP102 Prog Fundamentals I: while Loops/Slide 8 N! int number, factorial, counter; cout > number; factorial = 1; counter = 1; while(counter <= number){ factorial *= counter; counter += 1; //counter = counter + 1; } cout << "The factorial of " << number << " is " << factorial << endl;

COMP102 Prog Fundamentals I: while Loops/Slide 9 Compute 2 N 2 n - raise 2 to the nth power. 2 n = 2 n-1 x 2 Example: 2 0 = = 2 x 2 = = 2 x 2 x 2 x 2 = 16 How to compute 2 4 ? 2 0 = 1 ( store the temporary result in T, T is 2 0 ) 2 1 = 2 0 x 2 = T x 2 = 2 ( store the temporary result in T, T is 2 1 ) 2 2 = 2 1 X 2 = T x 2 = 4 ( store the temporary result in T, T is 2 2 ) 2 3 = 2 2 X 2 = T x 2 = 8 ( store the temporary result in T, T is 2 3 ) 2 4 = 2 3 X 2 = T x 2 = 16 ( final result )

COMP102 Prog Fundamentals I: while Loops/Slide 10 2N2N int number, result, counter; cout > number; result = 1; counter = 1; while(counter <= number){ result *= 2; // result = result * 2 counter += 1; } cout << "Two raised to the " << number << " power is " << result << endl;

COMP102 Prog Fundamentals I: while Loops/Slide 11 Find Maximum Value int value=0;// input value int max=0;// maximum value while(value!=-1){ cout << "Enter a positive integer” > value; if(value > max) max = value; } cout << "The maximum value found is " << max << endl;

//find the average of a list of integers #include using namespace std; int main() { int listSize = 0; int value; double sum = 0; double average; cout << "Provide a list of numbers (-1 to stop) " << endl; cin >> value; while (value != -1) { sum += value; listSize += 1; cin >> value; } if(listSize > 0){ average = sum / listSize; cout << "Average: " << average << endl; } else cout << "No list to average" << endl; return 0; }

COMP102 Prog Fundamentals I: while Loops/Slide 13 Paper, Scissors, Rock "If I'm going to play a sport, I want one that provides more exercise, such as "Paper, Scissors, Rock." - Dave Barry, SCMP, 4/4/98