Isp Lesson 5 Share
Isp Lesson 5 Share
CONCEPTS
(WORKING WITH DATA -
VARIABLES, DATA TYPES AND
OPERATORS)
The concept of variables
• A variable in programming is a named storage location in memory
that holds a value. Variables are fundamental to programming because
they allow developers to store, manipulate, and retrieve data
dynamically during the execution of a program. The value stored in a
variable can change during the program's execution, hence the term
"variable."
Data Types
• Data types define the type of data that a variable can hold. Each
programming language supports a set of built-in data types, and they
determine the operations that can be performed on the data. Common
data types include:
Data Types
• Integer (int): Represents whole numbers (e.g., 5, -10, 100).
• Floating-Point (float, double): Represents decimal numbers (e.g., 3.14, -0.001).
• Character (char): Represents a single character (e.g., 'a', 'Z', '$').
• String (str): Represents a sequence of characters (e.g., "Hello, World!").
• Boolean (bool): Represents a true or false value (e.g., true, false).
• Array/List: Represents a collection of elements of the same data type.
• Object/Class: Represents complex data structures with properties and methods.
Data Types
• The choice of data type affects the amount of memory allocated to the
variable and the range of values it can hold.
Identifiers
An identifier is a name given to a variable, function, class, or other
programming elements. Identifiers must follow specific rules, which vary by
programming language, but generally include:
• Must start with a letter (a-z, A-Z) or an underscore (_).
• Can contain letters, digits (0-9), and underscores.
• Cannot be a reserved keyword (e.g., int, if, return).
• Are case-sensitive in most languages (e.g., myVar and myvar are
different).
Examples of valid identifiers: age, _count, totalAmount.
Declaration of variables
• Variable declaration is the process of defining a variable by specifying
its name and data type. In some languages, you can also initialize the
variable with a value at the time of declaration.
• int age; // Declaration
• age = 25; // Initialization
• Or combined:
• int age = 25; // Declaration and initialization
Declaration of variables
• In dynamically typed languages (e.g., Python), you don't need to
declare the data type explicitly:
• python
• age = 25 # Python automatically infers the type as int
Variable Scope
• Variable scope refers to the region of the program where a variable is
accessible. There are typically two types of scope:
Variable Scope
Local Scope: A variable declared inside a function or block is
accessible only within that function or block.
• void myFunction() {
• int localVar = 10; // Local variable
• printf("%d", localVar); // Accessible here
•}
• // printf("%d", localVar); // Error: localVar is not accessible here
Variable Scope
Global Scope: A variable declared outside all functions is accessible throughout the entire
program.
• int globalVar = 20; // Global variable
•
• void myFunction() {
• printf("%d", globalVar); // Accessible here
• }
Understanding scope is crucial for avoiding naming conflicts and managing memory
efficiently.
Operators (types and descriptions)
• Operators are symbols that perform operations on variables and
values. They are essential for performing calculations, comparisons, and
logical decisions in a program.
Types of Operators
Arithmetic Operators: Perform mathematical operations.
o + (Addition)
o - (Subtraction)
o * (Multiplication)
o / (Division)
o % (Modulus, remainder after division)
o ++ (Increment)
o -- (Decrement)
Example of Arithmetic Operation
• Example:
• int a = 10, b = 3;
• int sum = a + b; // 13
• int remainder = a % b; // 1
Types of Operators
Relational Operators: Compare two values.
o == (Equal to)
o != (Not equal to)
o > (Greater than)
o < (Less than)
o >= (Greater than or equal to)
o <= (Less than or equal to)
Example of Relational Operation
• Example:
• if (a > b) {
• printf("a is greater than b");
•}
Types of Operators
o The program starts by assigning values to variables: price_of_item, quantity, and tax_rate.
o The program calculates the subtotal by multiplying the price_of_item by the quantity.
o The program calculates the tax by multiplying the subtotal by the tax_rate.
o The program calculates the total cost by adding the subtotal and the tax.
• IF (condition2) THEN
• ELSE
• END IF
• ELSE
• END IF
• Example:
• IF (marks >= 90) THEN
• PRINT("Grade: A")
• ELSE IF (marks >= 80) THEN
• PRINT("Grade: B")
• ELSE IF (marks >= 70) THEN
• PRINT("Grade: C")
• ELSE
• PRINT("Grade: D")
• END IF
• This checks multiple conditions to determine the grade based on the marks.
switch Statement
• The switch statement is used to select one of many code blocks to
execute based on the value of a variable or expression. It is an
alternative to multiple if..else statements.
• Pseudocode:
• SWITCH (expression) DO
• CASE value1:
• // Code to execute if expression == value1
• BREAK
• CASE value2:
• // Code to execute if expression == value2
• BREAK
• DEFAULT:
• // Code to execute if expression does not match any case
• END SWITCH
• Example:
• SWITCH (day) DO
• CASE 1:
• PRINT("Monday")
• BREAK
• CASE 2:
• PRINT("Tuesday")
• BREAK
• CASE 3:
• PRINT("Wednesday")
• BREAK
• DEFAULT:
• PRINT("Invalid day")
• If day is 1, "Monday" is printed. If day is 2,
"Tuesday" is printed, and so on. If day does not match
• END SWITCH
any case, "Invalid day" is printed.
Summary of Conditional Flow Structures:
1. if: Executes a block of code if a condition is true.
2. if..else: Executes one block of code if a condition is true and another if it is
false.
3. Nested if..else: Allows checking multiple conditions in a hierarchical
manner.
4. switch: Selects one of many code blocks to execute based on the value of
an expression.
These structures are fundamental to controlling the flow of a program and
making it dynamic and responsive to different inputs and conditions.
Iterative flow
• Iterative flow, also known as looping, is a fundamental concept in
programming that allows a set of instructions to be executed repeatedly
until a specific condition is met. Iteration is useful for performing
repetitive tasks, processing collections of data, or implementing
algorithms that require repeated steps. There are three common types of
loops in programming: for, while, and do-while. Each has its own use
case and syntax, but they all serve the purpose of repeating code
execution.
For Loop
• The for loop is used when the number of iterations is known in
advance. It typically consists of three parts:
• Initialization: Sets the starting value of the loop variable.
• Condition: Specifies the condition under which the loop continues to
execute.
• Update: Modifies the loop variable after each iteration.
For Loop
• Pseudocode Example:
• FOR i = 1 TO 5
• PRINT "Iteration: " + i
• END FOR
For Loop
• Explanation:
• The loop starts with i = 1.
• It checks if i <= 5. If true, the loop body executes.
• After each iteration, i is incremented by 1 (i++).
• The loop stops when i becomes greater than 5.
For Loop
• Output:
• Iteration: 1
• Iteration: 2
• Iteration: 3
• Iteration: 4
• Iteration: 5
While Loop
• The while loop is used when the number of iterations is not known in
advance, and the loop continues as long as a specified condition is true.
The condition is evaluated before each iteration.
While Loop
• Pseudocode Example:
• SET counter = 1
• WHILE counter <= 5
• PRINT "Iteration: " + counter
• counter = counter + 1
• END WHILE
While Loop
• Explanation:
• The loop checks if counter <= 5. If true, the loop body executes.
• After each iteration, counter is incremented by 1.
• The loop stops when counter becomes greater than 5.
While Loop
• Output:
• Iteration: 1
• Iteration: 2
• Iteration: 3
• Iteration: 4
• Iteration: 5
Do-While Loop
• The do-while loop is similar to the while loop, but it guarantees that
the loop body is executed at least once, even if the condition is false
initially. The condition is evaluated after each iteration.
Do-While Loop
• Pseudocode Example:
• SET counter = 1
• DO
• PRINT "Iteration: " + counter
• counter = counter + 1
• WHILE counter <= 5
Do-While Loop
• Explanation:
• The loop body executes first, regardless of the condition.
• After each iteration, the condition counter <= 5 is checked.
• If the condition is true, the loop repeats; otherwise, it stops.
• The loop stops when counter becomes greater than 5.
Do-While Loop
• Output:
• Iteration: 1
• Iteration: 2
• Iteration: 3
• Iteration: 4
• Iteration: 5
Key Differences Between the Loops
Loop Type When to Use Condition Guaranteed
Check Execution