In our last tutorial, we studied how to control flow of execution based on various conditions using “if” statement. In this chapter or tutorial we will have a look at various basic building blocks in C# like expressions, statements and operators.
We will take an example first to understand basic concept of today‘s lesson. Sentences in all the languages including English are made of various parts like verbs, nouns and other part of speech. Just like these languages, C# also have its own set of verbs, nouns and statements. Statements are “Complete Thoughts” in C# or in other words statements make complete sense in C# language. Statements are made up of one or more expressions and expressions are made up of one or more operators and operands. If you can recall our last application, you can find few statements in that as well. For example,
myTextBox.Text = “Hello World”;
This is an example of statement, which is made up of an expression (= sign) and operands (literal string on one side and property setter on other side). You can find multiple other statements in our earlier applications for no C# application can be made without statements.
Operands are objects like controls, variables, literal strings and other objects while operators are things like concate operator (+), comparison operator (==) and so on. There are many operators, some of which you even have used but didn’t recognize it as an operator (e.g. “;” semi-colon operator). You can find the list of all operators here. But as a beginner we will need only few operators, though extra knowledge never go wasted.
We will take a brief look at some operators which we have already used or which are important.
- Member access operator (.): Used to access member functions like in this example we have accessed text property of text box. E.g. txt1.Text
- Assignment operator (=): Used to assign values to L.H.S.
- Addition Operator (+): It can be used as arithmetic operator as well as concatenating operator. E.g. txt1.Text = “hi” +txtinp.Text;
- Subtraction, multiplication and division operators (-,*,/): Perform subtraction, multiplication and division respectively.
- Equality operator (==): Also knows as comparison operator.
- Less than and greater than operators (<,>)
- Greater than or equal to and less than or equal to operator (>=, <=)
- Conditional AND operator (&&): Used for checking two conditions at the same time (exp1 and exp2)
- Conditional OR operator (||): Used for checking two conditions at the same time (exp1 or exp2)
- Conditional operator (?:): It is pretty useful one and saves lots of keystrokes at times. E.g. message=(x==1)?”car”:”boat”; It means if x is equal to one then assign value car to message else assign value boat to message.
So this is all we will learn this chapter. But remember one thing; C# is just like English language. It needs meaningful sentences to make complete sense. All C# sentences are made up of few parts and all are equally important. Try to remember these operators and expressions as we will have to use them in almost all our tutorials.
With this, we are signing off from this chapter and see you soon in next part, where we will learn more about one statement which can be used as an alternative to “if statement” and conditional operator equivalent to “if statement”.