PF Lecture 1&2

43
Programming Fundamentals Lecture 01 Introduction

description

programming fundamental

Transcript of PF Lecture 1&2

Page 1: PF Lecture 1&2

Programming Fundamentals

Lecture 01

Introduction

Page 2: PF Lecture 1&2

Policy for the distribution of marks and examination is as follows:

Class performance & attendance 10 marks Presentation 05

marks Assignment 05

marks Midterms 30 marks Final 50 marks

Course Policy

Page 3: PF Lecture 1&2

A program is a precise sequence of steps to solve a particular problem.

Program

Page 4: PF Lecture 1&2

It goes against the grain of modern education to teach children to program. What fun is there in making plans, acquiring discipline in organizing thoughts, devoting attention to detail and learning to be self-critical.

Alan Perlis – Yale University

Page 5: PF Lecture 1&2

Analysis Critical Thinking Attention to Detail

Critical Skills

Page 6: PF Lecture 1&2

To design a program properly, we must: Analyze a problem statement, typically

expressed as a word problem. Express its essence, abstractly and with

examples. Formulate statements and comments in a precise

language. Evaluate and revise the activities in light of

checks and tests.

Design Recipe

Page 7: PF Lecture 1&2

Computers are

STUPID

Page 8: PF Lecture 1&2

Humans are even more…….

Page 9: PF Lecture 1&2

Think Reuse Think User Interface Comments liberally

Skills Needed for Programming

Page 10: PF Lecture 1&2

Think Reuse

Page 11: PF Lecture 1&2

Area of the Ring

Area of Outer Circle = Area of the Ring

Page 12: PF Lecture 1&2

System software

Communicates with computer hardware and controls different aspects operations. Sub categories are:

System Software Device DriversUtilities

Software categories

Page 13: PF Lecture 1&2

Application softwareA group of program design for end user.e.g. Payroll Systems, MS Word, Accounting Systems.

Software categories

Page 14: PF Lecture 1&2

Compiler & interpreter

Compiler & interpreter are the translators to translate programming language into machine language.

Interpreters translates the program line by line meaning it reads one line of program and translates it, then it reads second line, translate it and so on.

The Compiler read the whole program and translates it into machine language completely.

Tools of Trade

Page 15: PF Lecture 1&2

What is difference between Compiler and interpreter ?

Home work

Page 16: PF Lecture 1&2

EditorsIt is a tool for writing the code of program.

DebuggerCorrects Logical errors during the execution of program.

LinkerLinks the program with routines and functions located on other file.

LoaderLoad the program in main memory and instruct the processor to execute the program from first instruction.

Tools of Trade (cont.)

Page 17: PF Lecture 1&2

Programming Fundamentals Lecture 02

Page 18: PF Lecture 1&2

# include <iostream.h>main(){cout << "Welcome to Indus University ";}

Basic Syntax

Page 19: PF Lecture 1&2

# include <iostream.h> # include is pre-processor directive. It is an instruction to compiler to

add file iostream.h <iostream.h> Name of the library definition file for all Input Output Streams. main() main is actually the one which is run when your program is used. {} Braces allows to group together pieces of a program. cout cout takes data from computer and sends it to the output. << The sign << indicates the direction of data. Here it is towards cout and the

function ofcout is to show data on the screen.

Basic Syntax

Page 20: PF Lecture 1&2

<< The sign << indicates the direction of data. Here it is

towards cout and the function ofcout is to show data on the screen.

Welcome to Indus University The thing between the double quotes (“ ”) is known as

character string. In C++ programming character strings are written in double quotes.

; All C statements end with semicolon (;). Missing of a

semicolon (;) at the end of statement is a syntax error and compiler will report an error during compilation.

Page 21: PF Lecture 1&2

Name of locations in memory for storing data.

Variables

X

Page 22: PF Lecture 1&2

Picture of the memory

Variable

25

10323

nameof the variable

Page 23: PF Lecture 1&2

Variable

Variable starts with1. Character2. Underscore _ (Not Recommended)

Page 24: PF Lecture 1&2

Small post box

Variable

X

Page 25: PF Lecture 1&2

In a program every variable has

Name Type Size Value

Variables

Page 26: PF Lecture 1&2

A scope is a region of the program, where variables can be declared:

There are three scopes to declare a variable

◦ Inside a function or a block which is called local variables.◦ In the definition of function parameters which is called formal

parameters.◦ Outside of all functions which is called global variables.

Scope of variable

Page 27: PF Lecture 1&2

Variables that are declared inside a function or block are local variables. They can be used only by statements that are inside that function or block of code. Local variables are not known to functions outside their own.

#include <iostream.h>void main () {

// Local variable declaration: int a, b; int c; // actual initialization a = 10; b = 20; c = a + b; cout << c;

}

Local Variable

Page 28: PF Lecture 1&2

Global variables are defined outside of all the functions, usually on top of the program. The global variables will hold their value throughout the life-time of your program.

include <iostream.h> // Global variable declaration:

int g; void main () {

// Local variable declaration: int a, b; // actual initialization a = 10;

b = 20; g = a + b;

cout << g; }

Global variable

Page 29: PF Lecture 1&2

Assignment Operator

=x = 2

X 2

Page 30: PF Lecture 1&2

L.H.S = R.H.S.

X+ 3 = y + 4 WrongZ = x +4x +4 = Z Wrong

Assignment Operator

Page 31: PF Lecture 1&2

X = 10 ;

X = 30 ;

X 10

X 30

Page 32: PF Lecture 1&2

X = X + 1;

X 10 + 1=

X11

Page 33: PF Lecture 1&2

A data type determines what type of values an object can have and what operations can be performed.

Reserve words.

Data types

Page 34: PF Lecture 1&2

Data type int i ; ->

Declaration line i

Page 35: PF Lecture 1&2

Type KeywordBoolean boolCharacter charInteger intFloating point floatDouble floating point doubleValueless void

Data types

Page 36: PF Lecture 1&2

Data types their size and rangeType Typical Bit Width Typical Rangechar 1byte -127 to 127 or 0 to 255unsigned char 1byte 0 to 255signed char 1byte -127 to 127int 4bytes -2147483648 to 2147483647

unsigned int 4bytes 0 to 4294967295signed int 4bytes -2147483648 to 2147483647

short int 2bytes -32768 to 32767unsigned short int Range 0 to 65,535signed short int Range -32768 to 32767long int 4bytes -2,147,483,647 to 2,147,483,647

signed long int 4bytes same as long intunsigned long int 4bytes 0 to 4,294,967,295float 4bytes +/- 3.4e +/- 38 (~7 digits)double 8bytes +/- 1.7e +/- 308 (~15 digits)

long double 8bytes +/- 1.7e +/- 308 (~15 digits)

Page 37: PF Lecture 1&2

#include <iostream.h>main ( ){

int x ;int y ;int z ;x = 10 ;y = 20 ;z = x + y ;cout << " x = " ;cout << x ;cout << " y = " ;cout << y ;cout << " z =x + y = " ;cout << z ;

}

Page 38: PF Lecture 1&2

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.

Arithmetic Operators Relational Operators Logical Operators Bitwise Operators Assignment Operators Misc Operators

Operators

Page 39: PF Lecture 1&2

Plus + Minus - Multiply *

Divide / Modulus %

Arithmetic operators

Page 40: PF Lecture 1&2

i + jx * ya / ba % b

Arithmetic operators

Page 41: PF Lecture 1&2

5 % 2 = 12 % 2 = 0

% = Remainder

Page 42: PF Lecture 1&2

4 / 2 = 25 / 2 = ?

Page 43: PF Lecture 1&2

Highest: ( ) Next: * , / , % Lowest: + , -

Precedence