---
# System prepended metadata

title: COHORT III WEEK 3

---

DAY 1

WRITING CONDITIONAL STATEMENTS IN SHELL SCRIPTING

INTRODUCTION
When writing a code, there are decisions that needs to be made base on some certain conditions. These conditions are expressions that evaluates to a boolean expression which could either be True or False.

WHAT IS A CONDITIONAL STATEMENT?
A conditional statement is a statement that helps to execute some certain code branches based on some certain conditions.

Conditional statements can be written using;
1. if
2. else
3. elif (else if)

<strong>if</strong>
Using an if statement only: if...then...fi
When you are using a single if statement, the syntax is as follows:

    if [ condition ];
    then
        statement
    fi
    
    Note that the spaces are part of the syntax and should not be removed.
    
<strong>else</strong>
Using an if with an else statement: if...then...else...fi statements

When you are using an if statement and you want to add another condition, the syntax is as follows:

    if [ condition ]
    then
        statement
    else
        do this by default
    fi

<strong>elif (else if)</strong>
Using multiple else statements with if: if..elif..else..fi

Think of a scenario where we want to add further conditions and comparisons to make the code dynamic. In this case, the syntax would look like this:

    if [ condition ]
    then
        statement
    elif [ condition ] 
    then
        statement 
    else
        do this by default
    fi

To create meaningful comparisons, we can use AND -a && and OR -o || as well.

For example, lets apply the if, else and elif in a grading system.

where;
Score 70 - 100 Grade A
Score 60 - 69 Grade B
Score 50 - 59 Grade C
Score 40 - 49 Graade D
Score 30 - 39 Grade E
Score 0 - 29 Grade F

![Screenshot from 2025-06-23 20-35-21](https://hackmd.io/_uploads/ByAIa7PEle.png)

Result

![Screenshot from 2025-06-23 20-38-04](https://hackmd.io/_uploads/rk4daXwVgx.png)

SOME BASIC TERMINOLOGIES TO KNOW WHEN WRITING CONDITIONAL STATEMENTS IN SHELL SCRIPTING

<strong>=</strong> is an assignment operator use to assign a value to a variable
<strong>==</strong> is called equality operator which is use to check two things that are equal
<strong>> OR -gt</strong> greater than
<strong>< OR -lt</strong> less than
<strong>>=, -ge</strong> greater than or equals to
<strong><= OR -le</strong> less than or equals to
<strong>!</strong> not, negation operator, inverse, opposite
<strong>!=</strong> not equals to, inequality operator
<strong>|| OR -O</strong> symbol for OR, logical Add
<strong>&& OR -a</strong> symbol for AND, logical multiplication
    
DAY 2

LOOPS IN BASH SCRIPTING

Loops allow us to take a series of commands and keep re-running them until a particular situation is reached. They are useful for automating repetitive tasks.

Loops are programs that execute continously until a certain condition is certified or met.

They are categorised into three (3)
1. while loop
2. for loop
3. until loops

<strong>while loop</strong>
The while loop is one of the easiest loop to work with. Here, while an expression is true, the lines of code keeps executing.

SYNTAX
    
    while [ <condition> ]
    do
        <block of code>
    done
            
While loop can also be executed by declaring a boolean variable
For example;

    SHOULD_EXECUTE=true
    
    while $SHOULD_EXECUTE;
    do
        SHOULD_EXECUTE=false
    done
            
Lets take some examples by printing numbers from 1 to 10 using while

![Screenshot from 2025-06-25 20-55-46](https://hackmd.io/_uploads/BJktVAKNgx.png)

Result
            
![Screenshot from 2025-06-25 20-56-27](https://hackmd.io/_uploads/H17c4AFVxx.png)

Lets now try to print numbers from 10 down to 1
            
![Screenshot from 2025-06-25 20-58-15](https://hackmd.io/_uploads/H1VxSCKExl.png)

            
Result
            
![Screenshot from 2025-06-25 20-58-25](https://hackmd.io/_uploads/BkNmHCFNgg.png)
            
Lets try using while loop by declaring a boolean variable
            
![Screenshot from 2025-06-25 21-02-55](https://hackmd.io/_uploads/HJwLI0Y4lg.png)

Result
            
![Screenshot from 2025-06-25 21-03-09](https://hackmd.io/_uploads/rJoOIAFNee.png)

            
DAY 3

Was all about Task! Task!! Task!!!
            
We tried to print Odd and Even numbers and also print the sum of Odd an Even numbers.
            
![Screenshot from 2025-06-29 16-40-06](https://hackmd.io/_uploads/H1_Qy11Sxx.png)
            
RESULT
            
![Screenshot from 2025-06-29 16-40-40](https://hackmd.io/_uploads/HkSqyJkSlg.png)

            

            





