Introduction to Programming
Arithmetic Operators and
conditional if
Lecture 5
Data types
• You can assign different types of values to a variable such as a number or a
string. In JavaScript, there are two categories of data types :
4/7/2021 Introduction to Programming - Prepared by Nourhan Hamdi 2
Primitive Data Types
Type Description Example
Integer A whole number 7, -23
Float A floating point number 7.68, -23.45
String A sequence of characters "Hello", "abc123@#$"
Boolean Either true or false true, false
4/7/2021 Introduction to Programming - Prepared by Nourhan Hamdi 3
JavaScript Arithmetic Operators
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (Division Remainder)
++ Increment
-- Decrement
4/7/2021 Introduction to Programming - Prepared by Nourhan Hamdi 4
JavaScript Arithmetic Operators
• In JavaScript, the equal sign = is an assignment operator
• The numbers in an arithmetic operation are called operands.
• The operation to be performed between the two operands is defined by an operator.
• Operator precedence describes the order in which operations are performed in an
arithmetic expression.
()
*/%
+-
• Parentheses Have the Highest Precedence.
• Operators with higher precedence execute before those with lower precedence.
• When multiple operators have the same precedence, they are evaluated from left
to right.
Increment or Decrement Operators
• These operators increase (++) or decrease (--) a variable's value by 1.
• They can be used before (prefix) or after (postfix) a variable.
• Used in loops to increment or decrement counters.
Operators and Operands
• The numbers in an arithmetic operation are
called operands.
• The operation to be performed between the
two operands is defined by an operator.
• X=5*5;
4/7/2021 Introduction to Programming - Prepared by Nourhan Hamdi 7
Remainder
Programming (modulus operator):
In most programming languages, the remainder or modulus operator (%) gives the
remainder of a division.
Example: 17 % 5 → 2
Shorthand Operators
• Shorthand operators simplify expressions by combining an arithmetic,
bitwise, or logical operation with assignment in a single step.
Concatenation Operator (+)
• Concatenation is the process of combining two or more strings, or a
string with a number, into a single value.
• In JavaScript, the + operator is used for string concatenation.
• Basic String Concatenation:
• Concatenation with variables:
•
Comparison Operators
Comparison operators are used to compare two values.
Operator Description
== Checks if the value of the two operands are equal
or not, if yes,the
condition becomes true.
!= Checks if the value of the two operands are equal or not, if values are
not equal, then the condition becomes true.
> Checks if the value of the left operand is greater than the value of the
right
operand, if yes, then the condition becomes true.
< Checks if the value of the left operand is less than the value of the
right operand, if yes, then the condition becomes true.
>= Checks if the value of the left operand is greater than or equal the
value of the right operand, if yes, then the condition becomes true.
<= Checks if the value of the left operand is less than or equal the value of
the right operand, if yes, then the condition becomes true.
Conditional Statements
• Conditional statements in JavaScript control the program's flow by
deciding whether specific blocks of code should be executed based on a
given condition.
• Types of conditional statements in JavaScript:
if statement: executes a block of code if a specified condition is true.
if-else statement: runs one block of code if the condition is true and another if it
is false.
else if statements: evaluates multiple conditions sequentially, executing the
first matching block.
switch statement: executes different code blocks based on a variable’s value by
matching it to predefined cases. If no match is found, the default case runs.
JavaScript Logical Operators: && (AND) and ||
(OR)
• Logical operators are used to combine multiple conditions and return a
Boolean (true or false) result.
Operator Symbol Description
AND && Returns true only if
both conditions are
true. If either
condition is false, it
returns false.
OR || Returns true if at
least one condition is
true. Returns false only
if both conditions are
false.
if statement
• The if statement is used to execute a block of code **only if a specified
condition is true**.
if (condition)
{
// Code to execute if condition is true
}
• If age is 18 or greater, the message is displayed. If age is less than 18,
nothing happens.
if else statement
• The if-else statement is used to execute one block of code if a
condition is true, and another block if the condition is false.
if (condition)
{
// code to execute if condition is true
}
else
{
// code to execute if condition is false
}
• If temperature is greater than 25, it prints "It's a hot day! “ Otherwise, it
prints "It's a cool day."
else if statements
• The else if statement is used to check
multiple conditions in sequence
executing the first matching block.
if (condition1) {
// code to be executed if condition1 is true
} else if (condition2) {
// code to be executed if the condition1 is
false and condition2 is true
} else {
// code to be executed if the condition1 is
false and condition2 is false
}