What is the value of the variable named counter
What is the value of the variable named counter after the following statements are executed:
Code:
Dim percent As Double = 0.54
Dim valid as Boolean = True
Dim counter As Integer = 1
If percent > 0.050 and valid = True Then
counter += 2
If valid = True Then
counter += 1
ElseIf percent >= 0.50 Then
counter += 3
End If
Else
counter +=1
End If
The possible answers are 2, 3, 4, and 7 Please advise.
Thank you.
Re: What is the value of the variable named counter
Why can't you just run the code and figure it out?
Is there an actual question here?
Re: What is the value of the variable named counter
Re: What is the value of the variable named counter
-_-
I asked him why he needs to know, because this sounds like a homework or test question.
Re: What is the value of the variable named counter
Re: What is the value of the variable named counter
yep, sounds like homework...but i agree with weirddemon as to why he doesnt just run it.
Re: What is the value of the variable named counter
Re: What is the value of the variable named counter
you don't even need to run it... just follow it...
--tg
Re: What is the value of the variable named counter
(42 - (42 mod 3) ) \ 10
will evaluate to 4 he is just trying to test you, I don't see the point since he could have just run it and gotten the answer
maybe he wants an explanation as to why it comes out to 4..
you can debug and step through it if that helps you
Re: What is the value of the variable named counter
Re: What is the value of the variable named counter
Quote:
Originally Posted by
weirddemon
Why can't you just run the code and figure it out?
Is there an actual question here?
I agree. Just run the above code and put in break points to see what the values of the variables are.
Re: What is the value of the variable named counter
You should have just run it kid, lol, the answer is:
vb Code:
Dim answer as string = counter
msgbox(answer) 'Yields a string that says "Do you're homework!"
Re: What is the value of the variable named counter
Okay. You're right. I should do my own homework. Can you at least tell me how to run the code? What clues do I have for setting up the form in order to run the code from the question I previously posted?
Thanks
Re: What is the value of the variable named counter
Please tell me how to run the code so that I can figure out future questions myself. Thanks.
Re: What is the value of the variable named counter
Quote:
Originally Posted by
EbonyVb
Please tell me how to run the code so that I can figure out future questions myself. Thanks.
If this is homework for a course I find it hard to believe that your course hasn't covered how to run code.
I'd suggest going back to your course notes or tutor because you must have missed or not followed something thats already been covered.
Re: What is the value of the variable named counter
to test, if you do not have visual studio (you could get the express version for free via web), you could make some small changes and test this code in
Vba editor of Office Word (if you have it)...
I added some comments to help you understand why answer is 4...
Code:
'first, counter += means
'counter=counter + ...
'that is:add something to counter and
'assign the total to counter
'variable where declared and assigned with a value in a single line
'if you're going to use Vba, you must split the operation in two (declaring,then assigning)
Dim percent As Double
percent = 0.54
Dim valid As Boolean
valid = True
Dim counter As Integer
counter = 1
'now counter starts as 1
'percent is bigger than 0.05
'and valid is true, thus you enter
If percent > 0.05 And valid = True Then
'and add 2 to 1.
counter = counter + 2
'thus now counter is 3
'now you evaluate again, but
'when you have if...elseif...else...
'you will get only the first true.
'valid is true, thus you enter here
If valid = True Then
'and you add 1 to the previous value
'(which was 3)
counter = counter + 1
'thus now counter is 4
'as stated before, you do not enter in the elseIf below in any case
'even if percent is >0.5, as you already entered the previous
'alternative of the two (would you like a cup of coffe or
'do you prefer to take a walk? You might want both, but I asked
'you to choose...)
ElseIf percent >= 0.5 Then
'so you do not get here
counter = counter + 3
End If
Else
'this is alternative to percent > 0.05 and Valid true
'you would have entered here if percent <0.05 OR if Valid= false
'but as they are both true, you entered previous branch, and do
'not enter here. Do believe next assignment will se the valid value
'change to False...
counter = counter + 1
End If
'the followin instruction will show in an immediate window the result
Debug.Print counter
Re: What is the value of the variable named counter