principles of programming
DESCRIPTION
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 . - PowerPoint PPT PresentationTRANSCRIPT
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:1. Assume the person is at the main gate2. Take 35 steps to the fountain that overlooks
the main gate3. Walk past the fountain using the steps
either to the left or right of the fountain.4. Take 50 steps following the pavement after
the fountain.5. The building in front of you is the main
library
Natural Language Example 21. Check patient’s temperature2. Is the temperature less than or greater
than 37 degrees?3. If yes, give a warning4. 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
Programming Language
It expresses an algorithm in a way that can be executed by a computer
1. # include<iostream.h>2. using namespace std;3. int main()4. {5. int t1;//t1 is temperature6. cout<<“Enter temperature\n”;7. cin>>t1;8. if(t1!=37)9. cout<<“Warning!\n”;10. else11. cout<<“Your Temperature is normal\n”;12. return 0;13. }
Programming Language
Coded language used by programmers to write instructions 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
Printing out a line of text
1. # include <iostream.h>2. using namespace std;3. int main()4. {5. cout<<“Hello World!”;6. return 0;7. }
Escape Sequences
Escape Sequence Description
\n Newline. Position the screen cursor to the beginning of the next line.
\t Horizontal tab. Move the screen cursor to the next tab stop.
\r Carriage return. Position the screen cursor to the beginning of the current line; do not advance to the next line.
\a Alert. Sound the system bell.
\\ Backslash. Used to print a backslash character.
\" Double quote. Used to print a double quote character.
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
1. // Program to add two integers typed by user at keyboard
2. #include <iostream> 3. using namespace std;4. int main()5. {6. int a, b, total;7. cout << "Enter integers to be added:" << endl;8. cin >> a >> b;9. total = a + b; 10. cout << "The sum is " << total << endl;11. return 0;12. }
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
1. int a, b, total;2. cout << "Enter integers to be added:"
<< endl;3. cin >> a >> b;4. total = a + b; 5. 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