210 likes | 370 Views
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 .
E N D
Principles of Programming Revision
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.
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
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
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
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
Flow Chart Example START No Yes Read t1 Is t1=37? Warning! Temp is Normal STOP
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; • }
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
Understand the C++ Language • Errors • Syntax Errors • Runtime Errors • Logical Errors
Printing out a line of text • # include <iostream.h> • using namespace std; • int main() • { • cout<<“Hello World!”; • return 0; • }
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; • }
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; • }
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;
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
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
Decision Making • If statements • If – else statements • Nested if statements • Equality and Relational operators • Switch
Repetition • Loops • While loop • Do- While loop • For loop