Results 1 to 17 of 17

Thread: What is the value of the variable named counter

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Location
    North Carolina
    Posts
    14

    Red face 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.

  2. #2
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    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

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  3. #3
    Hyperactive Member Philly0494's Avatar
    Join Date
    Apr 2008
    Posts
    485

    Re: What is the value of the variable named counter

    four

  4. #4
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    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

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  5. #5
    Hyperactive Member
    Join Date
    Nov 2008
    Location
    USA
    Posts
    257

    Re: What is the value of the variable named counter

    7 --seven

  6. #6
    Hyperactive Member
    Join Date
    Nov 2008
    Location
    USA
    Posts
    257

    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.

  7. #7
    Hyperactive Member Philly0494's Avatar
    Join Date
    Apr 2008
    Posts
    485

    Re: What is the value of the variable named counter

    No its definitely 4

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: What is the value of the variable named counter

    you don't even need to run it... just follow it...

    --tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9
    Hyperactive Member Philly0494's Avatar
    Join Date
    Apr 2008
    Posts
    485

    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

  10. #10
    Hyperactive Member
    Join Date
    Nov 2008
    Location
    USA
    Posts
    257

    Re: What is the value of the variable named counter

    sorry, yeah answer was 4

  11. #11
    Hyperactive Member The Fire Snake's Avatar
    Join Date
    Sep 2009
    Location
    USA
    Posts
    401

    Re: What is the value of the variable named counter

    Quote Originally Posted by weirddemon View Post
    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.

  12. #12
    Addicted Member spyk3's Avatar
    Join Date
    Apr 2009
    Location
    Dothan, AL
    Posts
    218

    Re: What is the value of the variable named counter

    You should have just run it kid, lol, the answer is:
    vb Code:
    1. Dim answer as string = counter
    2. msgbox(answer) 'Yields a string that says "Do you're homework!"

  13. #13

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Location
    North Carolina
    Posts
    14

    Red face 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

  14. #14

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Location
    North Carolina
    Posts
    14

    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.

  15. #15
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: What is the value of the variable named counter

    Quote Originally Posted by EbonyVb View Post
    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.

  16. #16
    Addicted Member Cimperiali's Avatar
    Join Date
    Oct 2002
    Location
    Milan, Italy, Europe
    Posts
    188

    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....

  17. #17

    Thread Starter
    New Member
    Join Date
    Feb 2010
    Location
    North Carolina
    Posts
    14

    Thumbs up Re: What is the value of the variable named counter

    Thank you very much.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width