if statement explained
Post your C# code snippets in here.
1 post
Page 1 of 1
<!--(.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
There are all kinds of relational operators
you can't assign anything with a relational operators.
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"
The last two are the ones I use rarely , the syntax is exactly the same but the operator is changed
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
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
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
First of all , between the parentheses , you 'place what should be + relational operator + that'if(the boolean to specify if the code will execute or not)
{
the code to execute
}
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
Code: Select all
That example will return false, because 0 is not equal to 10int one = 0;
int two = 10;
if(one == two)
{
//Do that code
}
Code: Select all
Computer thinking : Is 0 smaller or equal then 10 ? He answered correct so I'll give him a cookieint one = 0;
int two = 10;
if(one <= two)
{
//If smaller or equal than
// Give 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
you just got slapped , because 0 is not equal to 10int one = 0;
int two = 10;
if(one != two)
{
// if one is not two then
// slap
}
The last two are the ones I use rarely , the syntax is exactly the same but the operator is changed
Code: Select all
congratulations, it will execute , because 0 is smaller then 10int one = 0;
int two = 10;
if(one < two)
{
// if one is smaller than two
//execute
}
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
since C# doesn't have an elseif-statement , you need to use an if within an else fieldint 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
}
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
1 post
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023