Connecting Tech Pros Worldwide Forums | Help | Site Map

need help in while wend loop help me plz

sweetzhay's Avatar
Newbie
 
Join Date: Sep 2009
Location: Philippines
Posts: 5
#1: Sep 2 '09
this code i will have an output of
1
12
123
1234
12345 when pressing command 1

Private Sub Command1_Click()

Label1.Caption = Clear
sides = 5
rows = 1
While rows <= sides
Column = 0
While Column < rows
Column = Column + 1
Label1 = Label1 & Column & Space(1)
Wend
Label1 = Label1 & vbCrLf
rows = rows + 1
Wend

End Sub

Now my problem is to get the output of
1
21
321
4321
54321
can anyone help me do this plz

and also this one
using 2 labels and 1 command button
on the first label1 when u hit command button u will have this output
2 4 6 8 10
then in label 2
1 3 5 7 9
this is the even and odd project
need help in a code.

Expert
 
Join Date: Jun 2007
Location: Derbyshire, UK
Posts: 347
#2: Sep 2 '09

re: need help in while wend loop help me plz


Hi

The code below is written in VBA.

I think it does what you have described, but not using While/Wend
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Dim sides As Integer
  3.  
  4. Private Sub Command1_Click()
  5.     Dim i As Integer
  6.     Dim j As Integer
  7.  
  8.     Label1.Caption = ""
  9.     sides = 5
  10.     For j = 1 To sides
  11.         For i = 1 To j
  12.             Label1 = Label1 & i & Space(1)
  13.         Next i
  14.         Label1 = Label1 & vbCrLf
  15.     Next j
  16. End Sub
  17.  
  18. Private Sub Command2_Click()
  19.     Dim i As Integer
  20.     Dim j As Integer
  21.  
  22.     Label1.Caption = ""
  23.     sides = 5
  24.     For j = sides - 1 To 0 Step -1
  25.         For i = sides - j To 1 Step -1
  26.             Label1 = Label1 & i & Space(1)
  27.         Next i
  28.         Label1 = Label1 & vbCrLf
  29.     Next j
  30. End Sub
  31.  
  32. Private Sub Command3_Click()
  33.     Dim i As Integer
  34.  
  35.     Label1.Caption = ""
  36.     Label2.Caption = ""
  37.     sides = 10
  38.     For i = 1 To sides
  39.         If i Mod 2 = 0 Then
  40.             Label1 = Label1 & i & Space(1)
  41.         Else
  42.             Label2 = Label2 & i & Space(1)
  43.         End If
  44.     Next i
  45. End Sub
HTH

MTB
sweetzhay's Avatar
Newbie
 
Join Date: Sep 2009
Location: Philippines
Posts: 5
#3: Sep 3 '09

re: need help in while wend loop help me plz


u dont have any idea on how to get it using while wend?

so what if i want to get also this output

'''''''''''''''1
''''''''''''12
''''''''''123
'''''''1234
''''12345

and also this one

'''''''''''''''''''1
'''''''''''''''''123
''''''''''''''12345
'''''''''''1234567
''''''''123456789
this one is the diamond, disregard the '''''''''''''''''''''''


does using FOR is also in looping thanks alot from helping
kadghar's Avatar
Expert
 
Join Date: Apr 2007
Location: Mexico City
Posts: 1,155
#4: Sep 3 '09

re: need help in while wend loop help me plz


actually any FOR can be converted into a While Wend statement

For i = 1 to 5
.....
next i

is the same as
i=1
while i <=5
.....
i=i+1
wend
sweetzhay's Avatar
Newbie
 
Join Date: Sep 2009
Location: Philippines
Posts: 5
#5: Sep 3 '09

re: need help in while wend loop help me plz


i am just a bit confused here coz in while wend i cannot use the STEP like u use in for the step - 1 im a having a hardtime to convert it

For j = sides - 1 To 0 Step -1
Expert
 
Join Date: Jun 2007
Location: Derbyshire, UK
Posts: 347
#6: Sep 3 '09

re: need help in while wend loop help me plz


Quote:

Originally Posted by sweetzhay View Post

i am just a bit confused here coz in while wend i cannot use the STEP like u use in for the step - 1 im a having a hardtime to convert it

For j = sides - 1 To 0 Step -1

Hi again

The code for your origional questions using While/Wend is as follows
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Dim sides As Integer
  3.  
  4. Private Sub Command1_Click()
  5.     Dim i As Integer
  6.     Dim j As Integer
  7.  
  8.     Label1.Caption = ""
  9.     sides = 5
  10.     j = 1
  11.     While j <= sides
  12.         i = 1
  13.         While i <= j
  14.             Label1 = Label1 & i & Space(1)
  15.             i = i + 1
  16.         Wend
  17.         Label1 = Label1 & vbCrLf
  18.         j = j + 1
  19.     Wend
  20. End Sub
  21.  
  22. Private Sub Command2_Click()
  23.     Dim i As Integer
  24.     Dim j As Integer
  25.  
  26.     Label1.Caption = ""
  27.     sides = 5
  28.     j = sides - 1
  29.     While j >= 0
  30.         i = sides - j
  31.         While i >= 1
  32.             Label1 = Label1 & i & Space(1)
  33.             i = i - 1
  34.         Wend
  35.         Label1 = Label1 & vbCrLf
  36.         j = j - 1
  37.     Wend
  38. End Sub
  39.  
  40. Private Sub Command3_Click()
  41.     Dim i As Integer
  42.  
  43.     Label1.Caption = ""
  44.     Label2.Caption = ""
  45.     sides = 10
  46.     i = 1
  47.     While i <= sides
  48.         If i Mod 2 = 0 Then
  49.             Label1 = Label1 & i & Space(1)
  50.         Else
  51.             Label2 = Label2 & i & Space(1)
  52.         End If
  53.         i = i + 1
  54.     Wend
  55.  
  56. End Sub
With regard your other questions, well, where do the permutations end !!

Without wishing to seem unhelpful, I think you should have enough info now to try things yourself so you can do whatever is required in future. It is the only way to learn !!


MTB
Reply

Tags
looping