Pages

Showing posts with label how to make programs in C#.Learn C# for free. Show all posts
Showing posts with label how to make programs in C#.Learn C# for free. Show all posts

Saturday, 15 June 2013

Using various operators in C# Part 3

                     So from our previous post we have learn about basic C# programming example writing code declaring classes, variable, accepting user input and compiling and executing the program. To read our two previous posts just click on Part 1 and Part 2.
                        In this post we will learn about various operators that are used in C#, sometimes you may need to compare data values and compute results in your programs it can’t be done without the help of operators. So learning, about operators is a good decision don’t worry they are simple and easy to understand.
          Now operators also have different categories, they are:
1)      Arithmetic operators
2)      Arithmetic assignment operators
3)      Unary operators
4)      Comparison operators
5)      Logical operators

Arithmetic operators
   
    They are used to perform basic arithmetic operation like adding and subtracting variables. The following table shows the list of operators which are used in Arithmetic operators

Operators
Use to
Examples
+
Add two numbers
X  = Y + Z;
If Y is equals to 40 and Z is equals to 4, X will be 44.
-
Subtract two numbers
X  = Y - Z;
If Y is equals to 40 and Z is equals to 4, X will be 36.
*
Multiply two numbers
X  = Y * Z;
If Y is equals to 40 and Z is equals to 4, X will be 160.
/
Divide one number by another
X  = Y / Z;
If Y is equals to 40 and Z is equals to 4, X will be 10.
%
Divide two number and return the reminder, also called module operators
X  = Y / Z;
If Y is equals to 41 and Z is equals to 4, X will be 1.





Arithmetic assignment operators
    
      Are used to perform arithmetic operations on two given operands and assigns the resultant value to any one of them. The following table shows the list of operators which are used in Arithmetic assignment operators

Operators
Use to
Examples
+=
X += Y;
Same as,
         X = X + Y;
-=
X -= Y;
Same as,
         X = X - Y;
*=
X *= Y;
Same as,
         X = X * Y;
/=
X /= Y;
Same as,
         X = X / Y;
%=
X %= Y;
Same as,
         X = X % Y;


For example take two variables X and Y where, X = 4 and Y = 6 and then apply X +=  Y; it will show X = 10 because it will add X’s value and Y’s value and assign it to X that’s why it shows X = 10 not like X = Y+Z, where it has three variables X,Y and Z and X value is equals to Y+Z.

Comparison operators

These types of operators are used to compare two expression or value and perform an action on the basis of the result of that comparison. The value of the comparison operator comes in Boolean value, true or false. The following table explains the use of some comparison operators.

(In the following example assume the value of x = 50 and the value of Y = 55)

Operators
Usage
Description
Example
< 
Expression1
< 
Expression2
It will check whether
Expression1
Is less than
Expression2
bool example;
Example = X<Y;
Example will show the value true
> 
Expression1
> 
Expression2
It will check whether
Expression1
Is greater than
Expression2
bool example;
Example = X>Y;
Example will show the value false
<=
Expression1
>=
Expression2
It will check whether
Expression1
Is less than or equals to
Expression2
bool example;
Example = X<=Y;
Example will show the value true
>=
Expression1
>=
Expression2
It will check whether
Expression1
Is greater than or equals to
Expression2
bool example;
Example = X>=Y;
Example will show the value false
==
Expression1
==
Expression2
It will check whether
Expression1
Is equals to
Expression2
bool example;
Example = X==Y;
Example will show the value false
!=
Expression1
!=
Expression2
It will check whether
Expression1
Is not equals to
Expression2
bool example;
Example = X != Y;
Example will show the value true.


Logical operators 

These types of operators evaluate an expression and return a Boolean value true or false. The following table shows and explain the use of logical operators.

Operators
Usage
Description
Example
&&
Expression1
&&
Expression2
Returns true if both
Expression1
And
Expression2
Are true
bool example;
String A1,A2;
A1 = 4;
A2 = 7;
example = (A1 == 4)&&(A2 == 7);
Console.WriteLine(example.ToString());

The above code will print true on the screen because A1 has the value 4 and A2 has the value 7.
!
! Expression

Returns true if
Expression
Is false
bool example;
int a
A1 = 10;
example = (!(A==20));
Console.WriteLine(example.ToString());

The above code will print true on the screen because the expression example = (!(A==20)); is false

||
Expression1
||
Expression2
Returns true if either
Expression1
or
Expression2
Or both of them are true
bool example;
String A1,A2;
A1 = 4;
A2 = 7;
example = (A1 == 5)||(A2 == 7);
Console.WriteLine(example.ToString());

The above code will print true on the screen because A2 has the value 7.
^
Expression1
^
Expression2
Returns true if
Expression1
or
Expression2
is true.it returns false if both
Expression1
and
Expression2
Are true or if  both
Expression1
and
Expression2
Are false

bool example;
String A1,A2;
A1 = 4;
A2 = 7;
example = (A1 == 4)^(A2 == 7);
Console.WriteLine(example.ToString());

The above code will print false on the screen because  both the expression are true.

 To understand it better you have to use these operators in a program. you can use this operators in any program but the main use of these operators are in Conditional constructs and Loop constructs we will explain them in our next two post....................

Tuesday, 5 February 2013

Declaring variable in C# Part 2



           In the previous post we have learn about basic terms of c#  a little about OOP (object oriented programming )  and writing and executing  a particular program in this post we will learn about variables and how we can apply it to our myname.cs program. If you didn't read it just click here.
So let’s begin the fun.

What is a Variable?

A variable is what we use to store some data or value to location in the memory, relax we just have to declare the name of the variable an assign a value to it, it is easy.
Syntax to declare any variable:

<Data type><variable name>;
<Variable name> = <value>;

Data Types and Their Types   

    Now you will learn about data types and we will explain you the preceding syntax more easily

In the above syntax what is?

1.    <Data type>

       <Data type> is the type of data we want use in our program example letter a number or mixer of both, C# provide some predefined data types that can be directly used in your program. Some of the predefined data types in C# are:

Data Type
Bytes
Values
Char
2
0 to 65535
Int
4
-2,147,483,648 to 2,147,483,647
String
Variable length
0-2 billion Unicode characters.
Float
4
(For negative Values)                      -3.402823E+38 to -1.401298E-45
(For positive Values) 1.401298E-45 to 3.402823E+38
Double
8
(For negative Values)                      -1 .79769313486232E308 to              -4.94065645841247E-324
(For positive Values)
4.94065645841247E-324 to
1 .79769313486232E308

Bool
1
True or false

        The bytes column shows that how many bytes are required to store a particular data type in the memory, And the value column show the range of the value.

How many types of data types there are?
          

         1. Reference
This type of variable contains the address of the value stored in the memory. More than one reference type variable can be created for the same memory location. All the referring variables will automatically reflect the changed value, if you modify the value in the referenced memory location. String is a reference data type in c#.
.            

           2. Value

      These types of variable directly contain data. Char, int, and float data types are value type. When you declare a char variable the system allocates memory to store the value, not the address of the memory location like reference type. 

        3. Dynamic data type

      A variable of this type can be used to store any type of value. And it can use the property of that data   type it is resolved to. Dynamic variables are declared by using the dynamic keyword.
Consider the following example:

 dynamic    A;

     In the preceding example, the variable A is declared a dynamic variable. So, it can store any type of value, example  
1.  A  =     2;( A is considered as int data type ) 
2.  A  =  “M12A”;                        ( A is considered as string data type )


Rules for declaring a variable
  1.  Must begin with a letter or an underscore (_) which may be followed by a sequence of digit (0-9), letters, or underscore. The first word of the variable name must not be a digit.
  2. Must not contain any symbol; however an underscore (_) can be use wherever a space is required.
  3. You can’t use any keyword as variable name.
  4.  Must be unique you can’t use the same name for declaring four different variables.

How to accept and store value in variables 

        Now if you want that your program will accept a value provided by user then you have to use the Console.ReadLine () method.

The syntax for the following code is:

Sting model_no;
model_no = Console.ReadLine ();

     In the preceding code, we declared a variable model_no of string data type, and instead of assigning its value we used the Console.ReadLine () method so it can accept value by the user.

     By default Console.ReadLine ()method accept data in string format if you want to use any other data type you have to use the convert () method, this method inform the compiler to convert one data type to another, it is known as explicit conversion 

To do this you have to use the following code:

model_no = Convert.ToInt32 (Console.ReadLine ());

Here, Convert.ToInt32 () method converts the data provided by user to int data type.

Always remember one thing that C# is case sensitive so if you write convert.toint32 () instead ofConvert.ToInt32()your program will not run.

Let’s write a program

 Now we will apply everything we learn here to our myname.cs program
  
  Now the code for our new program:

using System;

public class My_name
{
String name;
public void input_otput()
    {

Console.WriteLine("Type your name ");
    name = Console.ReadLine();
Console.WriteLine("your name is  " + name);

    }
public static void Main()
    {
My_name name = newMy_name();
        name.input_otput();

    }

}

The following output will be shown at your visual studio command prompt screen:


You have to type a name when you see “Type your name on the screen” and press enter then it will show whatever name you have typed.

In the preceding program code 

   We have used some new thing in the program code and some was already explained to you in my previous post   (click here to read)

     1.  Member variable

Member variables are data member of a class. In the given code, the My_name class has one member variable name. This variable is used to store data provided by the user.

      2. Member functions

      Member functions are also known as member method. The code for the My_name class contain one member function input_output(). And they are declared with return type void this they will not return any value.
           The input_output()method or function prompt the user to enter his/her name, it then store this details in the respective variable and then display it. You can create one more function and use it to only display details it is upon you.

3.    Instantiating class

   The MainClass contain the main () method. This class is used to instantiate the My_name class. To implement or use the functionality of a class you have to create an object of the class.

 Syntax for creating an object is:

<Class name><object name>=  new<class name> ();

Consider our My_name class. To create an object (name) of the My_name class you have to use the following statement in the Main() method 

My_name  name   =  new My_name ();

The new keyword is used to allocate memory to an object at run time. The object name and the “.” operators are used to access the member function of the My_name class. As shown in the following code snippet:

name.input_otput();

Now you can create some program like the above one and comment what you like and your problems in writing programs, I will answer your entire query.


Previous post                                                                                                             Next post