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

Basic Programming Concepts

Imran Rashid
CTO at ManiWeber Technologies

Basic Programming Concepts Imran Rashid


Outline

Program, Programming & Programming Languages

Variables, Constants & Data Types

Operators in Programming

Decision Making in Programming

Iterations in Programming

Basic Programming Concepts Imran Rashid


Program, Programming & Programming Languages
Program, Programming & Programming Languages
Program
A computer program, or just a program, is a sequence of
instructions, written to perform a specified task on a
computer
Programming
Computer programming is the process of writing or
editing source code
A person who practices this skill is referred to as a
computer programmer, software developer, and
sometimes coder
Programming Languages
A programming language is a formal constructed language
designed to communicate instructions to a machine
Basic Programming Concepts Imran Rashid
Variables, Constants & Data Types
Variables, Constants & Data Types
Variables & Constants
Most important concept for problem solving using
computers
All temporary results are stored in terms of variables
o The value of a variable can be changed
o The value of a constant do not change
Where are they stored?
In main memory
How does memory look like (logically)?
As a list of storage locations, each having a unique address
Variables and constants are stored in these storages

Basic Programming Concepts Imran Rashid


Variables, Constants & Data Types

Basic Programming Concepts Imran Rashid


Variables, Constants & Data Types

Basic Programming Concepts Imran Rashid


Variables, Constants & Data Types

Basic Programming Concepts Imran Rashid


Variables, Constants & Data Types
Data Types
Three common data types used:
o Integer: can store only whole numbers
– Examples: 25, -56, 1, 0
– Size: 16 bits & 32 bits
o Floating-point: can store numbers with fractional values
– Examples: 3.14159, 5.0, -12345.345
– Size: 32 bits & 64 bits
o Character: can store a character
– Examples: ‘A’, ‘a’, ‘*’, ‘3’, ‘ ’, ‘+’
– Size: 8 bits & 16 bits

Basic Programming Concepts Imran Rashid


Operators in Programming
Operators in Programming
Arithmetic Operators
Addition (+)
Subtraction (-)
Division (/)
Multiplication (*)
Modulus (%)
Examples
distance = rate * time;
netIncome = income - tax;
speed = distance / time;
area = PI * radius * radius;

Basic Programming Concepts Imran Rashid


Operators in Programming
Suppose x and y are two integer variables, whose values
are 13 and 5 respectively

Basic Programming Concepts Imran Rashid


Operators in Programming
Operator Precedence
In decreasing order of priority
o Parentheses: ( )
o Unary minus: –5
o Multiplication, Division, and Modulus
o Addition and Subtraction
For operators of the same priority, evaluation is from left
to right as they appear
Parenthesis may be used to change the precedence of
operator evaluation

Basic Programming Concepts Imran Rashid


Operators in Programming
Relational Operators
Used to compare two quantities

Basic Programming Concepts Imran Rashid


Operators in Programming
Logical Operators
There are two logical operators (also called logical
connectives)
o && : Logical AND
– Result is true if both the operands are true
o || : Logical OR
– Result is true if at least one of the operands are true

Basic Programming Concepts Imran Rashid


Decision Making in Programming
Decision Making in Programming
Statements and Blocks
An expression followed by a semicolon becomes a
statement
o x = 5;
o y = x + 3;
Braces { and } are used to group declarations and
statements together into a compound statement, or block

Basic Programming Concepts Imran Rashid


Decision Making in Programming
Control Statements: What do they do?
Allow different sets of instructions to be executed
depending on the outcome of a logical test
o Whether TRUE (non-zero) or FALSE (zero)
How do we specify the conditions?
Using relational operators (<, <=, >, >=, ==, !=)
Using logical operators (&&, ||, !)

Basic Programming Concepts Imran Rashid


Decision Making in Programming
The if Statement

The condition to be tested is any expression enclosed in


parentheses
The expression is evaluated, and if its value is non-zero,
the statement is executed

Basic Programming Concepts Imran Rashid


Decision Making in Programming
The if-else Statement

These statements can be nested

Basic Programming Concepts Imran Rashid


Decision Making in Programming
The switch Statement
This causes a particular group of statements to be chosen
from several available groups
o Uses “switch” statement and “case” labels

Basic Programming Concepts Imran Rashid


Iterations in Programming
Iterations in Programming
Loop
Group of instructions that are executed repeatedly while
some condition remains true
Loops can be nested as well
There are 3 main types of loops
o while loop
o do-while loop
o for loop

Basic Programming Concepts Imran Rashid


Iterations in Programming
while loop
The while loop is used to carry out looping operations, in
which a group of statements is executed repeatedly, as
long as some condition remains satisfied

Basic Programming Concepts Imran Rashid


Iterations in Programming
do-while loop
A do while loop is a control flow statement that executes a
block of code at least once, and then repeatedly executes
the block, or not, depending on a given boolean condition
at the end of the block

Basic Programming Concepts Imran Rashid


Iterations in Programming
for loop
The for loop is the most commonly used looping structure in
programming for known iterations

o expr1 (init) : initialize parameters


o expr2 (test): test condition, loop continues if satisfied
o expr3 (update): used to alter the value of the parameters after each
iteration
Basic Programming Concepts Imran Rashid
Any ?

You might also like