Oct 20, 2010 06:36 PM
(This post was last modified: Oct 20, 2010 06:37 PM by Pulser.)
C# If Statements
What Is It?
Basically, an "If Statement" tests a condition, if the condition matches it will execute a block of code.
Getting Started:
Begin by making a new textBox and button. Double click the button and insert the following:
Code
if (textBox1.Text == "Hello")
{
MessageBox.Show("Yes!");
}
else
{
MessageBox.Show("No!");
}if (textBox1.Text == "Hello")
This sets the condition, the word "if" sets up the if statement. The stuff inside of the brackets ( ) sets up the condition. The condition tests to see if the text inside of the textBox is "Hello". "==" is known as a conditional operator, it tests a value of something.
{
MessageBox.Show("Yes!")
}
After your if statement you always put the result that is to happen if the condition matches, this is done by making an opening { and closing } bracket. The code to be executed goes in between.
Else
Else is just a general term to say "If the above condition didn't match, then do this".
Once again, After else, make an opening { and closing } bracket, but the code to be executed.
Spoiler (Click to View)
Conclusion:
An If Statement can be used so much! If you want separate things to happen, or you want to test a condition, then this is the thing to use!
Code:
Code
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "Hello")
{
MessageBox.Show("Yes!");
}
else
{
MessageBox.Show("No!");
}
}http://www.pulsecodes.info
Computer and Information Science major.
Information Technology Apprentice.
Freelance Programmer.
Computer and Information Science major.
Information Technology Apprentice.
Freelance Programmer.



![[Image: gAW9U.png]](https://imgur.com/gAW9U.png)