Tuesday, December 9, 2008

Logical Looping Programming v2

Today the topic is print the * like the previous vertion one, but this time it's reverse. ow to print it without using text allingn?


here is the code ^ ^ : ( this time i only put the short one only, coz kinda busy lately) 
Dim i As Integer
        Dim j As Integer
        Dim a As Integer
        TextBox2.Text = ""
        For i = 1 To ((TextBox1.Text * 2) - 1)
            a = IIf(i > TextBox1.Text, (2 * TextBox1.Text) - i, i)
            For j = TextBox1.Text To 0 Step -1
                If j = 0 Then
                    TextBox2.Text = TextBox2.Text & "*" & vbCrLf
                Else
                    If j >= a Then
                        TextBox2.Text = TextBox2.Text & " "
                    Else
                        TextBox2.Text = TextBox2.Text & "*"
                    End If
                End If
            Next
        Next

Hope u enjoy the content ^ ^.
someone ask for exercise. how about print the star in a Diamond shape like this? 

Have  a nice Try.

3 comments:

Jaxcaizaer said...

I DON'T UNDERSTAND THE LOGIC!!!
EVEN THE SIMPLEST ONE......

Can u help me??? Please....

Jie said...

sorry i got a little busy lately.
First have u got the logic for the previous one?
the one to generate:
*
**
***
**
*

first we have to declare a variable for looping.
then we can see that the first one we use 4 looping. and it split in 2 part. part one is to make it print the * like :
*
**
***
it consist of 2 for. first one for row and second to generate the how many * in the row. and if the if the count of * is same with current row we make it down to the next row using vbcrlf(same as enter)
to make the star print reverse * :
**
*
we need to make the for to loop back ward. concept is the same. or from high to low. so we use step -1. and we only need 1 row for the longest *. so we need to minus the total row with 1. the other

Hope u understand the explanation ^ ^

Jaxcaizaer said...

thanx....

now, i understand!!!

Post a Comment