C# - Lesson 3 - Conditions

View previous topic View next topic Go down

C# - Lesson 3 - Conditions

Post  Guest on Sat Nov 14, 2009 3:31 am

Conditions:

Conditions are used to state a "yes" or "no" value. We say: "IF a certain value is reached, THEN execute a piece of code."

The conditions are somehow the most used basics in every programming language.

The first condition that we are going to look at is the IF condition.

> example: what does the IF condition look like?

Code:
if ()
{

}

Between the round brackets "()" behind the word "if", there are the conditions going to be.
Between the curly brackets "{}", there is the piece of code gonna be. So, this code gets executed when the value in between the round brackets is reached.

> example: checking for empty textboxes

Code:
if (textBox1.Text == "")
{
MessageBox.Show("The textbox has no value!");
}

In the example we ask if the "textBox1" has a value of "" (which means nothing, no characters and no spaces). The ".Text" behind the textBox1 means that the stuff that's in your textbox is converted to text.
The == behind the .Text is the same as the equal sign. In C Sharp, the following things are called Operators.

== equal to
!= not equal to
> greater than
>= greater than or equal to
< less than
<= less than or equal to
&& and
|| or

In our example from above, we use the operator == . So, if the value in the textbox is equal to "" (nothing), then execute the code between the curly brackets.

Our code that has to be executed tells us in a message box that the textbox doesn't have any value.
MessageBox.Show()
The "The textbox has no value!" is the text shown in our MessageBox.

To our IF condition we can add an Else conditions too.

> example: if and else

Code:
if (textBox1.Text == "")
{
MessageBox.Show("The textbox has no value!");
}
else
{
MessageBox.Show("The textbox has value!");
}

This means that if the IF statement is not reached (so there is actually a value in the textbox), then the else statement will execute. So, a message box with the text "The textbox has value!" will pop up.

This was it for Conditions, note that there are more conditions in C# but these are the basic ones we gonna use in the future tutorials.

______________

Next lesson we will handle Iterations.

Guest
Guest


Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum