foundations of programming part i

19
Jayesh Joy Meher Anand

Upload: vnit-acm-student-chapter

Post on 02-Jul-2015

248 views

Category:

Education


7 download

TRANSCRIPT

Page 1: Foundations of Programming Part I

Jayesh JoyMeher Anand

Page 2: Foundations of Programming Part I

Th e Ha r d wa r e

Programming – Making hardware perform an action in a desired way

Digital circuits are binary, they can understand only 0 and 1

Modern day processors are made from transistors, millions of them

Work in time of the order of 10-9 seconds

Page 3: Foundations of Programming Part I

Ev o l u t i o n o f Pr o g r a mmi n g La n g u a g e s Machine language – consists of 0s and 1s only

Very hard to read and debug

Time consuming

Low level language

Platform dependent

Page 4: Foundations of Programming Part I

Ev o l u t i o n ( c o n t d . . . )

Assembly language – consists of mnemonics and opcodes

Ex:- MOV A,B Better readability but not sufficient Hard to debug but better than machine code Low level language Platform dependent

Page 5: Foundations of Programming Part I

Ev o l u t i o n ( c o n t d . . . )

High level languages – English-like constructs, definitions, declarations.

Easier to read, debug and maintain Ex- C, C++, C#, Java etc. Platform independent

Page 6: Foundations of Programming Part I

Th e He l l o Wo r l d Pr o g r a m

#include<iostream>

using namespace std;

//This is my first program

int main()

{

cout<<”Hello, World!”<<endl;

return 0;

}

Page 7: Foundations of Programming Part I

Ex p l a n a t i o n

Program execution starts from main(), also known as PROGRAM ENTRY POINT

cout<< prints the text following it on to the screen

endl is equivalent to the Enter key on the keyboard. Starts off at the next line, the next time.

Blindly include the blue parts in the code for now

Page 8: Foundations of Programming Part I

Da t a

Programs can take in data, operate on it and give an output

Data is stored in variables. Each variable is of a certain “Data Type” Primitive (or standard) data types –

Integers (int) floating point (real) numbers (float) Characters (char)

Page 9: Foundations of Programming Part I

De c l a r a t i o n s

Tell the machine that you want to create a variable of a particular data type

Assign a name to a particular variable Assign a value to it and start using it Example declaration – Declare an integer

variable with the name 'apples' and assign a value 40 to it

int no_of_apples;no_of_apples=40;

Page 10: Foundations of Programming Part I

I n p u t

Take in data from the user (keyboard) while the program is running

cin>> operator instructs machine to take in input from user

Page 11: Foundations of Programming Part I

Ex a mp l e p r o g r a m

Example 1

Page 12: Foundations of Programming Part I

Op e r a t o r s

Basic Arithmetic: +, -, /, *, % Can be used for operations on int & float values.

Bitwise: & (and), | (or), ^(xor)

Relational and Logical: <, >, <=, >=, ==, !=, && (and), || (or), ! (not)

Sign: -, + Increment and Decrement: ++, --

Page 13: Foundations of Programming Part I

Hi e r a r c h y o f Op e r a t o r s

() [] ! ~ ++ -- + - (all are unary operators) * / % + - << >> < <= > >= == !=

Page 14: Foundations of Programming Part I

Hi e r a r c h y o f Op e r a t o r s ( c o n t d . ) & ^ | && || ?: = += -= *= /= %= &= ^= |= <<= >>= ,

Page 15: Foundations of Programming Part I

Co n t r o l f l o w

The following statements are used to control the order in which statements are executed: Selection statements:

if, if else, switch

Iterative Loops: while, do while, for

Page 16: Foundations of Programming Part I

S e l e c t i o n S t a t e me n t s

Used to choose which statements to execute based on certain conditions

Example 2Example 3Example 4

Page 17: Foundations of Programming Part I

F l o w Ch a r t

number>=0

Ta k e i n p u t i n t o

n u mb e r

Yes

Print on screen “number is non-

negative”

No

Entry

Exit

Page 18: Foundations of Programming Part I

I t e r a t i v e Lo o p s

Perform repetitive tasks If we want to perform certain tasks for a certain

number of times or until a certain condition is met.

Three types of loops are used: while (example 5) do while (example 6) for (example 7)

Page 19: Foundations of Programming Part I

Ne s t e d Lo o p s

Example 8