Adblock breaks this site

[TUT4noobies] If Statement

Discussion in 'Programming General' started by Blupig, Nov 23, 2007.

  1. Blupig

    Blupig BEEF TOILET
    $5 USD Donor

    Joined:
    Nov 23, 2006
    Posts:
    7,145
    Referrals:
    16
    Sythe Gold:
    1,609
    Discord Unique ID:
    178533992981594112
    Valentine's Singing Competition Winner Member of the Month Winner MushyMuncher Gohan has AIDS Extreme Homosex World War 3 I'm LAAAAAAAME
    Off Topic Participant
    [TUT4noobies] If Statement

    [TUT] If Statement

    The if statement is a really useful statement which allows the instruction for the program to do something if something else happens. For example, when you press a button, you can tell the program that if the "red program" option checkbox is checked, to make the program red. We'll build on this throughout the tutorial.
    The example I meantioned above can be coded out as follows:
    Code:
    Private Sub Command1_Click
    
    If Check1.Value = 1 Then
    Form1.Backcolor = vbRed
    End If
    
    End Sub
    

    Breakdown time! Alright, so you should already know the "Private Sub Command1_Click" line and the "End Sub" line. The first part of out statement is really simple. It's telling your application that if the checkbox check1's value is 1 (checked), to turn the backcolor of form1 to red (visual basic has built-in main colors, so like red, green, blue, black, white, etc. They come in the form of "vb" and the color name.). With the if statement, we need to close it off so that the rest of the code isn't ifified. That's where the "end if" comes in.

    So sure, this is simple and can't really do much. So, let's add an "Else" line in, so that when Command1 is clicked, if Check1 isn't checked than the form's background color will be blue instead of red. We can do that like this:


    Code:
    Private Sub Command1_Click
    
    If Check1.Value = 1 Then
    Form1.Backcolor = vbRed
    Else
    Form1.Backcolor = vbBlue
    End If
    
    End Sub
    

    Breaking down time again! Alright so I guess I just have to explain the "Else" line. The Else line allows an instruction that if check1's value isn't 1, to do something else instead. Putting it into speech, it'd be like this: If Check1's value is 1, then make Form1's backcolor red. Otherwise, make it blue.

    This is the last esential thing you need to know about the if statement: the ElseIf line. It's extremely basic. What it is is putting 2 if statements together to avoid having too many if statements all over. Change your checkboxes to radio buttons, so we can use the ElseIf line easier. Add 2 radio buttons, one for red and one for blue. Keep your usual Command1. Here's the code for the example we had above, except with radio buttons and the ElseIf line:
    Code:
    Private Sub Command1_Click
    
    If Option1.Value = True Then
    Form1.Backcolor = vbRed
    ElseIf Option2.Value = True Then
    Form1.Backcolor = vbBlue
    End If
    
    End Sub
    

    Now, when Command1 is clicked, Form1's backcolor is changed to red if option1's value is true. But, if option2's value is true, then the form's backcolor will be blue.
    -----
    Tutorial brought to you by Blupig

    If (lol jokes) you liked my tut, please give rep if it's available (I'm posting this on several forums :eek:)
     
  2. Nullware

    Nullware Guru

    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0
    [TUT4noobies] If Statement

    Although it's mostly common sense it might be useful to mention in this last example that if neither of the options are checked the program does nothing because none of the conditions were true and there was no "Else" option to go through.

    Nice tutorial, simple to follow, easy to understand, and most of all right to the point. Well done, consider sprucing it up with some color though. :p
     
  3. Blupig

    Blupig BEEF TOILET
    $5 USD Donor

    Joined:
    Nov 23, 2006
    Posts:
    7,145
    Referrals:
    16
    Sythe Gold:
    1,609
    Discord Unique ID:
    178533992981594112
    Valentine's Singing Competition Winner Member of the Month Winner MushyMuncher Gohan has AIDS Extreme Homosex World War 3 I'm LAAAAAAAME
    Off Topic Participant
    [TUT4noobies] If Statement

    Good idea, and thanks Null ;) Wait till I get pro in VB, there'll be tuts like this for actually useful things :D
    EDIT: Colourfied.
     
  4. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    [TUT4noobies] If Statement

    Im' not sure about V6, but in .Net its way simpler.

    Code:
    If Check.Checked Then
    Form1.Backcolor = Color.Red
    ElseIf Check2.Checked Then
    Form1.Backcolor = Color.Blue
    End If
    You don't need to type in if bool = true so far as I know, all you have to do is if bool.

    EXAMPLE:
    Code:
     Dim b As Boolean
    b = true
    If (b) Then
    MsgBox b
    Else
    '// nothing happens
    End If
    
    Dim b2 As Boolean
    b2 = false
    if Not b Then DoShit()
    
    Blupig - once I get pro at C++, we'll see whos the master ;)
     
  5. jdsfighter

    jdsfighter Forum Addict
    Visual Basic Programmers

    Joined:
    Jan 21, 2007
    Posts:
    603
    Referrals:
    0
    Sythe Gold:
    0
    [TUT4noobies] If Statement

    Yes Swan the If statements in VB and VB.net work practically the same. As far as I can tell there are no differences.
     
  6. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    [TUT4noobies] If Statement

    Yea - Most languages have the same form of If Statement. I prefer to use the ternary shorthand in languages such as C# whenever possible though. Too bad BASIC doesn't have one.

    e.g.
    Code:
    int num;
    num = i < j ? i++ : j++;
    meh. If statements are easy. Try introducing something a bit more advanced (like there is anything advanced in VB6 ...)
     
  7. Cruel__Machine

    Cruel__Machine Guest

    Referrals:
    100
    [TUT4noobies] If Statement

    Well, you could make an incrementor:
    Code:
    Function inc(ByRef num As Integer) As Integer
        num = num + 1
        inc = num
    End Function
    and use something like:
    Code:
    num = IIf(i < j, inc(i), inc(j))
    Not too much more after you have the funcs :p

    But as far as the bool statements go, yes, "== true" is always redundant.
     
  8. Jazz00006

    Jazz00006 Apprentice
    Visual Basic Programmers

    Joined:
    Aug 26, 2005
    Posts:
    807
    Referrals:
    0
    Sythe Gold:
    0
    [TUT4noobies] If Statement

    Hurrr, I'm a cruel
     
  9. Cruel__Machine

    Cruel__Machine Guest

    Referrals:
    100
    [TUT4noobies] If Statement

    Jazz mah boi, it must be embarrassing... to be that neeb.
    It doesn't increment num that way (notice the ByRef) ;)

    +1 leel :p
     
< C++ help =[ | What Sort Of Things Did You Make When You Started VB? >


 
 
Adblock breaks this site