For Loop Lesson 1 CS1313 Spring 2015 1 for Loop Lesson 1 Outline 1. for Loop Lesson 1 Outline 2.A while Loop That Counts #1 3.A while Loop That Counts.

Slides:



Advertisements
Similar presentations
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Advertisements

CS0007: Introduction to Computer Programming
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Arithmetic Expressions Lesson #1 CS1313 Spring Arithmetic Expressions Lesson #1 Outline 1.Arithmetic Expressions Lesson #1 Outline 2.A Less Simple.
While Loop Lesson CS1313 Spring while Loop Outline 1.while Loop Outline 2.while Loop Example #1 3.while Loop Example #2 4.Repetition and Looping.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
CS150 Introduction to Computer Science 1
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
CS Data Structures Appendix 1 How to transfer a simple loop- expression to a recursive function (factorial calculation)
Some loop programs 1: Read in 10 integers and output their sum
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
Week 11 Recap CSE 115 Spring Want to write a programming language? You’ll need three things: You’ll need three things: –Sequencing –Selection Polymorphism.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
For Loop Lesson 3 CS1313 Spring for Loop Lesson 3 Outline 1. for Loop Lesson 3 Outline 2. for Loop with a float Counter: BAD! 3. float Counter Example.
For Loop Lesson 2 CS1313 Spring for Loop Lesson 2 Outline 1. for Loop Lesson 2 Outline 2. for Loop Application 3. Factorial 4. Factorial Program.
Characters & Strings Lesson 2 CS1313 Spring Characters & Strings Lesson 2 Outline 1.Characters & Strings Lesson 2 Outline 2.Character String Declaration.
Array Lesson 1 CS1313 Spring Array Lesson 1 Outline 1.Array Lesson 1 Outline 2.Mean of a List of Numbers 3.mean: Declarations 4.mean: Greeting,
Array Lesson 2 CS1313 Spring Array Lesson 2 Outline 1.Array Lesson 2 Outline 2.Reading Array Values Using for Loop #1 3.Reading Array Values Using.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
AEEE 195 – Repetition Structures: Part B Spring semester 2011.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
User Defined Functions Lesson 1 CS1313 Spring User Defined Functions 1 Outline 1.User Defined Functions 1 Outline 2.Standard Library Not Enough.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
BY ILTAF MEHDI (MCS, MCSE, CCNA)1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI (MCS, MCSE, CCNA)2 Chapter No: 04 “Loops”
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
If Lesson 1 CS1313 Fall if Lesson 1 Outline 1.if Lesson 1 Outline 2.Absolute Value 3.Absolute Value Definition 4.Absolute Value Implementation 5.What.
Idiotproofing Lesson CS1313 Spring Idiotproofing Outline 1.Idiotproofing Outline 2.Idiotproofing 3.Idiotproofing Quotes 4.Idiotproofing Example.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
1 CSC103: Introduction to Computer and Programming Lecture No 9.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance.
Flow Control. Comments u Comments: /* This is a comment */ –Use them! –Comments should explain: v special cases v the use of functions (parameters, return.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
Chapter 4 – C Program Control
CSE 220 – C Programming Loops.
Operators And Expressions
Lecture 7: Repeating a Known Number of Times
for Loop 1 Outline for Loop 1 Outline for Loop
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Arrays, For loop While loop Do while loop
while Loop Outline while Loop Outline while Loop Example #1
- Additional C Statements
Lesson #5 Repetition and Loops.
Chapter 4 - Program Control
for Loop Lesson 3 Outline
3 Control Statements:.
Week 6 CPS125.
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
Chapter 4 - Program Control
PROGRAM FLOWCHART Iteration Statements.
More Loops Topics Counter-Controlled (Definite) Repetition
Arithmetic Expressions Lesson #1 Outline
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

for Loop Lesson 1 CS1313 Spring for Loop Lesson 1 Outline 1. for Loop Lesson 1 Outline 2.A while Loop That Counts #1 3.A while Loop That Counts #2 4.A while Loop That Counts #3 5.A while Loop That Counts #4 6.Count-Controlled Loops #1 7.Count-Controlled Loops #2 8.Count-Controlled Loop Flowchart 9.Arithmetic Assignment Operators #1 10.Arithmetic Assignment Operators #2 11.Jargon: Syntactic Sugar 12.Increment & Decrement Operators #1 13.Increment & Decrement Operators #2 14.Increment & Decrement Operators #3 15.Increment & Decrement Operators #4 16. for Loop 17. for Loop vs while Loop 18. for Loop Flowchart 19. Three Programs That Behave the Same #1 20. Three Programs That Behave the Same #2 21. Three Programs That Behave the Same #3 22. Three Programs That Behave the Same #4 23. for Loop Example 24. for Loop Behavior #1 25. for Loop Behavior #2 26. for Loop Behavior #3 27. for Loop Behavior #4 28. for Loop Behavior #5 29. for Loop Behavior #6

for Loop Lesson 1 CS1313 Spring A while Loop That Counts #1 #include int main () { /* main */ const int initial_sum = 0; const int increment = 1; const int program_success_code = 0; const int program_failure_code = -1; int initial_value, final_value; int count; int sum;

for Loop Lesson 1 CS1313 Spring A while Loop That Counts #2 printf("What value would you like to "); printf("start counting at?\n"); scanf("%d", &initial_value); printf("What value would you like to "); printf("stop counting at,\n"); printf(" which must be greater than "); printf("or equal to %d?\n", initial_value); scanf("%d", &final_value); if (final_value < initial_value) { printf("ERROR: the final value %d is less\n", final_value); printf(" than the initial value %d.\n", initial_value); exit(program_failure_code); } /* if (final_value < initial_value) */

for Loop Lesson 1 CS1313 Spring A while Loop That Counts #3 sum = initial_sum; count = initial_value; while (count <= final_value) { sum = sum + count; count = count + increment; } /* while (count <= final_value) */ printf("The sum of the integers from"); printf(" %d through %d is %d.\n", initial_value, final_value, sum); return program_success_code; } /* main */

for Loop Lesson 1 CS1313 Spring A while Loop That Counts #4 % gcc -o whilecount whilecount.c % whilecount What value would you like to start counting at? 1 What value would you like to stop counting at, which must be greater than or equal to 1? 0 ERROR: the final value 0 is less than the initial value 1. % whilecount What value would you like to start counting at? 1 What value would you like to stop counting at, which must be greater than or equal to 1? 5 The sum of the integers from 1 through 5 is 15.

for Loop Lesson 1 CS1313 Spring Count-Controlled Loops #1 On the previous slide, we saw a case of a loop that: executes a specific number of iterations, by using a counter variable, which is initialized to a particular initial value and is incremented (increased by 1) at the end of each iteration of the loop, until it goes beyond a particular final value: sum = initial_sum; count = initial_value; while (count <= final_value) { sum = sum + count; count = count + increment; } /* while (count <= final_value) */

for Loop Lesson 1 CS1313 Spring Count-Controlled Loops #2 sum = initial_sum; count = initial_value; while (count <= final_value) { sum = sum + count; count = count + increment; } /* while (count <= final_value) */ We call this kind of loop a count-controlled loop. If we express a count-controlled loop as a while loop, then the general form is: counter = initial_value ; while ( counter <= final value ) { statement1 ; statement2 ;... counter = counter + 1; } /* while ( counter <= final value ) */

for Loop Lesson 1 CS1313 Spring Count-Controlled Loop Flowchart counter = initial_value ; while ( counter <= final value ) { statement1 ; statement2 ;... counter = counter + 1; } /* while ( counter <= final value ) */ statement_after ;

for Loop Lesson 1 CS1313 Spring Arithmetic Assignment Operators #1 Some while back, we saw the following: x = x + y; We learned that this statement increases the value of x by y. That is, the statement takes the old value of x, adds y to it, then assigns the result of this addition to x. This kind of statement is so common that the C language has a special operator for it, called the addition assignment operator: x += y; Note that the two statements above behave identically.

for Loop Lesson 1 CS1313 Spring Arithmetic Assignment Operators #2 This:Is identical to this:Operation Name x += y;x = x + y; Addition assignment x -= y;x = x – y; Subtraction assignment x *= y;x = x * y; Multiplication assignment x /= y;x = x / y; Division assignment x %= y;x = x % y; Remainder assignment ( int only) C also has arithmetic assignment operators for the other arithmetic operations:

for Loop Lesson 1 CS1313 Spring Jargon: Syntactic Sugar Syntactic sugar is a programming language construct that doesn’t add any new capability to the language, but makes the language a bit easier to use. Arithmetic assignment operations are syntactic sugar.

for Loop Lesson 1 CS1313 Spring Increment & Decrement Operators #1 One of the most common addition assignments is: x = x + 1; We learned that this statement increases the value of x by 1. That is, the statement takes the old value of x, adds 1 to it, then assigns the resulting sum to x. For this statement, we could use the addition assignment operator: x += 1;

for Loop Lesson 1 CS1313 Spring Increment & Decrement Operators #2 x = x + 1; For this statement, we could use the addition assignment operator: x += 1; But this statement is MUCH more common than x += y; for generic y, so the C language has another special operator, called the increment operator: x++; (This is also known as the autoincrement operator.)

for Loop Lesson 1 CS1313 Spring Increment & Decrement Operators #3 x = x + 1; x += 1; Increment operator: x++; Also: x = x – 1; x -= 1; x--; This is known as the decrement operator (and also as the autodecrement operator).

for Loop Lesson 1 CS1313 Spring Increment & Decrement Operators #4 This:is identical to this: Name x++;x += 1;x = x + 1; Increment x--;x -= 1;x = x – 1; Decrement Note that the increment and decrement operators are syntactic sugar, just like the arithmetic assignment operators.

for Loop Lesson 1 CS1313 Spring for Loop A for loop has this form: for ( counter = initial_value ; counter <= final_value ; counter ++) { statement1 ; statement2 ;... } /* for counter */

for Loop Lesson 1 CS1313 Spring for Loop vs while Loop A for loop has this form: for ( counter = initial_value ; counter <= final_value ; counter ++) { statement1 ; statement2 ;... } /* for counter */ A for loop behaves exactly the same as a count-controlled while loop: counter = initial_value ; while ( counter <= final value ) { statement1 ; statement2 ;... counter = counter + 1; } /* while ( counter <= final value ) */

for Loop Lesson 1 CS1313 Spring for Loop Flowchart for ( counter = initial_value ; counter <= final value ; counter ++) { statement1 ; statement2 ;... } /* for counter */ statement_after ; Notice that the for loop flowchart is identical to the while loop flowchart on slide 8.

for Loop Lesson 1 CS1313 Spring Three Programs That Behave the Same #1 #include int main () { /* main */ int count; int sum; sum = 0; count = 1; sum = sum + count; count = count + 1; sum = sum + count; count = count + 1; sum = sum + count; count = count + 1; sum = sum + count; count = count + 1; sum = sum + count; count = count + 1; printf("count = %d\n", count); printf("sum = %d\n", sum); return 0; } /* main */

for Loop Lesson 1 CS1313 Spring Three Programs That Behave the Same #2 #include int main () { /* main */ int count; int sum; sum = 0; count = 1; while (count <= 5) { sum = sum + count; count += 1; } /* while (count <= 5) */ printf("count = %d\n", count); printf("sum = %d\n", sum); return 0; } /* main */

for Loop Lesson 1 CS1313 Spring Three Programs That Behave the Same #3 # include int main () { /* main */ int count; int sum; sum = 0; for (count = 1; count <= 5; count++) { sum = sum + count; } /* for count */ printf("count = %d\n", count); printf("sum = %d\n", sum); return 0; } /* main */

for Loop Lesson 1 CS1313 Spring Three Programs That Behave the Same #4 % gcc -o manycountstmts manycountstmts.c % manycountstmts count = 6 sum = 15 % gcc -o while_loop while_loop.c % while_loop count = 6 sum = 15 % gcc -o for_loop for_loop.c % for_loop count = 6 sum = 15

for Loop Lesson 1 CS1313 Spring for Loop Example % cat product_loop.c #include int main () { /* main */ int count; int product; product = 1; for (count = 1; count <= 5; count++) { product = product * count; } /* for count */ printf("After the loop: count = %d, ", count); printf("product = %d\n", product); return 0; } /* main */ % gcc -o product_loop product_loop.c % product_loop After the loop: count = 6, product = 120

for Loop Lesson 1 CS1313 Spring for Loop Behavior #1 for (count = 1; count <= 5; count++) { product = product * count; } /* for count */ 1.The loop initialization is performed; typically, the loop control variable (sometimes called the loop counter or the loop index) is assigned an initial value (sometimes called a lower bound). We refer to each trip through the body of the loop as an iteration.

for Loop Lesson 1 CS1313 Spring for Loop Behavior #2 for (count = 1; count <= 5; count++) { product = product * count; } /* for count */ 2.The loop continuation condition is evaluated, and if the loop continuation condition evaluates to false ( 0 ), then the for loop body is skipped, and the program continues on from the first statement after the for loop’s block close. (But if the loop continuation condition evaluates to true ( 1 ), then enter the loop body.) We refer to each trip through the body of the loop as an iteration.

for Loop Lesson 1 CS1313 Spring for Loop Behavior #3 for (count = 1; count <= 5; count++) { product = product * count; } /* for count */ 3.Each statement inside the loop body is executed in sequence. We refer to each trip through the body of the loop as an iteration.

for Loop Lesson 1 CS1313 Spring for Loop Behavior #4 for (count = 1; count <= 5; count++) { product = product * count; } /* for count */ 4.When the end of the loop body is reached (indicated by the block close associated with the block open of the for statement), the loop counter is changed by the loop change statement, typically (though not always) by incrementing. We refer to each trip through the body of the loop as an iteration.

for Loop Lesson 1 CS1313 Spring for Loop Behavior #5 for (count = 1; count <= 5; count++) { product = product * count; } /* for count */ 5.The program jumps back up to step 2, evaluating the condition again. We refer to each trip through the body of the loop as an iteration.

for Loop Lesson 1 CS1313 Spring for Loop Behavior #6 int product = 1; int count; for (count = 1; count <= 5; count++) { product = product * count; } /* for count */ The above program fragment behaves identically the same as: /* Program Trace */ int product = 1; /* product = 1 */ int count; /* count is undefined */ count = 1; /* count == 1, product == 1 */ product *= count; /* count == 1, product == 1 */ count++; /* count == 2, product == 1 */ product *= count; /* count == 2, product == 2 */ count++; /* count == 3, product == 3 */ product *= count; /* count == 3, product == 6 */ count++; /* count == 4, product == 6 */ product *= count; /* count == 4, product == 24 */ count++; /* count == 5, product == 24 */ product *= count; /* count == 5, product == 120 */ count++; /* count == 6, product == 120 */