• Log in or sign up for an account here: https://www.sythe.org/login

[TUT4noobies] If Statement

Blupig

BEEF TOILET

$5 USD Donor
Joined
Nov 23, 2006
Sythe Gold
1,609
Messages
7,145
Age
33
Location
Canada
Valentine's Singing Competition Winner
Sythe Awards 2015 Winner
Sythe Awards 2016 Winner
Sythe Awards 2020 Winner
Member of the Month Winner
MushyMuncher
Gohan has AIDS
Extreme Homosex
World War 3
I'm LAAAAAAAME
Discord Verified
Off Topic Participant
[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 :o)
 
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.
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
 
Valentine's Singing Competition Winner
Sythe Awards 2015 Winner
Sythe Awards 2016 Winner
Sythe Awards 2020 Winner
Member of the Month Winner
MushyMuncher
Gohan has AIDS
Extreme Homosex
World War 3
I'm LAAAAAAAME
Discord Verified
Off Topic Participant
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.
 
Member of the Month Winner
Sythe's 10th Anniversary
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 ;)
 
Yes Swan the If statements in VB and VB.net work practically the same. As far as I can tell there are no differences.
 
Member of the Month Winner
Sythe's 10th Anniversary
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 ...)
 
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++;
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.
 
Jazz mah boi, it must be embarrassing... to be that neeb.
It doesn't increment num that way (notice the ByRef) ;)

+1 leel :P
 
Runescape Gamblingrunescape gambling
Back
Top