1 CS 101 Lecture 4 Repetition. 2 pre and post increment and decrement If i=i+1; we may write i++ to mean the same thing, and is called the postincrement.

Slides:



Advertisements
Similar presentations
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
Advertisements

True or false A variable of type char can hold the value 301. ( F )
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.
CS150 Introduction to Computer Science 1
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
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.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
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.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
1 CS 101 Lecture 2. 2 Input from the Keyboard Here is a program to accept input from the keyboard and put into into two variables. #include main(){ cout.
do - while  while: Execute the body of the loop at least once
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.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
AEEE 195 – Repetition Structures: Part B Spring semester 2011.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Control Structures Repetition or Iteration or Looping Part II.
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.
LOOP & Type Conversion. do – while Loop In the while loop, the test expression is evaluated at the beginning of the loop. If the test condition is false.
Overview Go over parts of quiz? Another iteration structure for loop.
Computer Programming Control Structure
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
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 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
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.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
PGT C Programming1 Week 4 – Repetition Structures / Loops.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
Computer Programming -1-
UCT Department of Computer Science Computer Science 1015F Iteration
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.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Chapter 4: Looping Structures LECTURER : MRS ROHANI HASSAN
REPETITION CONTROL STRUCTURE
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Introduction To Repetition The for loop
Lecture 7: Repeating a Known Number of Times
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
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.
CiS 260: App Dev I Chapter 4: Control Structures II.
Chapter 2.2 Control Structures (Iteration)
Control Statements Kingdom of Saudi Arabia
Lecture 4B More Repetition Richard Gesick
Control Structures - Repetition
TOPIC 4: REPETITION CONTROL STRUCTURE
MSIS 655 Advanced Business Applications Programming
Intro to Programming Week # 4 Control Structure Lecture # 8
- Additional C Statements
Outline Altering flow of control Boolean expressions
3 Control Statements:.
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
Dale Roberts, Lecturer IUPUI
Based on slides created by Bjarne Stroustrup & Tony Gaddis
PROGRAM FLOWCHART Iteration Statements.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Programming Fundamental
Presentation transcript:

1 CS 101 Lecture 4 Repetition

2 pre and post increment and decrement If i=i+1; we may write i++ to mean the same thing, and is called the postincrement operator. ++i also means i=i+1 and is called the preincrement operator. --i and i-- are called the preincrement and postincrement operators and each means i=i-1. The difference shows up in expressions and commands. If i = 4 then cout<<++x<<‘ ‘<<x;will print out 5 5 but cout<<x++<<‘ ‘<<x; will print out 4 5. Similarly, if x=5 and y=3 then cout<<x<<‘ ‘<<y<‘ ‘<<++x*y--<<‘ ‘<<x<<‘ ‘<<y will print to the screen

3 Shortcut assignment and arithmetic operators We can write x+=3; to mean x=x+3, similarly we can write x*=4; x/=5; x-=2; x%=3; if x,y are integer variables, x/y is truncated, i.e., 7/3 is 2. If at least the numerator or denominator of x/y is a double or a float, then the result is an untruncated double or float. 7.0/3 is We can also write double(7)/3 or 7/double(3) to get the same result, but not double(7/3), since this gives 2.0. If x,y are integer variables, x%y gives the remainder when x is divided by y. 7%3 is 1. 13%5 is %5 is –3. In math, -13%5 is 2.

4 The while loop If i is an integer variable containing 0, then the statement while(i<5) {cout<<i*i<<‘ ‘;i++;} will print to the screen. The pair of statements cout<<i*i<<‘ ‘; i++; gets repeated 5 times, each time with a different value of i. The five values of i are 0,1,2,3,4. Before the pair of statements is executed, the test i<5 is performed. If it is true, then both statements are executed. If not(as when 5<5 is false), then the next statement after the while loop is executed.

5 while loop program While loops are usually used when it is not known in advance how many times the loop will be executed. The following program #include main( ){ double sum=0; int i=1; while(1.0/(2*i*i+2*i+1)>.0001){sum=sum+1.0/(2*i*i+2*i +1);i++} cout<<sum<<endl;} will execute 70 times and print to the screen. We have avoided solving the equation 2i 2 + 2i +1 = to find out in advance how many times we must loop.

6 for loops There are three parts to the for loop statement. for(int i=1;i<5;i++)cout<<i<<‘ ‘; int i=1; is called the initialization command, and is done first and only once. If there are several commands before the first ; then they must be separated by commas. i<5 is called the test expression. If it is true then the body of the for loop, in this case, the statement cout<<i<<‘ ‘; is performed. If the body is performed then the final statement(s) is(are) carried out after the body. Then the test expression is evaluated again. The above for statement will print to the screen. The above for statement is equivalent to for(int i=1;i<5;cout<<i<<‘ ‘,i++){} and also to for(int i=1;i<5;){cout<<i<<‘ ‘;i++;} and also to for(int i=1; ;){if(i<5)cout<<i<<‘ ‘;else break;}

7 break and continue If a break statement occurs in the body of a loop, the loop is exited. If a continue statement occurs in the body of a loop, the next iteration is performed. If the loop is a for loop then after the continue, the update portion of the for loop is executed.

8 Quiz 2 and solution Write a program which will ask the user to input 50 numbers(doubles), and print the average to a file called average.txt #include main(){ cout >x; sum=sum+x;} ofstream fout(“average.txt”); fout<<sum/50; fout.close();

9 loops with a conditional Suppose we want to read 50 integers from the file input.txt and write the even integers to the file oute.txt and the odd integers to the file outo.txt. #include main(){ ifstream fin(“input.txt”); ofstream foute(“oute.txt”),fouto(“outo.txt”); int x; for(int i=0;i >x;if(x%2==1)fouto<<x<<‘ ‘;else foute<<x<<‘ ‘;} fin.close();fouto.close();foute.close();}

10 Quiz 3 and solution Write a program which will ask the user to input 50 numbers(doubles), and print the numbers to three different files. If the number is divisible by 3, the number is printed to the file three0.txt; if dividing the number by 3 yields a remainder of 1, the number is printed to the file three1.txt; if dividing the number by 3 yields a remainder of 2, the number is printed to the file three2.txt #include main(){int x; ofstream out0(“three0.txt”),fout1(“three1.txt”),fout2(“three2.txt”); cout<<“Please enter 50 integrs.\n”; for(int i=0;i >x; if(x%3==0)fout0<<x<<‘ ‘; else if(x%3==1)fout1<<x<<‘ ‘; else fout2<<x<<‘ ‘;} fout1.close(); fout1.close(); fout2.close();}