Ruby Programming Tutorial - Lesson 07 - More on Conditional Statements

in ruby •  7 years ago 

ruby.jpg

Nested IF conditions

We are going to learn more about conditional statements in this post.
Nested if conditions means .. if conditions within if conditions ..

for example

## Checking for a person .. if its Bilal Haider .. we tell him, that he is lovely .. 

name        = "Bilal Haider"
balance     = 1000
age         = 27


if name == "Bilal Haider"
    puts " You are cute and lovely :) " 
    if balance <= 500
        puts "Your account balance is Low! "
        print balance
    end

    if age >= 25
        puts "You Should have a girl Friend :) "
    end 
end

if name == "Haider"
    puts " You are cute  :) " 
    if balance <= 500
        puts "Your account balance is Low! "
        print balance
    end

    if age >= 25
        puts "You Should have a girl Friend :) "
    end 
end

Note in this example ... there are two main IF conditions.. which check for a specific person,
if that condition is true.. then it executes the nested IF .. and only if comparison evaluates to true..
This kind of programming is called nested ifs, when we want to check one condition.. if its true.. we check another condition.. then perform certain tasks.

ElSIF conditional statement

Its used when you have more than 1 possibilities, depending upon the possibility a different task needs to be performed.
lets take an example ..

if balance is greater than 2000 withdraw 1000 , if balance is greater than 5000 withdraw 4000 and if balance is less than 500 withdraw nothing

Now you have learned about writing Nested if conditions, and also you have learned to write
ELSIF conditions.. you can write any conditional logic whatsoever .. :)

You need to write some code for practice. and once you master this skill, you can write programs to do amazing things for you.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!