Lecture 6 – Programming Fundamentals, Data Types, Arrays & Operators

Introduction

In the previous lecture, we learned:

  • CSS Float
  • Clear Property
  • Border Radius
  • Introduction to JavaScript
  • JavaScript Events
  • Functions and Alerts

Before writing programs in JavaScript, PHP, or any other language, it is important to understand the basic concepts of programming.

In this lecture, we will learn:

  1. Control Structure
  2. Loop Structure
  3. Functions
  4. Variables and Data Types
  5. Arrays
  6. Operators
  7. Arithmetic Operators
  8. Logical Operators

What is Programming?

Programming is the process of giving instructions to a computer.

Just like humans follow instructions, computers also follow instructions written by programmers.

Example:

Take Input
Process Data
Display Output

Every programming language follows this basic flow.


1. Control Structure

A Control Structure controls the flow of a program.

It helps the program make decisions.

Example

If age is greater than 18
    Allow Entry
Else
    Deny Entry

The program decides which path to follow.


Why Use Control Structures?

Control structures help us:

  • Make decisions
  • Execute specific code
  • Control program flow

Common Control Structures

If

If condition is true
    Execute Code

If Else

If condition is true
    Execute Code A
Else
    Execute Code B

Switch

Check Value
Execute Matching Option

2. Loop Structure

Loops are used when we want to repeat a task multiple times.

Instead of writing the same code again and again, we use loops.


Example

Without Loop

Print Hello
Print Hello
Print Hello
Print Hello
Print Hello

Using Loop

Repeat 5 Times
    Print Hello

Why Use Loops?

Loops help:

  • Reduce code
  • Save time
  • Improve efficiency

Common Loops

For Loop

Used when the number of repetitions is known.

Repeat 10 Times

While Loop

Runs while a condition remains true.

While age < 18

Do While Loop

Runs at least one time before checking the condition.

Do
    Execute Code
While Condition

3. Functions

A function is a reusable block of code.

Instead of writing the same code repeatedly, we place it inside a function.


Why Functions?

Functions help:

  • Reduce repetition
  • Organize code
  • Improve readability
  • Simplify maintenance

Function Structure

Function Name
{
    Statements
}

Example

function welcome()
{
    alert("Welcome Student");
}

Whenever the function is called, the code inside it executes.


4. Variables and Data Types

A variable is a container used to store data.

Example:

Name = Ali
Age = 20
Gender = Male

Important Rule

Every variable has a data type.

The data type tells the computer what kind of information is being stored.


5. Data Types

A data type defines the nature of data.


Integer (int)

Stores whole numbers.

Example

10
20
500
1000

Syntax

int age;

Float

Stores decimal numbers.

Example

10.5
25.75
100.25

Syntax

float salary;

Double

Stores large decimal numbers with higher precision.

Example

double amount;

Character (char)

Stores a single character.

Example

'A'
'B'
'M'

Syntax

char gender;

Boolean

Stores only two values.

Values

True
False

Syntax

boolean status;

Variable Declaration Examples

int age;

char gender;

char name[];

Variable Assignment

gender = 'M';

age = 25;

Example

int age;
char gender;

age = 22;

gender = 'M';

6. Arrays

An Array is a collection of homogeneous data.

Homogeneous means all elements should be of the same type.


Why Arrays?

Instead of creating many variables:

student1
student2
student3
student4
student5

We can use one array.

students[]

Array Symbol

[]

Square brackets represent an array.


Example

marks[5]

This array can store five marks.


Example Values

marks[0] = 85

marks[1] = 90

marks[2] = 78

marks[3] = 95

marks[4] = 88

PHP Array Example

$students = array(
    "Ali",
    "Ahmed",
    "Usman",
    "Bilal"
);

Benefits of Arrays

  • Easy data management
  • Less code
  • Better organization
  • Faster processing

7. Operators

Operators are symbols used to perform actions.


Definition

Operators

Symbols that perform actions.

Operands

The values on which actions are performed.


Example

10 + 20

Here:

+

is the Operator.

10 and 20

are Operands.


Types of Operators


A. Unary Operators (U)

Work with one operand.

Example

++a

--a

B. Binary Operators (B)

Work with two operands.

Example

a + b

a - b

C. Ternary Operators (T)

Work with three operands.

Example

condition ? true : false

8. Operator Behaviour Types

Operators are categorized according to their behavior.

Main categories include:

Arithmetic Operators

Used for calculations.

Logical Operators

Used for comparisons and decision making.


9. Arithmetic Operators

Arithmetic operators perform mathematical calculations.

OperatorNameType
+AdditionB
SubtractionB
*MultiplicationB
/DivisionB
%ModulusB
=AssignmentB
^PowerB
++IncrementU
DecrementU
+=Add and AssignB
-=Subtract and AssignB
*=Multiply and AssignB
/=Divide and AssignB

Addition

10 + 5 = 15

Subtraction

10 - 5 = 5

Multiplication

10 * 5 = 50

Division

10 / 5 = 2

Modulus

Returns remainder.

10 % 3 = 1

Increment

Increase value by one.

a++

Example:

a = 5

a++

Result = 6

Decrement

Decrease value by one.

a--

Example:

a = 5

a--

Result = 4

10. Logical Operators

Logical operators are used for comparison and decision making.


Less Than

<

Example:

5 < 10

Result:

True

Greater Than

>

Example:

10 > 5

Result:

True

Less Than or Equal To

<=

Example:

10 <= 10

Result:

True

Greater Than or Equal To

>=

Example:

20 >= 10

Result:

True

Equal To

==

Example:

10 == 10

Result:

True

Not Equal To

!=

Example:

10 != 5

Result:

True

AND Operator

&&

Both conditions must be true.

Example:

Age > 18 && Age < 60

Result:

True

Only if both conditions are true.


OR Operator

||

At least one condition must be true.

Example:

Age < 18 || Age > 60

NOT Operator

!

Reverses the result.

Example:

!(10 > 5)

Result:

False

Summary

In this lecture, we learned:

  • Programming Fundamentals
  • Control Structures
  • Loop Structures
  • Functions
  • Variables
  • Data Types
  • Arrays
  • Operators
  • Arithmetic Operators
  • Logical Operators
  • Unary, Binary and Ternary Operators

These concepts are the foundation of every programming language including JavaScript, PHP, Java, C++, Python, and many others.