170 likes | 180 Views
0 loop in :: 1 loop end :: 3 loop in :: 4 loop end :: 6 7 Press any key to continue. int main() { ........ ........ int i=0; cout << i << endl; for ( i = 1; i < 5; i ++) { cout << "loop in :: " << i << endl; i +=2; cout << "loop end :: " << i << endl; }
E N D
0 loop in :: 1 loop end :: 3 loop in :: 4 loop end :: 6 7 Press any key to continue int main() { ........ ........ int i=0; cout << i << endl; for (i = 1; i < 5; i++) { cout << "loop in :: " << i << endl; i+=2; cout << "loop end :: " << i << endl; } cout << i << endl; return 0; } Scopes of Variables IT 279
no standard for implementation!! int main() { ........ ........ int i=0; for (int i = 1; i < 5; i++) { cout << "loop in :: " << i << endl; i+=2; cout << "loop end :: " << i << endl; } for (int i = 1; i < 5; i++) { cout << "loop in :: " << i << endl; i+=2; cout << "loop end :: " << i << endl; } return 0; } Scopes of Variables in for-loop’s condition IT 279
0 loop in :: 1 loop end :: 5 loop in :: 2 loop end :: 5 loop in :: 3 loop end :: 5 loop in :: 4 loop end :: 5 5 Press any key to int main() { int i=0; cout << i << endl; for (i = 1; i < 5; i++) { cout << "loop in :: " << i << endl; int i=3; i+=2; cout << "loop end :: " << i << endl; } cout << i << endl; return 0; } Nested Scopes IT 279
0 loop in :: 1 loop end :: 1 loop in :: 2 loop end :: 2 loop in :: 3 loop end :: 3 loop in :: 4 loop end :: 4 5 Press any key to int main() { int i=0; cout << i << endl; for (i = 1; i < 5; i++) { cout << "loop in :: " << i << endl; { int i=3; i+=2; } cout << "loop end :: " << i << endl; } cout << i << endl; return 0; } Nested Scopes IT 279
0 loop in :: 1 inner for loop j :: 1 inner for loop j :: 2 loop end :: 1 loop in :: 2 inner for loop j :: 2 loop end :: 2 loop in :: 3 loop end :: 3 loop in :: 4 loop end :: 4 5 int main() { int i=0,j; cout << i << endl; for (i = 1; i < 5; i++) { cout << "loop in :: " << i << endl; for (j = i; j < 3; j++) { cout << "\t inner for loop j :: " << j << endl; } cout << "loop end :: " << i << endl; } cout << i << endl; return 0; } Loops and Scopes IT 279
0 loop in :: 1 inner for loop i :: 1 inner for loop i :: 2 loop end :: 3 loop in :: 4 loop end :: 4 5 int main() { int i=0; cout << i << endl; for (i = 1; i < 5; i++) { cout << "loop in :: " << i << endl; for (i = i; i < 3; i++) { cout << " inner for loop i :: " << i << endl; } cout << "loop end :: " << i << endl; } cout << i << endl; return 0; } Loops and Scopes IT 279
0 loop in :: 1 inner for loop i :: 0 inner for loop i :: 1 inner for loop i :: 2 loop end :: 3 loop in :: 2 inner for loop i :: 0 inner for loop i :: 1 inner for loop i :: 2 loop end :: 3 loop in :: 3 inner for loop i :: 0 inner for loop i :: 1 inner for loop i :: 2 loop end :: 3 loop in :: 4 inner for loop i :: 0 inner for loop i :: 1 inner for loop i :: 2 loop end :: 3 5 int main() { int i=0; cout << i << endl; for (i = 1; i < 5; i++) { cout << "loop in :: " << i << endl; int i=0; for (i = i; i < 3; i++) { cout << " inner for loop i :: " << i << endl; } cout << "loop end :: " << i << endl; } cout << i << endl; return 0; } Loops and Scopes IT 279
Compiler directive: #define, usage 1 #include<iostream> using namespace std; #defineTAX 0.075 //double TAX=0.08; #defineLAST_NAME "Li" #defineFIRST_NAME "Dennis" #defineGO #define SKIP void main() { cout << FIRST_NAME << " " << LAST_NAME << endl; cout << "TAX = " << TAX << endl; #ifdefGO cout << "GO\n"; cout << "and GO\n"; #endif #ifndefSKIP cout << "NO SKIP\n"; #endif cout << endl; } IT 279
Compiler directive: #define, usage 2 #definenow_define_add(type)\ type add(type a, type b) \ { return a+b; } \ now_define_add(int) now_define_add(double) void main() { int a=1,b=2; double r=1, s=2; cout << a / add(a,b) << endl; cout << a / add(r,s) << endl; } continue to the next line IT 279
A place for definitions. i.e., variables and function definition. Name space: #include <iostream> using namespace std; namespace room201 { char *instructor = "Osborne"; char *teach() { return "NetWorking"; } } namespace room211 { char *instructor = "Li"; char *teach() { return "Cryptography"; } } IT 279
Tell in which room.. Using Name space (1): #include <iostream> using namespace std; namespace room201 {...} namespace room211 {...} void main() { cout << room201::instructor << " teaches " << room201::teach() << endl; cout << room211::instructor << " teaches " << room211::teach() << endl; .... } As in the previous slide Osborne teaches NetWorking Li teaches Cryptography IT 279
Tell which room to use.. Using Name space (2): #include <iostream> using namespace std; namespace room201 {...} namespace room211 {...} void main() { ...... { using namespace room201; cout << instructor << " teaches "; cout << teach() << endl; } using namespace room211; cout << instructor << " teaches " << teach() << endl; } Osborne teaches NetWorking Li teaches Cryptography IT 279
Function examples: f(x) = 2x. f(1) = 2, f(4) = 8 g(x,y) = x+y. g(2,3)=5, g(4,5)=9 g(f(4),g(5,7)) = g(8,12)=20 // define functions f and g int f(int x) { return 2*x; } int g(int x, int y) { int i; i = x+y; return i; } int main() { ...... cout << f(2)+f(4) << “,” << g(f(2),g(5,7)); } IT 279
Prototypes of Functions .... int f(inta); int g(intb, intc); int main() { int a,b,c; ......... cout << f(a)+f(b) << “,” << g(f(a),g(b,c)); } int f(int x) { return 2*x; } int g(int x, int y) { int i; i = x+y; return i; } IT 279
Overload of a functions .. a function has more one definition intg(double a); // g double intg(double a, int b); // g double int intg(int a, int b); // g int int intg(int a, int b, int c); // g int int int intg(int a, int b, double c); // g int int double // int g(int a, int b, int c); // this is not allowed // double g(int a, int b, int c); // this is not allowed int main() { double r,s; int i,j; cout << g(r)*g(i); cout << g(r,i); // g(i,j) g(i,r) are not cout << g(i,i,j); cout << g(i,i,r); } int g(int x, int y) { return x+y; } int g(double x, int y) { return x/y; } ....... Signature of functions IT 279
Recursive function 5! = 5*4*3*2*1 = 5*(4!) f(5) = 5*f(4); f(n) = n*f(n-1); // A function that calls itself int factorial(int n) { if (n < 0) return –1; // Error if (n == 0) return 1;// By definition; int prefc, ans; prefc =factorial(n-1); ans = n*prefc; return ans; // or, simplyreturn n*factorial(n-1) } IT 279
What can be returned: anything! struct token { int value; int priority; }; deque<token> prepare(string exp); int evaluate(deque<token> exp); token get_a_token(string & ascii_string); ....... int main() { ........ } deque<token> prepare(string ascii_string) { deque<token> exp(0); token k; do { k = get_a_token(ascii_string); exp.push_back(k); } while (k.priority != 1); returnexp; } call by reference IT 279