Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 43

Programming

Fundamentals
Lecture 01
Introduction

Course Policy

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

Program

A program is a precise sequence of steps to


solve a particular problem.

Alan Perlis Yale


University

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 selfcritical.

Critical Skills
Analysis
Critical Thinking
Attention to Detail

Design Recipe

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.

Computers are

STUPID

Humans are even more.

Skills Needed for Programming

Think Reuse
Think User Interface
Comments liberally

Think Reuse

Area of the Ring


Inner Circle
Outer Circle

Area of Outer Circle

____Area of Inner Circle=

Area of the Ring

Software categories

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

Software
Device Drivers
Utilities

Software categories

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

Tools of Trade

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.

Home work

What is difference between Compiler and interpreter ?

Tools of Trade (cont.)

Editors
It is a tool for writing the code of program.

Debugger
Corrects Logical errors during the execution of program.

Linker
Links the program with routines and functions located on other file.

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

Programming Fundamentals
Lecture 02

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

Basic Syntax

# 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.

<<
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.

Variables

Name of locations in memory for storing data.

Variable
Picture of the memory

25

name
10323 of the
variable

Variable
Variable starts with
1. Character
2. Underscore _ (Not Recommended)

Variable

Small post box

Variables

In a program every variable has

Name
Type
Size
Value

Scope of variable

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.

Local Variable

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;
}

Global variable

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;
}

Assignment Operator

=
x=2
X

Assignment Operator
L.H.S = R.H.S.
X+ 3 = y + 4 Wrong
Z = x +4
x +4 = Z

Wrong

X = 10 ;

10

30

X = 30 ;

X = X + 1;

10

+ 1

= 11

Data types

Adata typedetermines what type of values an object can


have and what operations can be performed.
Reserve words.

Data type

int i ; ->
Declaration line

Data types
Type

Keyword

Boolean

bool

Character

char

Integer

int

Floating point

float

Double floating point

double

Valueless

void

Data types their size and


range
Type

Typical Bit Width

Typical Range

char

1byte

-127 to 127 or 0 to 255

unsigned char

1byte

0 to 255

signed char

1byte

-127 to 127

int

4bytes

-2147483648 to 2147483647

unsigned int

4bytes

0 to 4294967295

signed int

4bytes

-2147483648 to 2147483647

short int

2bytes

-32768 to 32767

unsigned short int

Range

0 to 65,535

signed short int

Range

-32768 to 32767

long int

4bytes

-2,147,483,647 to 2,147,483,647

signed long int

4bytes

same as long int

unsigned long int

4bytes

0 to 4,294,967,295

float

4bytes

+/- 3.4e +/- 38 (~7 digits)

double

8bytes

+/- 1.7e +/- 308 (~15 digits)

long double

8bytes

+/- 1.7e +/- 308 (~15 digits)

#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 ;

Operators

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

Arithmetic operators
Plus
Minus
Multiply

Divide
Modulus

+
*
/
%

Arithmetic operators

i+j
x*y
a/b
a%b

% = Remainder

5%2=1
2%2=0

4/2=2
5/2=?

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

You might also like