Foundations of Programming Part I


Published on

Published in: Education, Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Foundations of Programming Part I

  1. 1. Jayesh Joy Meher Anand
  2. 2. The Hardware <ul><li>Programming – Making hardware perform an action in a desired way </li></ul><ul><li>Digital circuits are binary, they can understand only 0 and 1 </li></ul><ul><li>Modern day processors are made from transistors, millions of them </li></ul><ul><li>Work in time of the order of 10 -9 seconds </li></ul>
  3. 3. Evolution of Programming Languages <ul><li>Machine language – consists of 0s and 1s only </li></ul><ul><li>Very hard to read and debug </li></ul><ul><li>Time consuming </li></ul><ul><li>Low level language </li></ul><ul><li>Platform dependent </li></ul>
  4. 4. Evolution (contd...) <ul><li>Assembly language – consists of mnemonics and opcodes </li></ul><ul><li>Ex:- MOV A,B </li></ul><ul><li>Better readability but not sufficient </li></ul><ul><li>Hard to debug but better than machine code </li></ul><ul><li>Low level language </li></ul><ul><li>Platform dependent </li></ul>
  5. 5. Evolution (contd...) <ul><li>High level languages – English-like constructs, definitions, declarations. </li></ul><ul><li>Easier to read, debug and maintain </li></ul><ul><li>Ex- C, C++, C#, Java etc. </li></ul><ul><li>Platform independent </li></ul>
  6. 6. The Hello World Program <ul><li>#include<iostream> </li></ul><ul><li>using namespace std; </li></ul><ul><li>//This is my first program </li></ul><ul><li>int main()‏ </li></ul><ul><li>{ </li></ul><ul><ul><li>cout<<”Hello, World!”<<endl; </li></ul></ul><ul><ul><li>return 0; </li></ul></ul><ul><li>} </li></ul>
  7. 7. Explanation <ul><li>Program execution starts from main(), also known as PROGRAM ENTRY POINT </li></ul><ul><li>cout<< prints the text following it on to the screen </li></ul><ul><li>endl is equivalent to the Enter key on the keyboard. Starts off at the next line, the next time. </li></ul><ul><li>Blindly include the blue parts in the code for now </li></ul>
  8. 8. Data <ul><li>Programs can take in data, operate on it and give an output </li></ul><ul><li>Data is stored in variables. </li></ul><ul><li>Each variable is of a certain “Data Type” </li></ul><ul><li>Primitive (or standard) data types – </li></ul><ul><ul><li>Integers (int)‏ </li></ul></ul><ul><ul><li>floating point (real) numbers (float)‏ </li></ul></ul><ul><ul><li>Characters (char)‏ </li></ul></ul>
  9. 9. Declarations <ul><li>Tell the machine that you want to create a variable of a particular data type </li></ul><ul><li>Assign a name to a particular variable </li></ul><ul><li>Assign a value to it and start using it </li></ul><ul><li>Example declaration – Declare an integer variable with the name 'apples' and assign a value 40 to it </li></ul><ul><li>int no_of_apples; </li></ul><ul><li>no_of_apples=40; </li></ul>
  10. 10. Input <ul><li>Take in data from the user (keyboard) while the program is running </li></ul><ul><li>cin>> operator instructs machine to take in input from user </li></ul>
  11. 11. Example program <ul><li>Example 1 </li></ul>
  12. 12. Operators <ul><li>Basic Arithmetic: +, -, /, *, % </li></ul><ul><ul><li>Can be used for operations on int & float values. </li></ul></ul><ul><li>Bitwise: & (and) , | (or) , ^ (xor)‏ </li></ul><ul><li>Relational and Logical: <, >, <=, >=, ==, !=, && (and) , || (or) , ! (not)‏ </li></ul><ul><li>Sign: -, + </li></ul><ul><li>Increment and Decrement: ++, -- </li></ul>
  13. 13. Hierarchy of Operators <ul><li>() [] </li></ul><ul><li>! ~ ++ -- + - (all are unary operators)‏ </li></ul><ul><li>* / % </li></ul><ul><li>+ - </li></ul><ul><li><< >> </li></ul><ul><li>< <= > >= </li></ul><ul><li>== != </li></ul>
  14. 14. Hierarchy of Operators (contd.) <ul><li>& </li></ul><ul><li>^ </li></ul><ul><li>| </li></ul><ul><li>&& </li></ul><ul><li>|| </li></ul><ul><li>?: </li></ul><ul><li>= += -= *= /= %= &= ^= |= <<= >>= </li></ul><ul><li>, </li></ul>
  15. 15. Control flow <ul><li>The following statements are used to control the order in which statements are executed: </li></ul><ul><ul><li>Selection statements: </li></ul></ul><ul><ul><ul><li>if, if else, switch </li></ul></ul></ul><ul><ul><li>Iterative Loops: </li></ul></ul><ul><ul><ul><li>while, do while, for </li></ul></ul></ul>
  16. 16. Selection Statements <ul><li>Used to choose which statements to execute based on certain conditions </li></ul><ul><li>Example 2 </li></ul><ul><li>Example 3 </li></ul><ul><li>Example 4 </li></ul>
  17. 17. Flow Chart Yes No Entry Exit number>=0 Take input into number Print on screen “number is non-negative”
  18. 18. Iterative Loops <ul><li>Perform repetitive tasks </li></ul><ul><ul><li>If we want to perform certain tasks for a certain number of times or until a certain condition is met. </li></ul></ul><ul><ul><li>Three types of loops are used: </li></ul></ul><ul><ul><ul><li>while (example 5) </li></ul></ul></ul><ul><ul><ul><li>do while (example 6) </li></ul></ul></ul><ul><ul><ul><li>for (example 7) </li></ul></ul></ul>
  19. 19. Nested Loops <ul><li>Example 8 </li></ul>