09-12-2010
Console is a pre-defined class it has various members methods among them two are important.
1. WritLine(): This method will print the given value on command prompt window after printing the value it will move the cursor to next line.
2. ReadLine(): This method will accept input from the user until user will press an enter key.
This method return type is string that means this method will treate th e user input as an string.
Eg: To print name and age:
void main(--)
{
Console.WriteLine("Enter your name");
string name = Console.ReadLine();
Console.WriteLine("Your name is " +name);
Console.WriteLine("Enter your age");
string age =Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Your age is " +age);
Console.ReadLine();
}
Output: Enter your name
Sathya
Your name is Sathya
Enter your age
16
Your age is 16
Convert Class: Convert is apre-defined class this class will have various data type conversion methods.
ToInt32(): It is a member of convert class. This method will convert the given value from any data type to int data type.
Class Convert
{
ToInt32()
}
Eg:
void main()
{
Console.WriteLine("Enter Employee number");
int empno = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter employee name");
string ename = console.ReadLine();
Console.WriteLine("Enter Salary");
int sal = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Employee number is " +empno);
Console.WriteLine("Employee name is " +ename);
Console.WriteLine("Employee salary is " +sal);
Console.ReadLine()
}
Output: Enter Employee number
111
Enter employee name
John
Enter salary
1000
Employee number is 111
Employee name is John
Employee salary is 1000
Conditional Statements: Whenever we want to execute a single statement or a block of statement based on some conditions we will go for conditional statements.
Conditional statements are of two types:
1. If condition
2. Switch condition
1. If condition: If condition we can implement in 3 ways:
a. if:
Syntax: if(<condtion>)
{
statement 1;
statement 2;
|
|
statement n;
}
If condition is true then control will execute the statement 1 & statement 2 & so on..after that it will execute the following statements.
If the condition is false control will skip the statement 1 & statement 2 and so on.., after that it will execute the following statements.