Program instructions execution, variables and their types, functions
Program instructions execution, variables and their types, functions
---
Program execution is the process where a computer follows a sequence of instructions written
in a programming language. These instructions are executed step by step by the CPU (Central
Processing Unit) according to the program logic. The execution process typically involves:
Compiled Languages (C, C++, Java): The entire code is translated into machine code before
execution. The CPU then executes this machine code.
Interpreted Languages (Python, JavaScript): The code is executed line by line by an interpreter.
Sequential Execution: The instructions are executed one after another in the order they appear.
Conditional Execution: Based on conditions (if-else statements), the program decides which
instructions to execute.
Loop Execution: Repeats a set of instructions multiple times (for, while loops).
3. Execute: Performs the operation specified (e.g., arithmetic calculation, memory access).
---
A variable is a named storage location in memory used to hold data that can change during
program execution.
x = 10 # Integer variable
name = "Alice" # String variable
Data types define the kind of values a variable can store. Common types include:
2.3 Type Conversion
x = 10
y = float(x) # Converts integer to float (10.0)
---
3. Functions
A function is a reusable block of code that performs a specific task. Functions help in code
modularity and reusability.
def greet(name):
return f"Hello, {name}!"
print(greet("Alice"))
def square(num):
return num * num
def my_function():
local_var = "I am local"
print(local_var)
print(global_var) # Accessible
# print(local_var) # Error: Not accessible outside function
---
Conclusion
Program instructions execute in sequence unless control structures (loops, conditions) alter the
flow.
Functions help organize code and allow reusability with parameters and return values.