Select Page

Condition Checking:

Condition checking, also known as conditional statements or control structures, allows programmers to make decisions and execute specific blocks of code based on whether certain conditions are true or false. Common conditional statements include if statements, switch statements, and ternary operators.

  1. If Statement:
    • The if statement evaluates a condition and executes a block of code if the condition is true.
    • Optionally, it can include one or more elif (else if) clauses to check additional conditions.
    • It may also include an else clause to execute a block of code if none of the previous conditions are true.

Example in Python:

python

x = 10

if x > 0:
print(“x is positive”)
elif x < 0:
print(“x is negative”)
else:
print(“x is zero”)

  1. Switch Statement (or its equivalent):
    • The switch statement evaluates an expression and executes a specific block of code depending on the value of the expression.
    • Some programming languages, like Python, do not have a switch statement, but equivalent functionality can be achieved using if-elif-else statements.

Example in C++:

cpp

int day = 3;

switch (day) {
case 1:
cout << “Monday”;
break;
case 2:
cout << “Tuesday”;
break;
// more cases…
default:
cout << “Invalid day”;
}

  1. Ternary Operator:
    • The ternary operator (?:) is a concise way to write conditional expressions with a single line of code.
    • It evaluates a condition and returns one of two values based on whether the condition is true or false.

Example in Java:

java
int x = 10;

String result = (x > 0) ? "Positive" : "Negative";

System.out.println(result);

Looping:

Looping allows programmers to execute a block of code repeatedly until a certain condition is met. Common looping structures include for loops, while loops, and do-while loops.

  1. For Loop:
    • The for loop iterates over a sequence of values (e.g., numbers) or elements (e.g., elements in an array) for a specified number of iterations.
    • It typically consists of an initialization, a condition, and an update expression.

Example in Python:

python
for i in range(5):

print(i)

  1. While Loop:
    • The while loop repeatedly executes a block of code as long as a specified condition is true.
    • It may not execute at all if the initial condition is false.

Example in Java:

java
int i = 0;

while (i < 5) {

System.out.println(i);

i++;

}

  1. Do-While Loop:
    • The do-while loop is similar to the while loop, but it guarantees that the block of code is executed at least once before checking the condition for continuation.

Example in C++:

cpp
int i = 0;

do {

cout << i << endl;

i++;

} while (i < 5);

Structured Data Types:

Structured data types are types that can hold multiple values or elements as a single entity. They include arrays, lists, tuples, dictionaries, structs, classes, and objects. These data types allow programmers to organize and manipulate data more efficiently and effectively.

Example in Python:

python
# List

my_list = [1, 2, 3, 4, 5]

# Tuple
my_tuple = (1, 2, 3, 4, 5)

# Dictionary
my_dict = {‘a’: 1, ‘b’: 2, ‘c’: 3}

# Class
class Person:
def __init__(self, name, age):
self.name = name
self.age = age

# Object
person1 = Person(“Alice”, 30)
person2 = Person(“Bob”, 25)

Structured data types provide a way to store and manipulate collections of related data, enabling more complex and sophisticated programming tasks.