Programming and computer languages 2
Programming and computer languages 2
Control structures are fundamental constructs in programming that determine the flow of
execution within a program. They dictate how and when certain parts of the code are executed
based on conditions, loops, or branching mechanisms. These structures help developers write
efficient, logical, and reusable code.
---
1. Sequential Control
Additionally, some programming languages support exception handling and function calls as
part of control mechanisms.
---
1. Sequential Control
Sequential control is the simplest form of control structure, where statements are executed one
after another in the order they appear in the code.
Example in Python:
a = 10
b = 20
c = a + b # This statement executes after the previous ones
print(c) # The final statement prints the result
Selection control structures allow the program to make decisions based on conditions. They
include:
a. If Statement
Syntax:
if condition:
# Code to execute if the condition is true
Example:
age = 18
if age >= 18:
print("You are eligible to vote.")
b. If-Else Statement
Executes one block of code if the condition is true and another if it is false.
Example:
marks = 50
if marks >= 40:
print("Pass")
else:
print("Fail")
Example:
score = 75
if score >= 90:
print("Grade: A")
elif score >= 75:
print("Grade: B")
elif score >= 60:
print("Grade: C")
else:
print("Grade: D")
d. Nested If Statements
Example:
x = 10
if x > 5:
if x < 15:
print("x is between 5 and 15")
Used to replace multiple if-elif conditions. Python lacks a built-in switch statement, but it is
available in languages like C, C++, and Java.
Example in C:
#include <stdio.h>
int main() {
int day = 3;
switch (day) {
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
default:
printf("Invalid day");
}
return 0;
}
---
a. For Loop
Example in Python:
b. While Loop
Example:
x=0
while x < 5:
print(x)
x += 1
Example in C++:
#include <iostream>
using namespace std;
int main() {
int x = 0;
do {
cout << x << endl;
x++;
} while (x < 5);
return 0;
}
d. Nested Loops
Example in Python:
for i in range(3):
for j in range(2):
print(f"i={i}, j={j}")
for i in range(10):
if i == 5:
break # Stops the loop when i is 5
print(i)
for i in range(5):
if i == 2:
continue # Skips iteration when i is 2
print(i)
---
Exception handling helps manage runtime errors and prevents program crashes.
Example in Python:
try:
num = int(input("Enter a number: "))
print(10 / num)
except ZeroDivisionError:
print("Cannot divide by zero!")
except ValueError:
print("Invalid input! Please enter a number.")
finally:
print("Execution completed.")
---
Functions help modularize code, and their execution alters control flow.
Example in Python:
def greet(name):
return f"Hello, {name}!"
print(greet("Alice"))
Example of Recursion:
def factorial(n):
if n == 1:
return 1
return n * factorial(n - 1)
---
Conclusion
Control structures are crucial in programming, allowing developers to make decisions, repeat
operations, handle errors, and organize code efficiently. Understanding these structures enables
writing better, more efficient programs across various programming languages.
---
Low-Level Languages: Closer to machine code, offering high performance but requiring detailed
memory and hardware management (e.g., Assembly, C).
High-Level Languages: Abstracted from hardware, easier to read and write, with automatic
memory management (e.g., Python, Java).
---
Compiled Languages: Convert source code into machine code before execution, making them
faster (e.g., C, C++).
Interpreted Languages: Execute code line by line using an interpreter, which makes debugging
easier but execution slower (e.g., Python, JavaScript).
Some languages, like Java, use a hybrid model (compilation to bytecode, then interpretation by
a virtual machine).
---
A. General-Purpose Languages
These languages can be used for various applications, from system programming to web
development.
1. C
Paradigm: Procedural
Compiled: Yes
Use Cases: System programming, embedded systems, operating systems, game development
Key Features:
Fast execution
Example Code:
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
2. C++
Compiled: Yes
Key Features:
Example Code:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!";
return 0;
}
3. Python
Interpreted: Yes
Key Features:
Dynamic typing
Example Code:
print("Hello, World!")
4. Java
Paradigm: Object-Oriented
Key Features:
5. C#
Paradigm: Object-Oriented
Key Features:
Example Code:
using System;
class Program {
static void Main() {
Console.WriteLine("Hello, World!");
}
}
---
These languages are commonly used for frontend and backend web development.
6. JavaScript
Key Features:
Example Code:
console.log("Hello, World!");
7. TypeScript
Key Features:
Static typing
Example Code:
8. PHP
Interpreted: Yes
Use Cases: Web development (Backend - WordPress, Laravel)
Key Features:
Server-side scripting
Embedded in HTML
Example Code:
<?php
echo "Hello, World!";
?>
9. Ruby
Paradigm: Object-Oriented
Interpreted: Yes
Key Features:
Example Code:
---
Example Code:
11. Perl
Interpreted: Yes
Example Code:
---
12. R
Interpreted: Yes
Example Code:
print("Hello, World!")
13. Julia
Example Code:
println("Hello, World!")
---
14. Haskell
Paradigm: Functional
Compiled: Yes
Example Code:
15. Lisp
Interpreted: Yes
Example Code:
---
Conclusion
Programming languages differ based on their paradigms, execution methods, and use cases.
Choosing the right language depends on the application domain, performance requirements,
and ease of development. Some languages, like Python and JavaScript, are widely used across
multiple domains, while others, like R and Haskell, are more specialized.