|
-
Feb 27th, 2010, 11:14 AM
#1
Thread Starter
New Member
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.
-
Feb 27th, 2010, 12:05 PM
#2
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?
CodeBank contributions: Process Manager, Temp File Cleaner
 Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Feb 27th, 2010, 12:19 PM
#3
Hyperactive Member
Re: What is the value of the variable named counter
-
Feb 27th, 2010, 12:30 PM
#4
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.
CodeBank contributions: Process Manager, Temp File Cleaner
 Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Feb 27th, 2010, 12:33 PM
#5
Hyperactive Member
Re: What is the value of the variable named counter
-
Feb 27th, 2010, 12:35 PM
#6
Hyperactive Member
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.
-
Feb 27th, 2010, 12:35 PM
#7
Hyperactive Member
Re: What is the value of the variable named counter
-
Feb 27th, 2010, 12:37 PM
#8
Re: What is the value of the variable named counter
you don't even need to run it... just follow it...
--tg
-
Feb 27th, 2010, 12:39 PM
#9
Hyperactive Member
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
-
Feb 27th, 2010, 12:40 PM
#10
Hyperactive Member
Re: What is the value of the variable named counter
-
Feb 27th, 2010, 12:40 PM
#11
Hyperactive Member
Re: What is the value of the variable named counter
 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.
-
Feb 27th, 2010, 12:44 PM
#12
Addicted Member
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!"
-
Feb 27th, 2010, 02:59 PM
#13
Thread Starter
New Member
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
-
Feb 27th, 2010, 03:06 PM
#14
Thread Starter
New Member
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.
-
Feb 27th, 2010, 03:12 PM
#15
Re: What is the value of the variable named counter
 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.
-
Feb 27th, 2010, 05:20 PM
#16
Addicted Member
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
Special thanks to some wonderful people,
such as Lothar the Great Haensler, Aaron Young,
dr_Michael, Chris Eastwood, TheOnlyOne ClearCode....
-
Feb 27th, 2010, 06:49 PM
#17
Thread Starter
New Member
Re: What is the value of the variable named counter
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|