In the previous lecture, we learned:
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:
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.
A Control Structure controls the flow of a program.
It helps the program make decisions.
If age is greater than 18
Allow Entry
Else
Deny Entry
The program decides which path to follow.
Control structures help us:
If condition is true
Execute Code
If condition is true
Execute Code A
Else
Execute Code B
Check Value
Execute Matching Option
Loops are used when we want to repeat a task multiple times.
Instead of writing the same code again and again, we use loops.
Without Loop
Print Hello
Print Hello
Print Hello
Print Hello
Print Hello
Using Loop
Repeat 5 Times
Print Hello
Loops help:
Used when the number of repetitions is known.
Repeat 10 Times
Runs while a condition remains true.
While age < 18
Runs at least one time before checking the condition.
Do
Execute Code
While Condition
A function is a reusable block of code.
Instead of writing the same code repeatedly, we place it inside a function.
Functions help:
Function Name
{
Statements
}
function welcome()
{
alert("Welcome Student");
}
Whenever the function is called, the code inside it executes.
A variable is a container used to store data.
Example:
Name = Ali
Age = 20
Gender = Male
Every variable has a data type.
The data type tells the computer what kind of information is being stored.
A data type defines the nature of data.
Stores whole numbers.
10
20
500
1000
int age;
Stores decimal numbers.
10.5
25.75
100.25
float salary;
Stores large decimal numbers with higher precision.
double amount;
Stores a single character.
'A'
'B'
'M'
char gender;
Stores only two values.
True
False
boolean status;
int age;
char gender;
char name[];
gender = 'M';
age = 25;
int age;
char gender;
age = 22;
gender = 'M';
An Array is a collection of homogeneous data.
Homogeneous means all elements should be of the same type.
Instead of creating many variables:
student1
student2
student3
student4
student5
We can use one array.
students[]
[]
Square brackets represent an array.
marks[5]
This array can store five marks.
marks[0] = 85
marks[1] = 90
marks[2] = 78
marks[3] = 95
marks[4] = 88
$students = array(
"Ali",
"Ahmed",
"Usman",
"Bilal"
);
Operators are symbols used to perform actions.
Symbols that perform actions.
The values on which actions are performed.
10 + 20
Here:
+
is the Operator.
10 and 20
are Operands.
Work with one operand.
++a
--a
Work with two operands.
a + b
a - b
Work with three operands.
condition ? true : false
Operators are categorized according to their behavior.
Main categories include:
Used for calculations.
Used for comparisons and decision making.
Arithmetic operators perform mathematical calculations.
| Operator | Name | Type |
|---|---|---|
| + | Addition | B |
| – | Subtraction | B |
| * | Multiplication | B |
| / | Division | B |
| % | Modulus | B |
| = | Assignment | B |
| ^ | Power | B |
| ++ | Increment | U |
| — | Decrement | U |
| += | Add and Assign | B |
| -= | Subtract and Assign | B |
| *= | Multiply and Assign | B |
| /= | Divide and Assign | B |
10 + 5 = 15
10 - 5 = 5
10 * 5 = 50
10 / 5 = 2
Returns remainder.
10 % 3 = 1
Increase value by one.
a++
Example:
a = 5
a++
Result = 6
Decrease value by one.
a--
Example:
a = 5
a--
Result = 4
Logical operators are used for comparison and decision making.
<
Example:
5 < 10
Result:
True
>
Example:
10 > 5
Result:
True
<=
Example:
10 <= 10
Result:
True
>=
Example:
20 >= 10
Result:
True
==
Example:
10 == 10
Result:
True
!=
Example:
10 != 5
Result:
True
&&
Both conditions must be true.
Example:
Age > 18 && Age < 60
Result:
True
Only if both conditions are true.
||
At least one condition must be true.
Example:
Age < 18 || Age > 60
!
Reverses the result.
Example:
!(10 > 5)
Result:
False
In this lecture, we learned:
These concepts are the foundation of every programming language including JavaScript, PHP, Java, C++, Python, and many others.