• Top Members
    Reps
    Posts
  • 834 Replies
    2585 Replies
  • 716 Replies
    2025 Replies
  • 331 Replies
    1928 Replies

if statement explained

User avatar
Axel
Coding God
Posts: 1928

if statement explained

Mon Apr 04, 2011 9:22 pm

<!--(.bbc) The if statement.bbc file create at: 4/04/2011 21:57:02 -->

Should this be in the tutorials or still in the snips ?

An 'if statement' is likely the statement you'll use the most ever

I'll explain how to use it
In english

Code: Select all

if(the boolean to specify if the code will execute or not)
{
the code to execute
}

First of all , between the parentheses , you 'place what should be + relational operator + that'
There are all kinds of relational operators
you can't assign anything with a relational operators.
  • == If it is exactly this , do not use '=' , its an assignment
    <= if the first thing is smaller or equal to the second thing
    >= if the first thing is bigger or equal to the second thing
    != if the first thing is not equal to the second thing
    < smaller , but not equal
    >bigger, but not equal
so how to use them :

Code: Select all

int one = 0;
int two = 10;
if(one == two)
{
//Do that code
}
That example will return false, because 0 is not equal to 10

Code: Select all

int one = 0;
int two = 10;
if(one <= two)
{
//If smaller or equal than
// Give a cookie
}
Computer thinking : Is 0 smaller or equal then 10 ? He answered correct so I'll give him a cookie
the if statement will return true, the code will execute

that's the same with bigger/equal to but ofcourse the operator is different and it will return false because 0 isn't more than 10

But now we don't want something to be that other thing; how do we do that ?
We use the the "is not-operator"

Code: Select all

int one = 0;
int two = 10;
if(one != two)
{
// if one is not two  then
// slap
}
you just got slapped , because 0 is not equal to 10

The last two are the ones I use rarely , the syntax is exactly the same but the operator is changed

Code: Select all

int one = 0;
int two = 10;
if(one < two)
{
// if one is smaller than two 
//execute
}
congratulations, it will execute , because 0 is smaller then 10

and the other example will be : <insert guess>

Else
the else statement will be used if the other if-statement didn't give any results

So ; if 2 is smaller than 1: false , if 3 is smaller than 3 : damn false again ? :( my program will stop here.
but with the else thing your code in the else field will be executed only when the others didn't do anything

a quick example

Code: Select all

int one = 0;
int two = 10;
int three = 20;
int four = 30;
if(two <= one)
{
//Yes this won't do anything
}else
{
if(three >= four)
{
//This will also return false , did you ever knew 20 was bigger or equal than 30 ? I don't think so.
}
}else
{
//the final solution
}
since C# doesn't have an elseif-statement , you need to use an if within an else field

I have lots more of this to tell, I might do this in some video tutorial or whatever but this is already to big to fit in the quick snips section

I'm sure I made some mistakes, I'm not perfect, are you perfect and did you see the mistakes ? Tell me I'll adjust it

Post Reply

Return to “Quick Snips”