Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • control structure

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 179
    Comment on it

    In programming our program must act according to our control structure, it must follow the rules and conditions which are supposed to be follow, for this we use some conditions that is known as conditional flow, by this our program do what we want them to do. In flow control we usually use followings:

    1. conditionals

    2. comparisons

    3. Combining Operation

    4.Ternary operators Conditionals

    In conditionals we give specific conditions to our code that must be followed and if they are matching then program will go further. conditions are formed by using (<, >, <=, >=, ==, !=, &&, ||) they are logical structures and conditions are formed by using if and else statement.

    puts "enter the num which is multiple of 5"
     a = gets.chomp.to_i
     
     if(a==5)
     
     puts "a is 5"
     else
     puts "enter the multiple of 5"
    
     end

    here we are using gets.chomp and to_i is converting it to integer type to take input from user, you can see in above code how we put a condition to pass a user's entered num through if condition, if he enters 5 as input it will print "a is 5" comparisons. In comparisons irb :001 > 4 < 10 => true irb :002 > 4 > 5 => false

     

    In comparison we compare two numbers and according to that it will show result. Combining Operator Combining operator are used where you want your program to pass from two conditions, it is achieved by AND and OR operator.

    AND- And operator is used to join multiple conditions. All conditions must met with &&.

     OR- In OR operator 1 out of 2 conditions need to met.

    irb :001 > (6 == 6) && (5 == 5)
    => true
    
    irb :002 > (4 == 10) && (5 == 5)
    => false
    
    irb :002 > (4 == 9) && (6 == 6)
    => false
    
    

     

    OR- || operator

    irb :001 > (10 == 10) || (5 == 5)
    => true
    
    irb :002 > (0 == 5) || (5 == 5)
    => true
    
    irb :002 > (4 == 5) || (8 == 6)
    => false
    
    

     

    Ternary Operator Ruby has extra option for if and else statement that option is termed as ternery operator it uses the combination of ? and :

    irb :001 > true ? "true" : "not true"
     => "true"
    
    irb :001 > false ? "true" : "not true"
     => "this is not true"
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: