1. static void main(String [] arg)
{
char ch;
Console.WriteLine("Enter the character:\t");
ch=Convert.ToChar(Console.ReadLine());
if(ch=='U')
Console.Writeline("The character is X");
else
Console.WriteLine("The char is not X");
Console.ReadLine();
}
wats the o/p of the above code .............................?
2. To which category does the operator ++, = belong.........?
3. In the statement using System, System is a.........................
4. which of the following data type is not a value type.............?
- char
- int
- float
- string
- cc
- csc
- cs
- int
- float
- bool
- string
- class
- function
- namespace
Note:
Give the answers by there respective question numbers.
Labels: C# Programming
Some times we need to exit out of the loop with out performing next iteration. SO in such case we make use of break and continue statements.
Break statement:
- break statement is used in order to exit from the loop.
- It Terminates a statement sequence in a switch statement.
- Used to skip all the subsequent instructions and shift the cursor position back to the loop.
- Here the compiler tries to iterate the loop when ever the condition is satisfied .
/*....................... sum of +ve numbers..........................*/
using System;
class Break_Continue
{
static void Main(string [] arg)
{
int num,sum,i;
for(sum=num=i=0;i<5;i++)
{
Console.WriteLine("Enter the +ve number:\t");
num=Convert.ToInt32(Console.ReadLine());
if(num<=0) //............checks for +ve numbers.
continue;
sum=sum+num; //..............adds only +ve numbers.
}
Console.WriteLine("The sum of +ve numbers:\t"+sum); //..... Displays result of +ve numbers.
}
}
Labels: C# Programming
using System;
class Fibonacci
{
public static void Main(string [] arg)
{
int num;
int f[] =new int[15];
Fibonacci()
{
f[0]=1;
f[1]=1;
}
void accept()
{
Console.WriteLine("Enter the number :\t");
num =Convert.ToInt32(Console.ReadLine());
}
for(int i=2; i<=num;i++) { f[i]=f[i-1]+f[i-2]; Console.WriteLine(f[i]); } } } Note:
When u write a program using C sharp u have to save the file with ' .cs' extension.
example: Fibonacci.cs
compilation mtd: csc Fibonacci.cs
execution mtd: Fibonacci.cs
Labels: C# Programming
We have three types of loop constructs
1. while.
2. do...while .
3. for.
- while loop
- This construct is generally used to execute a block of statements for a definite number of times, depending on the condition.
- while statements checks the condition before executing the statements within the loop.
- When the execution reaches the last line of the block the control is transferred to the condition of while and if the condition is satisfied then the statements are executed again and when the condition sets to false it comes out the loop.
while(expression)
{
statements;
}
- do..while loop
2. however, in do...while loop , the body of the loop is executed at least once and the condition is checked for next subsequent iterations.
3. The do...while loop construct is similar to the while loop constructs.
syntax:
do
{
statements;
}while(expression);
example:
int var=10;
do
{
Console.WriteLine("value of var is :{0}",var);
var=var+10;
}
while(var<20);
- for loop
- for loop is used to execute a block of statements for a specific number of times.
- Here the main difference between for and while is mainly in syntactical format. The syntax used for loop is ......
for(initialization; condition; increment/decrement)example:
{
statements;
}
for(int i=1;i<=10;i++) { Console.WriteLine("{0}",i); }Note:
We can follow different syntax's for for loop
for(; ;)
{
statements;
}
Labels: C# Programming
Conditional constructs are used to control the flow of program . conditional constructs allow the flow selective execution of statements , depending on the value of the expressions associated with them. The comparison operators are required for evaluating the conditions . various conditional constructs are :
1. if....else construct.
2. switch... case construct.
- if....else construct
if...else conditional construct is followed by a logical expression where data is compared and a decision is made on the basis of the result of the comparison. syntax is
{
statements;
}
else
{
statements;
}
example:
class Number
{
static void Main(string [] arg)
{
int num;
Console.WriteLine("Enter the number: \t");
num=Convert.ToInt32(Console.ReadLine());
if(num%!=0)
{
Console.WriteLine(" Number is odd");
}
else
{
Console.WriteLine(" Number is even");
}
if....else constructs can be nested inside each other. This process of using multiple if....else constructs is known as cascading if ...else. consider the example :
int n1=5;
int n2=6;
int n3=7;
if(n1>n2)
if(n1>n3)
{
Console.WriteLine("Number 1 is great");
}
else
{
Console.WriteLine("number 3 is great");
}
else if(n2>n3)
{
Console.WriteLine("number 2 is great");
}
else
{
Console.WriteLine("number 3 is great");
}
- switch...case construct
switch(variable name)
{
case option: statement;
break;
case option: statement;
break;
default: statement;
}
Labels: C# Programming
"Operators r the ones which r used to perform different operations on atleast one operand".
Operators r mainly classified into 5 types
- Arithmetic operators
- Arithmetic assignment operators
- unary operators
- comparison operators
- Logical operators
2. Arithmetic assignment operator:
These operators r used in order to perform assignment operations to assign values to variables (operands).We can perform Operations such as ( +=, -=, *=, /=, %= ) . We shall understand them with examples
- += :- x+=y it implies x=x+y;
- *= :- x*=y it implies x=x*y;
These operators use only single operand. These r mainly used for incrementing and decrementing the value present at the variable. these incrementation process includes mainly post and pre process. we shall see examples,
- ++ which is used as ++x where x is the operand and this is called pre incrementation. where the value is assigned after getting incremented . and if we use the operator like x++ then it is called post increment . and the case is similar with decrement process.
- -- is used for decrementation process . the pre and post decrementations r done similar to that of incrementation process.
These operators r used to compare two values and perform the required action.when ever u use a comparison operator the expression results a boolean value (true or false) .
Comparison operators are ( <, >, <=, >=, !=, ==) .These r used in between two expressions .
5. Logical operators:
These r used to evaluate a expression and return a boolean value . the logical operators includes (&&, !, ||, ^).
6. Terenary operator (or) conditional operator: this operator is used to perform an action based on the result that is generated.
syntax:
expr1? expr2 : expr3
here if expr1 is true then expr2 is printed if it is false then expr3 is printed. In general expr1 contains statements regarding comparison of values present at the operands .
Labels: C# Programming
/* Example One */
using System;
class Myclass
{
public static void Main(String arg[])
{
string add='A';
string Response_code="007";
int counter=60;
Console.WriteLine(add);
Console.WriteLine(Responce_code);
Console.WriteLine(counter);
}
}
output:
fnds can u guess it .......................!
Labels: C# Programming
Well in order to be a Good Programmer you have to be a good learner, patient and humble..
Never be under the opinion that you are the best since there are no "GURU'S" in programming..
- So firstly brush up with your concepts, be thorough with all the concept's be it any language.
- Go to a good institute and make your self used to programming.
- Write a basic code (program) and then understand how exactly the program is going to be executed, don't start off with a huge code but take a small and easy one.
- So now you have a picture on how the program will be executed, so now is the fun part, make a backup of the code and mess the code !!!
- I know your thinking I'm crazy but this really helps, so as i said mess up the code and note down the errors, study them and find out what exactly caused the error. So this way you'll know to Debug the program, and this will help you in the future when you deal with a huge code..
- When you finish this task you are not done yet, now think of a way you can improve this code, make it more unique, make it your own... This way you'll master the program and make it easy, easier and easiest..!
- As you go deep into the subject, read different codes in different websites ( we know Internet is the base for all these stuff ) go through different blogs about programming and learn more.
- Think like a programmer and not an end user, to be a programmer you have to think like a programmer.
- Lastly Don't make this(programming) your job, but make this your passion..!
All the best..!
Jammy
Labels: Programming
In the sample program we have come across a statement "using System"
using System;
class Keyword
The class keyword is used to declare a class . The braces ,known as delimeters ,are used to indicate the start and end of class body.
Member variables
variables are used to store data. These r also called as date members of a class or instance members of a class. These variables r declared with the suitable data types.
Member functions
A function is a set of statements that perform a specific task in response to a message . Member functions of a class r declared inside the class.
Instantiating a class
To create memory for a class we generally use the system of dynamic allocation in C#.(i.e by using the keywords new and delete). so to allocate memory to a class we take help of objects which act as instance for the class . These objects interact with each other by passing messages and by responding to received messages. Objects use methods to pass messages, the task of passing messages is done by using member functions. all the objects of a class will use the same copy of member functions through out but they use different copy of member variables in memory.
eg: Student std=new Student();
here 'std' is the object of class Student where memory is allocated dyanamically using the keyword new.
we can access the member functions using this object 'std' .
eg:
std.Accept();
std.Display();
Labels: C# Programming
Here is a sample C# program
using System;
class Student
{
//Member data.
string name;
int id;
//Member functions.
Public void Accept()
{
Console.WtiteLine("Enter the name:\t");
name =Console.ReadLine();
Console.WriteLine("Enter the id of employee:\t");
id=Convert.ToInt32(Console.ReadLine());
}
public void Display()
{
Console.WriteLine("The name is :{0}",name);
Console.WriteLine("The id is :{0}",id);
}
}
// class used to intiate student class
class Executestudent
{
public static void Main(string[] arg)
{
Student st=new Student();
st.Accept();
st.Display();
}
}
Labels: C# Programming
Accepting and storing values in Member variables
So far we have seen different programming languages where they contain different libraries, different code snippets with different syntax, but other than these there is one thing common in all programming languages i.e input and output statements. In C#(a purely OOPs concept) we also use input and output statements with the following syntax.
example:
int Number;
Number=Convert.ToInt32(Console.ReadLine());
Here Console.ReadLine() is a function which accepts the inputs from user and store it in the variable 'Number'. 'Console.ReadLine() ' is a function of Console class and in turn the console class s said to be part of System namespace(collection of classes).
Convert.ToInt32() converts the data entered by the user to integer format. A user has a doubt that why an integer value is again converted into int by using those function, yes , while taking input a compiler generally takes input in string format so in order to convert the data entered by the user to prescribe type we use the function.
Convert() method Explicitly tells the compiler to convert one data type to another data type and this is called explicit conversion .The compiler performs an implicit conversion automatically. (exam: in implicit conversion converts the int data type to float or float datatype to int ,automatically).
Labels: C# Programming
Before talking about variable we shall first know What is a variable ?
A variable is a location in a memory that has a name and holds some value. the value can be an integer, character, decimal. A variable is declared with a specific data type in order to represent what value the variable holds.
Naming the variables in C#
1. Must begin with a character or an underscore and it should be followed by sequence of characters. can not declare a variable which starts with a digit or any other special symbols (other than underscore).
2. a variable should not contain any special symbols . keywords can not be used as variable names.
Examples for valid variable names
Game_level
Myclass (using pascal case).
sum_of_individual_num.
invalid variable names
#result
2score
Note: C# is a case sensitive language. It means that uppercase letters are different from lower case. exam: TennisPlayerName is diff from tennisplayername
Declaring a variable in C#
we can also define a variable at the time of declaration
exam:
int result;
char choice='v';
data types in C#
predefined type Bytes Range
3. float 4bytes
4. double 8bytes
5. bool 1byte specifies whether true or false true or false
they r mainly two type of data types used
1. value type. The value type includes int, float, char. these directly contain data.
2. reference type.
These do not contain any data but contain reference to variables which are stored in memory. example of reference type is 'string' .
Labels: C# Programming
In general we know that a class consists of Member data and Member functions. so similarly in C# a class definition contains Member data and function . Now let us consider an example of a class in C#
public class Hello
{
public static void main(string [] args)
{
system.console.WriteLine("Hello, fnds");
}
}
Now we shall learn about each and every step in detail
1. In any programming language the execution starts from main function and this is said to be the entry point of the application.
2. Main function is used to create objects and invoke member functions.
A class keyword is used in order to create a class . While designing a class we have to remember the following class naming conventions.
- should be meaningful.
- should identically be a noun.
- we can use either pascal case or camel case, where in pascal case the first letter is capitalized and rest are small (ex: Hello) cmg to camel case the first letter is in lower case and first letter of each subsequent word must be in caps (ex: myClass).
Rules for naming a class is
- They must be begin with a letter and not with digits or special symbols.
- a class name should not contain any special characters (other than '_').
- We must not declare a class with any keyword .
- Now coming to System.Console.WriteLine()Here Console is a class that belongs to System namespace . A namespace is a collection of classes . The system namespace contains the method WriteLine(), which displays the enclosed text on the screen. The console class has other methods, which are used for various input/output operations. The character (.) is used to access the function, WriteLine(), which is coded in the Console class of the System namespace.
here from the above example the statement Console.WriteLine("Hello, fnds"); gives the output as Hello, fnds. It is similar to printf (in C), cout( in C++) .
- We can also use escape sequences in these output statements.
- "String [] arg" declared in main function means that is a predefined class name which takes only one argument . It allows a programmer to pass inputs at command line level. In java we pass this as 'main(String arg[])'.
- Now coming to System.Console.WriteLine()Here Console is a class that belongs to System namespace . A namespace is a collection of classes . The system namespace contains the method WriteLine(), which displays the enclosed text on the screen. The console class has other methods, which are used for various input/output operations. The character (.) is used to access the function, WriteLine(), which is coded in the Console class of the System namespace.
Labels: C# Programming
Before knowing about the origin of C# we shall know about the evolution of some basic languages .
Computer languages have come a long way since the 1940. Before evolution of languages the instructions was passed into systems as zeroes and ones ,and these where mainly used in 1st generation of computers. this was considered to be as machine understandable language.
Later on in 2nd generation, they used assembler languages to give instructions to computer. But still the computer was in a position to understand only machine language .so in order to over come this problem they developed a assembler software which translates the assembly language into machine level language.
Now it was the time to develop Operating system, so Martin Richard developed a language called BCPL. later on this BCPL was modified by Ken Thompson and it resulted in the language B. When Ken Thompson was working at Bell laboratories he teamed up with Dennis Ritchie and wrote the early version of Unix operating system for a DEC PDP-7 computer. Now comes the evolution of language C.
Dennis ritchie was working on a project of further developing Unix operating system ,so for that purpose he required a low level language like assembly language that could control hard ware efficenlty and at the same time it should provide features of high level language (i.e it should run on different hardwares) . So he started working with 'B' he found draw backs in that language and he modified it and named it as 'C'. so after the development of C language the entire Unix system was coded in C . It came into existence in 1972. In 1989 American National Standard Institute(ANSI) along with International Standards Organisation (ISO) approved a machine independent and standard version of C.
In early 1980s, Bjarne Stroustrup of Bell Labs developed the C++ language. C++ was originally known as 'C with classes' because two languages contributed to its design: C,which provide low-level features, and Simula67, which provided the class cocept. C++ is an object-oriented language.Other examples of object oriented languages are Java, Smalltalk, and C#.
C# (C-sharp) is a programming language introduced by Microsoft. C# contains features similar to java and c++ .Is is especially designed to work with Microsoft's .NET platform.
Labels: C# Programming
Now we shall discuss about char of object orientation programming in short.
- Realistic Modeling
It is based on modeling a real world which comprises of objects . Object oriented approach allows you to identify objects having attributes and behavior. Attributes and behavior explains how an object acts and reacts. consider the example :- car is an object of class vehicle which has the attributes such as speed, color, power. It displays behavior such as being stationery, moving slowly, accelerating.
- Reusability The Process of using an object for different purposes is known as reusability. This concept mainly involves the usage of existing classes or objects from other applications resources spent in recreating the classes from scratch. example: consider the usage of a two seater car to bring it to four seater car which posses new attributes and behaviour. note: Process of creating a new class by adding some features to existing class is known as inheritance.
- Resilence to change
An existing object can be used to create a similar object. exam: you have a wooden chair and you need to add a head rest to it . So this feature of object orientation programming explains the modification of already existing object to a new object. so inorder to obtain a chair with headrest there is no need for the user to create a new chair but instead he can use the existing chair and add a headrest to it . Resilence to change results in easier maintenance and this feature of Oops is also known as Extensibility.
Labels: C# Programming