Question New, need help, learning Visual Basic 2010

Erin

Member
Joined
Nov 7, 2011
Messages
9
Programming Experience
Beginner
HI everyone! I have a question. This is my first post and need help with figuring out this bug. I am in college and learning Visual Basic 2010. We were given this and for some reason I can't get this to work right. Here is the whole thing:

The following code should display three rows of percent signs in the msgLabel. The first row should contain one percent sign, the second row should contain two percent signs, and the third row should contain three percent signs. However, the code is not working correctly.
For row As Integer = 1 To 3
     For percent As Integer = 1 To 3
          msgLabel.Text = msgLabel.Text & "%"
     Next percent
     msgLabel.Text = msgLabel.Text & ControlChars.NewLine
Next row






If anyone could help me I would be so appreciative!!!!!!!
 
Last edited by a moderator:
You have two nested loops that each go from 1 to 3. That means that you will get 3 lines of 3 characters each. If you want different numbers of characters on each line then you need different limits to your inner loop for each iteration of your outer loop.
 
msgLabel.Text = "%" & vbCrLf & "%%" & vbCrLf & "%%%"
 
msgLabel.Text = "%" & vbCrLf & "%%" & vbCrLf & "%%%"
First up, please don't do people's homework for them. It's cheating. By all means help but it is for people to write their own code. Secondly, as this is homework, your solution is obviously of no use because this is an exercise in using loops. If it was as simple as what you have shown, what would be the point? The OP is supposed to gain an understanding of how to use nested loops and it is that lack of understanding that is the issue. The inner loop is supposed to have a relationship to the outer loop but there currently is none. Using logic to figure out what that relationship is and how to implement it in code is what the marks are to be awarded for, not blindly copying someone else's code. If a student can't figure that out then they don't deserve the marks.
 
jmcilhinney - Thank you for your response in both comments! That is truly what I needed. I wasn't coming on here asking for someone to do my homework for me. I assure you that I am a great student and work hard for my grades. I was stumped on this one question and needed some guidance and that is exactly what you did. I truly appreciate it! Please rest assured that I was not trying to cheat or get someone else to do my homework for me. I just needed that. Please don't be upset at me for posting in here. I will be doing this for a living once I graduate and I will be more active then. I have browsed around in here and have already learned a lot. I appreciate the great forums!

Herman - I appreciate your help as well. Unfortunately the snippet of code that you posted I can't use as we aren't that far into the book to be able to use the vbCrLf right now. I greatly appreciate your help!
 
It kind of saddens me that college programming courses struggle with teaching students basic flow control. I was learning these things literally in junior high.
 
It kind of saddens me that college programming courses struggle with teaching students basic flow control. I was learning these things literally in junior high.
Not everyone takes computer science classes before college and even when in college it's quite common to see students taking a programming class or two as part of their degree even though their intentions aren't even to be programmers. I don't know about Erin in this case, but I felt I should point this out.
 
I have never taken any computer programming courses before this semester in college. This is my first semester and I want to be a computer programmer. But this really bugged me because I couldn't figure it out. I do want to continue doing this but sometimes people just get stuck and have a hard time figuring it out. But no, nothing like this course was ever even offered in junior high or high school. I'm majoring in Computer Science, but like I said, this is my very first semester in college so I am very new at this.
 
You have two nested loops that each go from 1 to 3. That means that you will get 3 lines of 3 characters each. If you want different numbers of characters on each line then you need different limits to your inner loop for each iteration of your outer loop.

Okay, I know what you are saying, however, I'm not sure how to go about getting it to do that. I have tried so many different things and still am having trouble. I have tried the +1, tried converting the % sign from a string and well that didn't work. I just keep getting errors. I have no idea what I'm doing wrong, or forgetting. Being they are percent signs and therefore can't really be converted to anything other than that, it doesn't really make sense in my head. I know I am missing something simple here, but for the life of me I can't figure out what it is!!!! This is so frustrating!
 
By the way, I have already turned in the homework, so everything from here on out is just learning it because I want to know it. I'm not interested in only getting a "grade", I want to do this for a living eventually so I truly want to understand it. I just wanted to let everyone know that the homework is already turned in and I know I got this question wrong on it, but I still want to know how to do it.
 
Okay I've got it to display this:
%%
%%%%
%%%%%%%%

How do I get it to show
%
%%
%%%

Here is the code I have


Dim output As String = "%"


        For row As Integer = 1 To 3
            output += output
            For percent As Integer = 1 To 3 Step 1
            Next percent
            msgLabel.Text = msgLabel.Text & output & ControlChars.NewLine
        Next row
 
Last edited:
I got it figured out now!!!!! Thank you all for all your help!!!

Dim output As String = "%"

        msgLabel.Text = output & ControlChars.NewLine
        For row As Integer = 1 To 2
            output = output + "%"
            For percent As Integer = 1 To 3
            Next percent
            msgLabel.Text = msgLabel.Text & output & ControlChars.NewLine
        Next row
 
Erin, you need to have your inner loop use the variable from the outter loop:
VB.NET:
For row As Integer = 1 To 3
  For col as Integer = 1 to row

  Next col
Next row
 
Nested For loops

Erin,

Please do not send me any private messages. I just received the one you sent but was unable to respond.

I see that someone already posted the solution to the problem, which changes the terminal value of the inside For loop. Here is the code you should be using:

VB.NET:
        For row As Integer = 1 To 3
            For percent As Integer = 1 To row
                msgLabel.Text &= "%"
            Next percent
            msgLabel.Text &= ControlChars.NewLine
        Next row
 
Back
Top