Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • break and next keyword

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 158
    Comment on it

    If i want to break our loop at specific condition then it is same as other languages .
    In Ruby continue doesn't work , in this there is equivalent keyword i.e next

    class Number
      def looping(d)
        while(d!=50)
         puts d
         d = d + 1  // doesn't support d++ 
        end
      end
     def brk(d)
        while(d > 20)
          d  = d - 1
          if d == 60
            next
          end  
          puts d    
        end
      end
    end
    b = Number.new
    b.looping(1)
    b.brk(70)
    

    Output :

    b.looping(1) // in this i use break keyword at d == 50 , so at 49 it exit the loop
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    
    
    
    
     b.brk(70) // in this i apply the continue keyword at d == 60 so it skip 60 , you can see in below output
    69
    68
    67
    66
    65
    64
    63
    62
    61
    59
    58
    57
    56
    55
    54
    53
    52
    51
    50
    49
    48
    47
    46
    45
    44
    43
    42
    41
    40
    39
    38
    37
    36
    35
    34
    33
    32
    31
    30
    29
    28
    27
    26
    25
    24
    23
    22
    21
    20
    
    

 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: