AEEE 195 – Repetition Structures: Part B Spring semester 2011.

Slides:



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

Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Introduction to Computers and Programming Lecture 7:
Additional control structures. The if-else statement The if-else statement chooses which of two statements to execute The if-else statement has the form:
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
Introduction to Computers and Programming Class 9 Introduction to C Professor Avi Rosenfeld.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
do - while  while: Execute the body of the loop at least once
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 6 – Car Payment Calculator Application: Introducing.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
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.
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.
LOOPS In the Name of Allah The Most Merciful The Most Compassionate LOOPS
1. Agenda for loop Short-handed notation How to use break and continue 2.
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,
© 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.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
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.
Introduction to Computers and Programming Lecture 7:
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
PGT C Programming1 Week 4 – Repetition Structures / Loops.
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 5: Layers of Control. Nested while Loops Problem Multiplying two numbers and outputting the result only if they are both less than 5. (i.e. Start.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
LOOPS IN ‘C’ PROGRAMMING. V ERY O FTEN, Y OU W ILL W ANT TO D O S OMETHING M ORE T HAN O NCE HA.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Chapter 7 JavaScript: Control Statements, Part 1
Chapter 4 – C Program Control
EKT120 COMPUTER PROGRAMMING
Lecture 7: Repeating a Known Number of Times
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Chapter 5: Loops and Files.
Learning About the Loop Structure (continued)
Arrays, For loop While loop Do while loop
MSIS 655 Advanced Business Applications Programming
2008/10/27: Lecture 13 CMSC 104, Section 0101 John Y. Park
Chapter 4 - Program Control
3 Control Statements:.
2008/10/27: Lecture 13 CMSC 104, Section 0101 John Y. Park
Assignment Operators Topics Increment and Decrement Operators
Chapter 4 - Program Control
PROGRAM FLOWCHART Iteration Statements.
Chap 7. Advanced Control Statements in Java
Lec 6 Loop Statements Introduction to Computer Programming
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Presentation transcript:

AEEE 195 – Repetition Structures: Part B Spring semester 2011

2 Outline Repetition structures definition “while” loop “do/while” loop Inifite loop Arithmetic operators Examples

3 Repetition Structures Are structures that allow you to repeat execution of the same code That is, you can execute particular statements more than once in a controlled fashion Statements are executed as long as some condition remains true Also known as “loops” Sometimes referred to as “iterations”

4 “ while ” Repetition Structure Actions will be performed as long as some condition is true For example (pseudocode), while (something is true)‏ { do this (again and again...)‏ } Can have various kinds of conditions tested

5 Parts of a While Loop Every while loop will always contain three main elements:  Priming: initialize your variables.  Testing: test against some known condition.  Updating: update the variable that is tested.

6 Simple While Loop #include #define MAX 10 main ()‏ { int index =1; while (index <= MAX) { printf ("Index: %d\n", index); index = index + 1; } OUTPUT: Index: 1 Index: 2 Index: 3 Index: 4 Index: 5 Index: 6 Index: 7 Index: 8 Index: 9 Index: Priming 2. Test Condition 3. Update

7 While Loop Flowchart 1. Priming Set index=1 2. Test index <= 10 3a. print index 3b. Update index = index + 1; TRUE FALSE next iteration

8 Infinite Loop Infinite Loop: A loop that never ends.  Generally, you want to avoid these!  There are special cases, however, when you do want to create infinite loops on purpose.

9 Infinite Loop: Example #1 #include #define MAX 10 main ()‏ { int index =1; while (index <= MAX) { printf ("Index: %d\n", index); } Here, part 3 is deleted : the index = index + 1 statement. Index: 1 … [forever]

10 Infinite Loop: Example #2 #include /*no MAX here*/ main ()‏ { int index = 1; while (index > 0) { printf ("Index: %d\n", index); index = index + 1; } Here, part 2 has changed : the test condition. Index: 1 Index: 2 Index: 3 Index: 4 Index: 5 … [forever]

11 “ do/while ” Repetition Structure Similar to the while structure But instead of testing the condition at the top, test is at the bottom of the loop Used when you want to execute the loop AT LEAST ONCE

12 do/while pseudocode do { something } while (condition is true) ; Note the semicolon at the end

13 do/while example int iCounter = 1; do { printf( “%d “, iCounter ); iCounter = iCounter + 1; } while ( iCounter <= 10 ) ;

14 do/while loop Flowchart 1. Priming Set index=1; 3. Test index <= 10 2a. print index 2b. Update index = index + 1; TRUE FALSE

15 Example: while vs do-while structures int num; printf(“Give a positive number: ); scanf(“%d”, &num); while(num <=0)‏ { printf(“Give a positive number: ); scanf(“%d”, &num); } printf(“The number is %d\n”, num); int num; do { printf(“Give a positive number: ); scanf(“%d”, &num); } while(num <=0); printf(“The number is %d\n”, num);

16 Shortcuts C provides abbreviations for some common operations  Assignment operators  Increment/Decrement operators

17 Assignment Operators Abbreviations are provided for the basic binary operations  Addition  Subtraction  Multiplication  Division  Modulus (%)‏

18 Addition Assignment Operator Instead of writing iCount = iCount + 1; You can write iCount += 1 ; Adds the value of the expression on the right to the variable on the left and stores the new total in the variable on the left

19 Subtraction Assignment Operator Instead of writing iCount = iCount - 1; You can write iCount -= 1 ; Subtracts the value of the expression on the right from the variable on the left and stores the new total in the variable on the left

20 Multiplication Assignment Operator Instead of writing iCount = iCount * 2; You can write iCount *= 2; Multiplies the variable on the left by the value of the expression on the right and stores the new total in the variable on the left

21 Division Assignment Operator Instead of writing iCount = iCount / 2; You can write iCount /= 2; Divides the variable on the left by the value of the expression on the right and stores the new total in the variable on the left

22 Modulus Assignment Operator Instead of writing iSeconds = iSeconds % 60; You can write iSeconds %= 60; Takes the remainder of dividing the variable on the left by the value of the expression on the right, and stores the new total in the variable on the left

23 Examples Assume variable int c = 10; 3 to cc = c % 7;c %= 7;10%= 2 to cc = c / 5;c /= 5;10/= 60 to cc = c * 6;c *= 6;10*= 7 to cc = c – 3;c -= 3;10-= 17 to cc = c + 7;c += 7;10+= AssignsMeaningSample Initial Value Operator

24 Increment and Decrement Operators C provides unary increment operators ++ and decrement operators –- (no spaces between them)‏ Increment operators add 1 Decrement operators subtract 1 Not for other assignment operators (*, /, %)‏

25 Increment Operator ++ Instead of writing iCount = iCount + 1; or iCount += 1; You can write iCount++ ; ( post-increment ) or ++iCount ; ( pre-increment)‏

26 Post-increment The ++ operator is after the variable Causes the initial value of the variable to be used in the expression where it appears, and THEN adds the 1 to the variable For example, int iCount = 5; printf( “%d\n”, iCount++ ); Would print 5… but iCount is incremented to 6 after the statement

27 Pre-increment The ++ operator is before the variable Adds 1 to the initial value of the variable BEFORE it is used in the expression where it appears For example, int iCount = 5; printf( “%d\n”, ++iCount); Would print 6… and iCount is incremented to 6 after the statement

28 More examples ++ post-increment vs. pre-increment int iTotal = 0; int iCount = 5; iTotal = iCount++ * 2; printf( “%d\n”, iTotal); printf( “%d\n”, iCount); Would print 10 for iTotal and then 6 for iCount iTotal = ++iCount * 2; printf( “%d\n”, iTotal); printf( “%d\n”, iCount); Would print 14 for iTotal and then 7 for iCount

29 Decrement Operator -- Similar to increment in syntax/operation Instead of writing iCount = iCount - 1; or iCount -= 1; You can write iCount-- ; or --iCount ;

30 Post-decrement The -- operator is after the variable Causes the initial value of the variable to be used in the expression where it appears, and THEN subtracts the 1 from the variable For example, int iCount = 5; printf( “%d\n”, iCount-- ); Would print 5… but iCount is decremented to 4 after the statement

31 Pre-decrement The -- operator is before the variable Subtracts 1 from the initial value of the variable BEFORE it is used in the expression where it appears For example, int iCount = 5; printf( “%d\n”, --iCount); Would print 4… and iCount is decremented to 4 after the statement

32 More examples -- post-decrement vs. pre-decrement int iTotal = 0; int iCount = 5; iTotal = iCount-- * 2; printf( “%d\n”, iTotal); printf( “%d\n”, iCount); Would print 10 for iTotal and then 4 for iCount iTotal = --iCount * 2; printf( “%d\n”, iTotal); printf( “%d\n”, iCount); Would print 6 for iTotal and then 3 for iCount

33 Summary Table Subtract 1 from c THEN use new value of c in expression --c-- Use value of c in expression THEN subtract 1 from c c---- Add 1 to c THEN use the new value of c in expression ++c++ Use value of c in expression THEN add 1 to c c++++ ExplanationSampleOperator