Thursday, December 21, 2006 The Briggs/Chase Law of Program Development: To determine how long it will take to write and debug a program, take your best.

Slides:



Advertisements
Similar presentations
Write a function to calculate the cubic function: y = 4x 3 + 2x 2 –5x – 4 The function should return y for any given value of x. Question One #include.
Advertisements

For loops For loops are controlled by a counter variable. for( c =init_value;c
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Computer Science 1620 Loops.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
Saturday, December 16, 2006 “The whole of the development and operation of analysis are now capable of being executed by machinery… As soon as an Analytical.
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.
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 192 Lecture 9 Winter 2003 December 19, 2003 Dr. Shafay Shamail.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
לולאות 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי 15 1 Department of Computer Science-BGU.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
1 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions.
CHAPTER 5 CONTROL STRUCTURES II (Repetition). In this chapter, you will:  Learn about repetition (looping) control structures  Explore how to construct.
Lecture 4 Loops.
1 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
Chapter 5: Control Structures II (Repetition)
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
True or False: Boolean Expression Boolean expression are expressions which evaluate to "true" or "false“ Primary operators to combine expressions: && (and),
Chapter 4: Looping. Resource: Starting Out with C++, Third Edition, Tony Gaddis 5.1 The Increment and Decrement Operators ++ and -- are operators that.
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=
do - while  while: Execute the body of the loop at least once
CONTROLLING PROGRAM FLOW
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 5: Control Structures II (Repetition)
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: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
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,
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Chapter 05 (Part III) Control Statements: Part II.
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
Program Looping Why we need loops in our code –Make code concise for repetitive processes When to use loops –Run a block of code repetitively –Process.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: The while Statement cin within a while Loop The for.
A First Book of C++ Chapter 5 Repetition.
Statements
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Chapter 5 Repetition. 2 Objectives You should be able to describe: The while Statement cin within a while Loop The for Statement The do Statement Common.
We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration statement.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Computer Programming -1-
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
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.
REPETITION CONTROL STRUCTURE
Intro to Programming Week # 6 Repetition Structure Lecture # 10
Programming Fundamentals
Looping.
Increment/Decrement Unary operators require only one operand
Introduction to Programming
Chapter 2.2 Control Structures (Iteration)
Objectives You should be able to describe: The while Statement
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Programming Fundamental
Programming Fundamental
Presentation transcript:

Thursday, December 21, 2006 The Briggs/Chase Law of Program Development: To determine how long it will take to write and debug a program, take your best estimate, multiply that by two, add one, and convert to the next higher units To determine how long it will take to write and debug a program, take your best estimate, multiply that by two, add one, and convert to the next higher units.

§Friday classes §Assignment 1 on website §Late Assignment Policy §Quiz 1 has been marked

Fibonacci numbers F1=1 F2=2 Fn=Fn-1 + Fn …

int i; double Fn, Fnminus2=1, Fnminus1=2; for (i=0; i<100; i++){ Fn=Fnminus1 + Fnminus2; cout<<Fn<<" "; //updating values below Fnminus2=Fnminus1; Fnminus1=Fn; }

The for loop int main() { int x; for(x=0; x != 123; ) { cout << "Enter a number: "; cin >> x; } return 0; }

The for loop int x = 0; for( ; x<10; ) { cout << x << " " ; ++x; } int x; for (x=0; x<10; x++) { cout << x << " " ; }

for loop Self test exercise: Write a function to calculate the factorial of a number. 4! = 24 5! = 120 6! = 720 7! =5040 8!=40320

Print 30 stars on the screen ******************************

Print 30 stars on the screen int i; for (i=0; i<30; i++) { cout<<"*"; } cout<<endl;

Print 20 lines of 30 stars on screen ******************************

Print 20 lines of 30 stars on screen int i,j; for (j=0; j<20; j++) { for(i=0; i<30; i++) { cout<<"*"; } cout<<endl; } cout<<endl;

Nested Loops int main() { int i, j; for(i=0; i<3; i++) { for(j=0; j <= 4; j++) cout<<“The value of i=” <<i<<“ and j= ”<<j<<“\n”; cout<<"Out of the inner for-loop\n"; } return 0; }

Nested Loops The value of i=0 and j=0 The value of i=0 and j=1 The value of i=0 and j=2 The value of i=0 and j=3 The value of i=0 and j=4 Out of the inner for-loop The value of i=1 and j=0 The value of i=1 and j=1 The value of i=1 and j=2 The value of i=1 and j=3 The value of i=1 and j=4 Out of the inner for-loop The value of i=2 and j=0 The value of i=2 and j=1 The value of i=2 and j=2 The value of i=2 and j=3 The value of i=2 and j=4 Out of the inner for-loop

Print this pattern on screen * ** *** **** ***** ****** ******* ******** ********* ********** *********** ************ ************* ************** *************** **************** ***************** ****************** ******************* ********************

Print this pattern on screen int i,j; for (j=0; j<20; j++) { for(i=0; i<(j+1); i++) { cout<<"*"; } cout<<endl; } cout<<endl;

SELF TEST: Print this pattern on screen ******************** ******************* ****************** ***************** **************** *************** ************** ************* ************ *********** ********** ********* ******** ******* ****** ***** **** *** ** *

Do-while loops  Do-while loops are very much like while loops, except  that the loop body is executed once before the loop condition is ever evaluated.  After that, they are exactly the same.

Do-while loops  Example: do { cout << "This is a test"; } while (0); –Compare this to: while (0) { cout << "This is a test"; }

char ans; do { cout << "Hello\n"; cout << "Do you want another greeting?\n" << "Press y for yes, n for no,\n" << "and then press return: "; cin >> ans; } while (ans == 'y' || ans == 'Y'); cout << "Good-Bye\n";

int secret=3, guess; char answer; do { cout<<"Guess a number between 0 and 10 "; cin>>guess; if (guess < secret) cout<<"Sorry, Too Small!"<<endl; else if (guess > secret) cout<<"Sorry, Too Big!"<<endl; else cout<<"Congratulations! Your guess is right!"<<endl; cout<<"Do you want to play again?\n"; cin>>answer; }while(answer=='y' || answer=='Y');

int secret=3, guess; char answer; do { cout<<"Guess a number between 0 and 10 "; cin>>guess; while((guess 10)) { cout<<"Out of range. Enter number between 0 and 10"; cin>>guess; } if (guess < 3) cout<<"Sorry, Too Small!"<<endl; else if (guess > 3) cout<<"Sorry, Too Big!"<<endl; else cout<<"Congratulations! Your guess is right!"<<endl; cout<<"Do you want to play again?\n"; cin>>answer; }while(answer=='y' || answer=='Y');

continue statement int i; for (i=0; i<50; i++){ if ((i==4)||(i==42)) continue; cout<<i<<"\n"; } In for-loop a continue statement will cause the increment part to be performed and then conditional expression to be executed.

break statement int i, j=15; for (i=0; i<50; i++){ if((i*2)==(j+3)) break; else { cout<<i<<" "; }

break statement int i, j=15; for (i=0; i<50; i++){ if((i*2)==(j+3)) break; else { cout<<i<<" "; } //prints

continue int main() { int x; for(x=0; x<=10; x++) { if(x%2) continue; cout << x << " "; } return 0; } //output?

continue int main() { int x; for(x=0; x<=10; x++) { if(x%2) continue; cout << x << " " ; } return 0; } //output?

Self Test: continue int loop ; for( loop=2; loop<10; ++loop ) {if (loop%3==0) continue; cout << loop << " "; } cout<<loop<<endl; int loop ; for( loop=2; loop<10; ++loop ) {if (loop%3) continue; cout << loop << " "; } cout<<loop<<endl;

Self Test: continue What happens here? int i, s=0 ; for( i=1; i<5; ) { continue; cout<<i<<endl; }

Self Test: continue int i=0; while (i<10){ i++; if (i==5) continue; cout<<i; }

Self Test: break break forces immediate exit from loop int main() { int t; // what is the output? for (t=0; t<100; t++) { if(t==10) break; cout << t << “ “; } return 0; }

break int i; for( i=1; ; ++i ) {if (i==5) continue; if (i%9 ==0) break; cout<<i; } cout<<"\n"<<i;

break int i; for( i=1; ; ++i ) {if (i==5) continue; if (i%9 ==0) break; cout<<i; } cout<<"\n"<<i; //prints

§continue and break inside loops only.

char a,b; cin>>a>>b; cout<<a<<b<<endl; cout<<a<<" "<<b<<endl;