foundations of programming part i

Post on 02-Jul-2015

248 Views

Category:

Education

7 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Jayesh JoyMeher Anand

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

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

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

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

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;

}

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

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)

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;

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

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

Example 1

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: ++, --

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

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

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

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

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

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

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)

Ne s t e d Lo o p s

Example 8

top related