Conditionals are programming constructs that execute different code blocks based on whether a condition is true or false, with common types including `if`, `else`, `else if`, and `switch` statements. They enable decision-making in programs, improve efficiency by reducing repetitive code, and provide flexibility for varying actions based on specific conditions. Examples in JavaScript, Python, and C# illustrate the use of these statements in practice.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
1 views
Programming concepts class2
Conditionals are programming constructs that execute different code blocks based on whether a condition is true or false, with common types including `if`, `else`, `else if`, and `switch` statements. They enable decision-making in programs, improve efficiency by reducing repetitive code, and provide flexibility for varying actions based on specific conditions. Examples in JavaScript, Python, and C# illustrate the use of these statements in practice.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2
### What Are Conditionals?
Conditionals evaluate expressions and execute different blocks of code depending on
whether a condition is **true** or **false**. The most common conditional statements are: - `if` statements - `else` statements - `else if` statements (or `elif` in some languages) - `switch` statements (in languages like JavaScript and C#)
### Basic `if` Statement
The simplest form of a conditional is the `if` statement. It checks whether a condition is met and executes the associated code if **true**.
#### Example in JavaScript:
```javascript let temperature = 20; if (temperature > 25) { console.log("It's a hot day!"); } ``` Since `temperature` is **20**, and **not** greater than 25, the message won�t be displayed.
### `if-else` Statement
An `if-else` statement provides an alternative action when the condition is **false**.
#### Example in Python:
```python age = 18 if age >= 21: print("You can legally drink alcohol in the US.") else: print("You are not old enough.") ``` Since `age` is **18**, the program prints `"You are not old enough."`
### `else if` Statements
The `else if` structure lets you check multiple conditions in sequence.
#### Example in C#:
```csharp int score = 75; if (score >= 90) { Console.WriteLine("Grade: A"); } else if (score >= 80) { Console.WriteLine("Grade: B"); } else if (score >= 70) { Console.WriteLine("Grade: C"); } else { Console.WriteLine("Grade: F"); } ``` Since `score` is **75**, the program outputs `"Grade: C"`.
### `switch` Statements
In some languages, `switch` statements offer an alternative to multiple `else if` conditions. #### Example in JavaScript: ```javascript let day = "Monday"; switch (day) { case "Monday": console.log("Start of the week!"); break; case "Friday": console.log("Almost the weekend!"); break; default: console.log("Just another day."); } ``` Since `day` is `"Monday"`, it prints `"Start of the week!"`
### Why Are Conditionals Important?
- **Decision Making:** Programs can react dynamically to user input or data. - **Efficiency:** Reduces repetitive code and enables cleaner logic. - **Flexibility:** Allows different actions based on specific circumstances.