1 / 37

Review of Basic Structured Programming

M. Taimoor Khan taimoorkhan@ciit-attock.edu.pk. Review of Basic Structured Programming . Basic Program Construction. #include< iostream.h > void main() { //This is my first C++ Program /* This program will display a string message on the screen */

wanda
Download Presentation

Review of Basic Structured Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. M. Taimoor Khan taimoorkhan@ciit-attock.edu.pk Review of Basic Structured Programming

  2. Basic Program Construction #include<iostream.h> void main() { //This is my first C++ Program /* This program will display a string message on the screen */ cout << “Hello to C++”; } Header file Preprocessor directives Main method Console output stream Insertion operator String constant or string literal Comments White spaces Program statements

  3. Variables #include<iostream.h> void main() { int var1; int var2, var3; var1 = 5; int var4 = 10; cout << var1 + var4 << endl; } Variable names Type Declaration Initialization Arithmetic's / Comparisons End line manipulator

  4. Local and Global Variables #include<iostream.h> int numb = 15; void main() { int var1; int var2, var3; { intvar 4; intvar 5; } var1 = 5; int var6 = 10; cout << var1 + var4 << endl; }

  5. Static and Constant Variables #include<iostream.h> void main() { int var1; int var2, var3; { static int var4 = 9; } var1 = 5; int var5 = 10; const int var6 = 20; cout << var1 + var4 << endl; }

  6. Other Variables Types • Float • Double • Char • Bool • Short • long

  7. Escape Characters #include<iostream.h> void main() { cout << “ Things are \a getting \n better in \t c++”; } \a beep \b backspace \n new line \t tab \\ \’ \” etc

  8. Input Stream #include<iostream.h> void main() { int score; cout << “Please enter the score value\n”; cin >> score; cout << “Score is: \t” << score; } Extraction operation / get from operator

  9. Type Conversion / Mixed Expression #include<iostream.h> void main() { int height = 7; float length = 4.5; double sum = height + length; }

  10. Type Conversion / Type Casting #include<iostream.h> void main() { int var1 = 5; char var2 = static_cast<char>(var1); } Static_cast<type>(Variable)

  11. Arithmetic Operations #include<iostream.h> void main() { int a = 5; int b = 6; cout << a % b; } +, -, %, * /

  12. Assignment Operators #include<iostream.h> void main() { int a = 5; int b = 6; b += a; cout << a % b; } =, +=, -=, *=, /=

  13. Increment / Decrement Operator #include<iostream.h> void main() { int a = 5; cout << a << endl; cout << a++ << endl; cout << ++a << endl; cout << a-- << endl; cout << --a << endl; } Prefix, postfix

  14. Relational Operators #include<iostream.h> void main() { int a = 5; int b = 6; cout << (a <= b); } <, >, ==, !=, <= >=

  15. Loops • For Loop • While Loop • Do While Loop

  16. For Loop #include<iostream.h> void main() { for(inti = 0; i < 5; i++) cout << i*2; } Declaration and initialization expression Test expression Increment expression

  17. For Loop #include<iostream.h> void main() { int a; for( a =0; a<5; a++) { a = a*2; cout << a; } } Blocks

  18. Nested For Loop #include<iostream.h> void main() { for(inti=0; i<2;i++) { for(int j=0; j<3; j++) cout << i+j<<endl; } }

  19. While Loop #include<iostream.h> void main() { int a = 4; while (a > 1) { cout <<a <<endl; a--; } }

  20. Do-While Loop #include<iostream.h> void main() { int a = 5; do{ cout <<a <<endl; a--; }while(a>2); }

  21. Control / Decision Statements #include<iostream.h> void main() { int a = 5; int b = 6; if(a > b) cout << a % b; } if

  22. If-else statement #include<iostream.h> void main() { int a = 5; int b = 6; if(a > b) cout << a % b; else cout << b % a; }

  23. Nested If-else #include<iostream.h> void main() { int a = 5; int b = 6; if(a < 3) if( a > b) cout << a % b; else cout << b % a; }

  24. Switch Statement #include<iostream.h> void main() { int num; cout << Enter a number <<endl;cin>> marks; switch(marks) { case 0: cout << “low”; break; case 1: cout << “medium”; break; case 2: cout << “high”; break; default: cout << “very high”; } }

  25. Logical Operators #include<iostream.h> void main() { int var1 = 5; int var2 = 6; if(var1>3 && var1>var2) cout << “var1 is a high number” <<endl; } &&, ||, !

  26. Operator Precedence • Unary !, ++, -- • Arithmetic *, /, %, +, - • Relational <,>,<=,>=,==,!= • Logical &&, || • Conditional ?: • Assignment =, +=, -=, *=, /=, %=

  27. Functions #include<iostream.h> void display(); void main() { cout << “main body” <<endl; display(); cout << “main body” <<endl; } void display() { cout << “Display function called” <<endl; } Declaration Definition Calling

  28. Passing variables #include<iostream.h> int sum(intv1, int v2); void main() { int a = 5; int b = 6; cout << sum(a, b); } int sum(int v1, int v2) { int temp = v1 + v2; return temp; } By value Return statement

  29. By Reference #include<iostream.h> int sum(int& v1, int& v2) { return v1 + v2; } void main() { int a = 5; int b = 6; cout << a % b; } Passing objects

  30. Function Overloading #include<iostream.h> int sum(int v1, int v2) { cout << “function 1” <<endl; return v1 + v2; } int sum(int v1, int v2, int v3) { cout << “function 2” <<endl; return v1 + v2 + v3; } float sum (float v1, float v2) { cout << “function 3” <<endl; return v1 + v2; } void main() { int a = 5; int b = 6; int c = 10; float d = 4.5; float e = 6.4; cout << sum(a, b); cout << sum(a, b, c); cout << sum (d, e); }

  31. Recursion #include<iostream.h> Int fact(int n) { if(n > 1) return n * fact(n – 1); else return 1; } void main() { fact(3); }

  32. Inline Function #include<iostream.h> inline intgetTwice(int a) { return a*2; } void main() { cout << getTwice(4); }

  33. Scope of variables • Local Variables • Global Variables • Static Local Variables

  34. Global Variable #include<iostream.h> int a = 5; intsomeFunction() { return a; } void main() { int b = a; cout << a % b; }

  35. Static Local Variables #include<iostream.h> int a = 5; intsomeFunction() { static inthalfValue = a/2; return a; } void main() { int b = a + halfValue; cout << a % b; }

  36. Returning by Reference #include<iostream.h> int& sum(int a, int b) { return a+b; } void main() { int a = 4; int b = 5; sum(a, b); }

  37. Const Function Arguments #include<iostream.h> intsomeFunction(const int b) { if(b < 2) cout << “B < 2” <<endl; } void main() { int b = 5; cout <<someFunction(b); }

More Related