1 / 21

Principles of Programming

Principles of Programming. Revision. A Computer. A Computer. A useful tool for solving a great variety of problems. To make a computer do anything (i.e. solve a problem), you have to write a computer program .

Download Presentation

Principles of 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. Principles of Programming Revision

  2. A Computer

  3. A Computer • A useful tool for solving a great variety of problems. • To make a computer do anything (i.e. solve a problem), you have to write a computer program. • The computer program tells a computer, step by step, exactly what you want it to do. • The computer then executes the program, following each step mechanically, to accomplish the end goal.

  4. Algorithms • The sequence of steps to be performed in order to solve a problem by the computer • Can be expressed as: • Natural languages • Pseudocode • Flowcharts • Programming Languages

  5. Natural Language Direct someone to the Library: • Assume the person is at the main gate • Take 35 steps to the fountain that overlooks the main gate • Walk past the fountain using the steps either to the left or right of the fountain. • Take 50 steps following the pavement after the fountain. • The building in front of you is the main library

  6. Natural Language Example 2 • Check patient’s temperature • Is the temperature less than or greater than 37 degrees? • If yes, give a warning • If no, let the patient know their temperature is normal

  7. Pseudocode Example • Temp = t1 • If (t1 <37)&(t1>37), Warning! • Else • Temp is Normal • OR • Temp = t1 • If(t1 ≠37), Warning! • Else Temp is Normal

  8. Flow Chart Example START No Yes Read t1 Is t1=37? Warning! Temp is Normal STOP

  9. Programming Language • It expresses an algorithm in a way that can be executed by a computer • # include<iostream.h> • using namespace std; • int main() • { • int t1;//t1 is temperature • cout<<“Enter temperature\n”; • cin>>t1; • if(t1!=37) • cout<<“Warning!\n”; • else • cout<<“Your Temperature is normal\n”; • return 0; • }

  10. Programming Language • Coded language used by programmers to writeinstructions that a computer can understand to do what the programmer (or the computer user) wants.Read more: http://www.businessdictionary.com/definition/programming-language.html

  11. Typical C++ development Environment

  12. Understand the C++ Language • Errors • Syntax Errors • Runtime Errors • Logical Errors

  13. Printing out a line of text • # include <iostream.h> • using namespace std; • int main() • { • cout<<“Hello World!”; • return 0; • }

  14. Escape Sequences

  15. Example Program 2 • # include<iostream.h> • using namespace std; • int main() • { • int x; • int y; • x=2; • y=4; • cout<<x<<“\t”<<y; • return 0; • } • # include<iostream.h> • using namespace std; • int main() • { • int x, y; • x=2; • y=4; • cout<<x<<“\t”<<y; • return 0; • }

  16. Example Program 3 • // Program to add two integers typed by user at keyboard • #include <iostream> • using namespace std; • int main() • { • int a, b, total; • cout << "Enter integers to be added:" << endl; • cin >> a >> b; • total = a + b; • cout<< "The sum is " << total << endl; • return 0; • }

  17. Definition of terms • Variables • Use valid identifiers • Identifier • Sequence of letters numbers/digits and underscores • Data type • int, Char, float, double, string • Declarations • int x; • Initialization • x = 0;

  18. Comments • // this comment spans one line • /* this kind of comment is used for more than one line*/ • Documentation • For the person reading your code • Not interpreted by the compiler

  19. Good Programming practice • Computation statements separated from declaration • int a, b, total; • cout << "Enter integers to be added:" << endl; • cin >> a >> b; • total = a + b; • cout << "The sum is " << total << endl; • Do not use variables you have not declared

  20. Decision Making • If statements • If – else statements • Nested if statements • Equality and Relational operators • Switch

  21. Repetition • Loops • While loop • Do- While loop • For loop

More Related