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.410-38 3.41038 Double 64 -1.710-308 1.710308