While loop statement condition list

Slides:



Advertisements
Similar presentations
Reasoning About Code; Hoare Logic, continued
Advertisements

C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
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.
General Computer Science for Engineers CISC 106 Lecture 28 Dr. John Cavazos Computer and Information Sciences 04/29/2009.
Computer Science 1620 Loops.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Lecture 071 CS 192 Lecture 7 Winter 2003 December 15-16, 2003 Dr. Shafay Shamail.
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.
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
Java Syntax Primitive data types Operators Control statements.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.12Recursion 3.13Example Using Recursion: The Fibonacci Series 3.14Recursion.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 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.
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
1 LoopsBranching Condition Statement list T F Condition Statement list T F.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
Input & Output: Console
Do … while ( continue_cond ) Syntax: do { stuff you want to happen in the loop } while (continue_condition);
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
Prime numbers Jordi Cortadella Department of Computer Science.
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Functions Why we use functions C library functions Creating our own functions.
C++ Tutorial Hany Samuel and Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2006 by Douglas.
Semester Review. As we have discussed, Friday we will have in class time for you to work on a program, this program will come with instructions and you.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
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.
CSC 107 – Programming For Science. The Week’s Goal.
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
Chapter 05 (Part III) Control Statements: Part II.
For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.
CS31: Introduction to Computer Science I Discussion 1A 4/16/2010 Sungwon Yang
1/30/2016IT 2751 Operators Arithmetic operators: + - / * % Relational operators: == > = != Logical operators: || && !
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
2/19/2016IT 279, Chung-Chih Li1 Branching Condition Statement list 1 T F Statement list 2 Condition Statement list T F.
Review. Problem 1 What is the output of the following piece of code void increment(int x) { x++; } int main() { int y = 10; increment(y); cout
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Functions Jordi Cortadella Department of Computer Science.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Chapter Topics The Basics of a C++ Program Data Types
while Repetition Structure
Bill Tucker Austin Community College COSC 1315
Basic Elements of C++.
5. Function (2) and Exercises
For loops, nested loops and scopes
Repetition Structures (Loops)
Basic Elements of C++ Chapter 2.
Operators and Expressions
Introduction to Programming
Unary Operators ++ and --
Counting Loops.
Loops.
CS 101 First Exam Review.
Chapter 18 Recursion.
Recursion Method calling itself (circular definition)
Names of variables, functions, classes
Main() { int fact; fact = Factorial(4); } main fact.
REPETITION Why Repetition?
Presentation transcript:

While loop statement condition list while (condition) { statement_list } F How do we know the condition will eventually become false?

Print a string backwards the index of the letters in string starts from 0 the length of string 6 int i; string s; ...... i = s.length()-1; while (i >= 0) { cout << s.substr(i,1); i -= 1; } the index of the last letter is 6-1 print letter no. 5, g print letter no. 4, n print letter no. 3, i print letter no. 2, r print letter no. 1, t print letter no. 0, s no letter left

Return a reversed string One more thing we need to know is how to construct a string + as string concatenation operator. string reverse(string s) { int i; string rev=""; i = s.length()-1; while (i >= 0) rev = rev + s.substr(i,1); i -= 1; } return rev;

Factorial Wrong!! n! = n  (n-1)  (n-2)  (n-3)  ......  1 int factorial(int n) { int fact,i; if (n == 0) return 1; fact = i = n; while (i != 0) fact *= i; i -= 1; } return fact; Wrong!!

Factorial Wrong!! n! = n  (n-1)  (n-2)  (n-3)  ......  1 int factorial(int n) { int fact,i; if (n == 0) return 1; fact = i = n; while (i != 0) i -= 1; fact *= i; } return fact; Wrong!!

Factorial n! = n  (n-1)  (n-2)  (n-3)  ......  1 int factorial(int n) { int fact,i; if (n == 0) return 1; fact = i = n; while (i != 1) i -= 1; fact *= i; } return fact;

Another form of while loop statement list T condition statement list condition T F F while (condition) { statement_list } do { statement_list } while (condition);

The For Loop v.s. In many coding problems a definite loop is needed That is, number of iterations is known before loop begins int n,i; n = ....; for (i=2; i<n; i +=1) { statement_list } int n,i; n = ....; i = 2; while (i<n) { statement_list i += 1; } v.s.

Factorial (using for loop) n! = n  (n-1)  (n-2)  (n-3)  ......  1 = n  (n-1)  (n-2)  (n-3)  ......  (n-(n-1)) int factorial(int n) { int fact=1,i; if (n < 0) return -1; for (i=0; i < n; i += 1) fact *= (n-i); } return fact; int factorial(int n) { int fact=1,i; if (n < 0) return -1; for (i=0; i < n; i++) fact *= (n-i); } return fact;

Increment i = i+1; i+=1; i++; ++i; i++ is called post-increment Each of the four statement will increase the value of the integer variable i by 1. i++ is called post-increment ++i is called pre-increment The different between the two is the time when the increment performed. i = 1, n = 1 i = 1, n = 0 Press any key to continue int i,n; i=0; n = ++i; cout << "i = " << i << ", n = " << n << endl; i=0 n = i++;

Decrement i = i-1; i-=1; i--; --i; i-- is called post-decrement Each of the four statement will decrease the value of the integer variable i by 1. i-- is called post-decrement --i is called pre-decrement The different between the two is the time when the increment performed. i=n=1; cout << "(n-- == i) is "; if (n-- == i) { cout << "true and n = " << n; } else { cout << "false and n = " << n; } (n-- == i) is true and n = 0 Press any key to continue

My own square root function (main) int main() { double s,t; do { cout << "Input a number:"; cin >> s; t = my_sqrt(s); if (t >= 0) cout << "The square root of " << s << " is " << t; else cout << "Can't find a square root of " << s; cout << endl; } while (t >= 0); return 0; }

My own square root function (incorrect my_sqrt) up a double my_sqrt(double a) { double up=a,down=0,st=a/2; if (a < 0) return -1; while (a - st*st != 0) if (st*st > a) up = st; else down = st; st = (up+down)/2; } return st; guess st real square root down Where can I improve the program? Where is the potential problem?

My own square root function (incorrect my_sqrt) double my_sqrt(double a) { double up=a,down=0,st=a/2,est; if (a < 0) return -1; est = st*st; while (a - est > 0.0000001) if (est > a) up = st; else down = st; st = (up+down)/2; } return st; What is the problem?

My own square root function (incorrect my_sqrt) double my_sqrt(double a) { double up=a,down=0,st=a/2,est; if (a < 0) return -1; est = st*st; while ((a - est > 0.0000001)||(a - est < -0.0000001)) if (est > a) up = st; else down = st; st = (up+down)/2; } return st; Still wrong!! What is the roblem?

My own square root function (my_sqrt) double my_sqrt(double a) { double up,down,st,est; if (a < 0) return -1; if ( a >= 1) { up=a; down=0;} else { up=1; down=a;} st = (up+down)/2; est = st*st; while ((a - est > 0.0000001) || (a - est < -0.0000001)) if (est > a) up = st; down = st; } return st;

Primality Test Prime number: A positive integer that cannot be factorized, or no numbers other that 1 and itself that can divide it. is 893 a prime? is (893 % 2 == 0) (893 % 3 == 0) (893 % 4 == 0) (893 % 5 == 0) (893 % 6 == 0) ............................ (893 % 892 == 0) We can write a while loop to test it.

Numbers inside Computer Bits Minimum Maximum Integers Short Signed 8 -128 127 Unsigned 255 Integer 16 -32,768 32,767 65,535 Long 32 -2,147,483,648 2,147,483,647 4,294,967,295 Range Floating Points Single -3.410-38 3.41038 Double 64 -1.710-308 1.710308