Skip to content

Lesson 1: Conditional statements

What Are Conditional Statements?

Conditional statements are a fundamental concept in programming that allows your code to make decisions based on certain conditions. They help your program choose different paths or actions depending on whether specific conditions are met or not.

In programming, there are primarily two types of conditional statements: if statements and if-else statements.

If Statements

An if statement is used to execute a block of code only if a specified condition is true. Here’s a basic structure of an if statement:

1. Start
2. If (condition) Then
3.    # Code to execute if the condition is true
4. End If
5. End

For example, if you want to check if a number is positive, you can use an if statement:

1. Start
2. Input a number (num)
3. If num > 0 Then
4.    Display "The number is positive"
5. End If
6. End

In this example, the code inside the if block will execute because the condition num > 0 is true.

If-Else Statements

An if-else statement is used when you want to execute one block of code if a condition is true and a different block of code if the condition is false. Here’s the structure of an if-else statement:

1. Start
2. If (condition) Then
3.    # Code to execute if the condition is true
4. Else
5.    # Code to execute if the condition is false
6. End If
7. End

For example, let’s check if a number is even or odd:

1. Start
2. Input a number (num)
3. If num % 2 == 0 Then
4.    Display "The number is even"
5. Else
6.    Display "The number is odd"
7. End If
8. End

Understanding the “%” Symbol

The “%” symbol in programming represents the modulo (or modulus) operator. It is used to find the remainder when one number is divided by another. For example:

  • 7 % 3 = 1, because when you divide 7 by 3, you get a quotient of 2 and a remainder of 1.
  • 10 % 2 = 0, because when you divide 10 by 2, there is no remainder.

The modulo operator is often used in conditional statements to check for divisibility or to perform tasks that involve repeating patterns.

Let’s check out some more examples

Example 1: Determine if a Number is Positive or Negative

1. Start
2. Input a number (num)
3. If num > 0 Then
4.    Display "The number is positive"
5. Else
6.    Display "The number is negative"
7. End If
8. End

Example 2: Find the Maximum of Three Numbers

1. Start
2. Input the first number (num1)
3. Input the second number (num2)
4. Input the third number (num3)
5. Set max = num1
6. If num2 > max Then
7.    Set max = num2
8. End If
9. If num3 > max Then
10.    Set max = num3
11. End If
12. Display "The maximum number is: ", max
13. End

Example 3: Determine If a Year is a Leap Year

1. Start
2. Input a year (year)
3. If (year is divisible by 4 and not divisible by 100) OR (year is divisible by 400) Then
4.    Display "It's a leap year"
5. Else
6.    Display "It's not a leap year"
7. End If
8. End

Example 4: Calculate the Absolute Value of a Number

1. Start
2. Input a number (num)
3. If num < 0 Then
4.    Set num = -num
5. End If
6. Display "The absolute value is: ", num
7. End

Example 5: Determine If a Number is Even or Odd

1. Start
2. Input a number (num)
3. If num % 2 == 0 Then
4.    Display "The number is even"
5. Else
6.    Display "The number is odd"
7. End If
8. End