Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

C# If Statements [Intermediate Level Tutorial]

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.
Thanks for this :-), it will help me while im moving onto C#
(Oct 20, 2010 06:40 PM)Yui Hirasawa Wrote:  Thanks for this :-), it will help me while im moving onto C#

No problem! Be sure to check out the other tutorials that I will be posting in the future.
http://www.pulsecodes.info
Computer and Information Science major.
Information Technology Apprentice.
Freelance Programmer.
You forgot to add "else if" which is very important.
You should insert the term boolean into your vocabulary.
(Oct 21, 2010 12:44 AM)tflo Wrote:  You forgot to add "else if" which is very important.

These are BEGINNER LEVEL tutorials. You and zer0 factor keep coming onto all of my threads and making negative comments and I don't appreciate it. Please stop. Once I move onto more advanced / intermediate level tutorials I WILL go into greater detail.
http://www.pulsecodes.info
Computer and Information Science major.
Information Technology Apprentice.
Freelance Programmer.
(Oct 21, 2010 08:35 AM)Pulser Wrote:  These are BEGINNER LEVEL tutorials. You and zer0 factor keep coming onto all of my threads and making negative comments and I don't appreciate it. Please stop. Once I move onto more advanced / intermediate level tutorials I WILL go into greater detail.

lolwut
Do you expect everyone to worship you because you gave an explanation of the if statement? If so, you should probably do these 'tutorials' on VB.
(Oct 21, 2010 09:26 AM)Zer0Fact0r Wrote:  lolwut
Do you expect everyone to worship you because you gave an explanation of the if statement? If so, you should probably do these 'tutorials' on VB.

Its quite obvious that you are a troll, and thats fine. Not once in any of my posts have I ever attempted to gain praise. The title of this thread clearly says [Intermediate Level].. If Statements, obviously, are beginner/intermediate level.

If you want to see a more in-depth, advanced tutorial, then I am going to have to ask you to wait because I am still working on them.

Thanks! Have a nice day.
http://www.pulsecodes.info
Computer and Information Science major.
Information Technology Apprentice.
Freelance Programmer.
Your title actually says intermediate, but yeah they are beginner level. It looks nice though - good job. Don't let them bully you. :]
(Oct 21, 2010 09:30 AM)Pulser Wrote:  Its quite obvious that you are a troll, and thats fine. Not once in any of my posts have I ever attempted to gain praise. The title of this thread clearly says [Beginner Level].. If Statements, obviously, are beginner level.

If you want to see a more in-depth, advanced tutorial, then I am going to have to ask you to wait because I am still working on them.

Thanks! Have a nice day.

Lol I'm the troll?
My monitor must be seeing things...

[Image: gAW9U.png]

Your topics are only beginner level in the sense that a beginner wrote them.
What I was saying originally that started all this, was you didn't include the "else if" statement, which I think is very import. Now that I am at it, you should of listed the operators too such as "!=" "<" ">" and so on. And explain how = is declarative and == is comparative.
(Oct 21, 2010 10:32 AM)tflo Wrote:  What I was saying originally that started all this, was you didn't include the "else if" statement, which I think is very import. Now that I am at it, you should of listed the operators too such as "!=" "<" ">" and so on. And explain how = is declarative and == is comparative.

Thanks for the tip Vanquish. I may put together a more advanced tut for this, or simply edit this one.
http://www.pulsecodes.info
Computer and Information Science major.
Information Technology Apprentice.
Freelance Programmer.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by dummycartSep 9, 2021 03:38 AM 1 146 Sep 9, 2021 03:38 AM
Last Post: Lindros
[C] by Tr0jandogJul 18, 2020 12:05 AM 4 224 Jul 18, 2020 12:05 AM
Last Post: Xora
[Python] by CiataLearnerDec 22, 2018 02:27 PM 1 212 Dec 22, 2018 02:27 PM
Last Post: notjapanese