How to write number pattern programs in VBScript? UFT/QTP.

Number pattern programs in VBScript, UFT/QTP

Number Pattern programs are not only common in an UFT/QTP Interview but, the logic that is implemented would come very handy while designing the scripts using descriptive programming.In this post, we will look at some of the YogaAsanas 🙂

Number Pattern: YogaAsana1

1
12
123
1234
12345
123456
1234567
12345678
123456789
12345678910

Script

For i = 1 to 10 
    Yoga1 = Yoga1 & i
    Print Yoga1 
Next

How does this work?

  • For loop (variable i) is set from 1 to 10 as the numbers are from 1 to 10 in the pattern.
  • Variable ‘Yoga1’ is not set to any value initially which means its value is Null.
  • ‘&’ is used to concatenate(combine) the variable values.

Number Patterns in UFT QTP

Step1:
Yoga = Yoga & i

  • Yoga1 = null & 1 which gives output as 1
  • Step2:

  • i is incremented to 2 and Yoga1= 1 from previous step.
  • Yoga1 = 1 & 2 which gives output as 12
  • Step3:

  • i is incremented to 3 and Yoga1= 12 from previous step.
  • Yoga1 = 12 & 3 which gives output as 123 and the same way for other steps.
  • Number Pattern: YogaAsana2

    12345
    1234
    123
    12
    1

    Script

    For i = 5 To 1 Step -1
        For j = 1 To i
            Yoga2 = Yoga2 & j
        Next
        Print Yoga2
        Yoga2 = ""
    Next
    

    How does this work?

    Similar to the above pattern. One addition is that Yoga2 variable is set to blank at the end and two loops i and j are used

    .
    Number Patterns in UFT QTP

    Step1:

    • i is set to 5 and it enters loop J which is 1 to 5 (as i=5).
    • For j=1 the output is 1, The for loop repeats until j=5 which means we get output 12345 and j loops ends.
    • The Yoga2 variable is set to Blank “”

    Step2:

    • i is decremented and it enters loop j which is 1 to 4 now.
    • The ouput is 1234 and loop ends. Variable Yoga2 is set to Blank””. The same applies to next steps.

    Questions for you?

    We now know, couple of basic programs. Why not try some new programs. Try on your own and provide your answers in the comments section below.

    YogaAsana3

    5
    54
    543
    5432
    54321

    YogaAsana 4

    54321
    5432
    543
    54
    5

    Clues:

    I know that, you are smart enough to skip this section without answering. Here are some of the clues. Please provide your answers in the comments section below.

    For i = 5 To 1 Step -1
        
    
    Next
    
    -----------------------
    
    For i = 5 To 1 Step -1
        For j = i To 5-i+1 Step -1
           
        Next
        
    Next
    
    

    These programs might look easy. But, very useful when you are scripting. Practice them well and try to frame your own patterns. In the next post we will look at the star patterns.