for( 起始條件 ; 判斷式 ; 條件運算 ){ // 迴圈內容 } while( 判斷式 ){ // 迴圈內容 } do{ // 迴圈內容 } while( 判斷式 ) ;

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

Recursion.
Divisor máximo de dois inteiros. unsigned int gcd(unsigned int A, unsigned int B) { if (B > A) return gcd(B,A); else if (B==0) return A; else return gcd(B,A%B);}
Recursion practice. Problem 0 Using recursion (and no arrays), write the code to read in a series of numbers (until EOF) and then print them backwards.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 CIS Jan Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
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.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
Searching Arrays Linear search Binary search small arrays
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
 Write a program that uses a one dimension to do a table look-up  Learn about parallel arrays.
Run-Time Type Identification Jim Fawcett CSE687 – Object Oriented Design Spring 2007.
Chapter 05 (Part V) Control Statements: Part II. Nested For-Structures Consider the following codes: for (int i=0; i
 For Loops › for (variable set; condition; incremental or decrement){ // loop beginning › } // loop end  While loops › while (condition) { // beginning.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 4.
Starting Out with C++, 3 rd Edition 1 Searching an Arrays.
COMP102 Lab 121 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Searching Dr. Jose Annunziato. Linear Search Linear search iterates over an array sequentially searching for a matching element int linearSearch(int haystack[],
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
Palindromes revisited Here's a simpler program for checking palindromes: int nums[100]; int i = 0, a; cin >> a; while(a > 0) { nums[i++] = a; cin >> a;
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
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.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Review the following: if-else One branch if Conditional operators Logical operators Switch statement Conditional expression operator Nested ifs if –else.
Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 2.
Review the following : Flowcharting Variable declarations Output Input Arithmetic Calculations Conditional Statements Loops.
Overview Go over parts of quiz? Another iteration structure for loop.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Fundamental Programming Fundamental Programming More on Repetition.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
1 11/12/04CS150 Introduction to Computer Science 1 More Arrays.
 Learn how to form strings using one-dimensional array  String manipulation functions:  strcpy  strrev  strcmp  Program using strings.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Exercises on Polymorphism and Operator Overloading TCP1201: 8.
01/05/100 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while.
Program Development and Design Using C++, Third Edition
Chapter five exercises. a. false; b. true; c. false; d. true; e. true; f. true; g. true; h. false.
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
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.
T/F  The following code will compile without error. int x = 3, y = 4, z = 5; double k = 3.4; cout
Searching Arrays Linear search Binary search small arrays
生查子 ~ 歐陽修 去年元夜時,花市燈如晝, 月上柳梢頭,人約黃昏後; 今年元夜時,月與燈依舊, 不見去年人,淚濕春衫袖。
Section 3 Review Mr. Crone.
לולאות קרן כליף.
CS149D Elements of Computer Science
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13]
Random Number Generation
Conditional Construct
Counting Loops.
Chapter 9 One-Dimensional Arrays
Repetition Control Structure
Recursive GCD Demo public class Euclid {
If Statements.
Fundamental Programming
CS149D Elements of Computer Science
General Condition Loop
ㅎㅎ Fifth step for Learning C++ Programming Homework 1 Homework 2
Presentation transcript:

for( 起始條件 ; 判斷式 ; 條件運算 ){ // 迴圈內容 } while( 判斷式 ){ // 迴圈內容 } do{ // 迴圈內容 } while( 判斷式 ) ;

Example: Please output 0 ~ 9 for( int i = 0; i <10 ; i++){ cout << i << “ ”; } int i = 0; while( i < 10){ cout << i << “ ”; i++; } 請按任意鍵繼續... _

while & do while int i = 10; while( i < 10){ cout << i << “ ”; i++; } int i = 10; do{ cout << i << “ ”; i++; } while( i < 10); 請按任意鍵繼續... _ 10 請按任意鍵繼續... _

起始條件、判斷式、條件運算可以不只一個。 for(int i = 0, j = 1, k = 0; j < 5 || i++ ; j++, k++){ cout<< i << " " << j << " " << k << endl; } 請按任意鍵繼續..._

What about for(int i = 0, j = 1, k = 0; j < 5 || ++i ; j++, k++){ cout<< i << " " << j << " " << k << endl; } Infinite loop

WHY ?

while( true ) while( false ) = while( 1) = while( 0) 若條件為 true 可以省略條件不寫 → while() 若條件為 false 可以省略迴圈不寫 → 留他何用?

隨手一個 break 救救無限迴圈

for(int i = 0 ; ; i++) { if(i == 10){ break; } cout << i << " "; } 請按任意鍵繼續... _ break 會跳出一層迴圈

for(int i = 0; i < 10; i++) { if(i % 3 == 0){ continue; } cout << i << " "; } continue 會直接跳到最後面 請按任意鍵繼續... _

(condition1 || condition2 && condition3) condition1 is true condition1 is false → condition2 is true → condition3 is true→ true → condition2 is true→ condition3 is false→ false → condition2 is false→ false → true

NOTE int array[3] = {0, 1, 2}; for( int i = 0; i <= 3; i++) { cout << array[i] << " "; } 請按任意鍵繼續... _

NOTE for( int i = 0; i <= 3; i++) { cout << i << " "; } cout << i << " "; name lookup of `i' changed for new ISO `for' scoping

NOTE Give appropriate condition. Infinite loop would cause Time Limit Exceeded. for( int i = 0; i < 10; i--) { cout << i << " "; }

NOTE while( condition 1){ … while( condition 2){ … while( condition 3){ … } … } … } 括號的對應很重要。

Recursion

遞迴 → 自己呼叫自己 Example: n! int f( int n){ if( n == 1){ return 1; } else return n * f( n-1); } n! = n * ( n - 1) * … * 1 = n * ( n - 1)!

int f( 3){ if( 3 == 1){ return 1; } else return 3 * f( 3-1); } When n = 3 : int f( 2){ if( 2 == 1){ return 1; } else return 2 * f( 2-1); } int f( 1){ if( 1 == 1){ return 1; } else return 1 * f( 1- 1); }

int f( 3){ if( 3 == 1){ return 1; } else return 3 * f( 3-1); } When n = 3 : int f( 2){ if( 2 == 1){ return 1; } else return 2 * 1; }

int f( 3){ if( 3 == 1){ return 1; } else return 3 * 2; } When n = 3 : 注意: 一定要寫終止條件

費氏數列 … F(1) = 1F(2) = 1F( n) = F( n - 1 ) + F( n - 2) int f( int n ){ if ( n == 1 || n == 2){ return 1; } else{ return f( n - 1 ) + f( n - 2 ); }

F(5) F(1) = 1F(2) = 1F( n) = F( n - 1 ) + F( n - 2) F(4)F(3) F(2) F(1) F(2) F(1)

int GCD( int x, int y){ if( x % y != 0){ return GCD( y, x % y); } else{ return y; } 最大公因數 GCD GCD( 187, 209) GCD( 209, 187) GCD( 187, 22) GCD( 22, 11) int GCD( 187, 209){ if( 187 % 209 != 0){ return GCD( 209, 187); } else{ return y; } int GCD( 209, 187){ if(209 % 187 != 0){ return GCD( 187, 22); } else{ return y; } int GCD( 187, 22){ if( 187 % 22 != 0){ return GCD( 22, 11); } else{ return y; } int GCD( 22, 11){ if( 22 % 11 != 0){ return GCD( y, x % y); } else{ return 11; } 11

int f( n){ if( n == 1){ return 1; } else return n * f( n-1); } for( int i = 1, j = 1; i <= n; i++){ j = j * i; } Recursion Loop

Exercise

基礎 : 100 The 3n + 1 problem 408 Uniform Generator 488 Triangle Wave F the 9s 進階 : Queens Chess Problem 785 Grid Colouring Sweet Child Makes Trouble