10-12-2010
Eg: for if condition:
void main()
{
Console.Write Line("Enter a value");
int a = convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter b value")
int b = convert.ToInt32(Console.ReadLine());
if(b == 0)
{
Console.WriteLine("Please enter b as other than zero");
b = Convert.ToInt32(Console.ReadLine());
}
int c = a/b;
Console.WriteLine("division result is " +c)
Console.ReadLine();
}
Output: Enter a value
10
Enter b value
0
Please enter b as other than zero
2
Division result is 5
b. if else:
Syntax: if (<Condition>)
Statement 1;
else
Statement 2;
Eg: for comparing two numbers:
void main()
{
Console.Write Line("Enter i value");
int i = convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter j value")
int j = convert.ToInt32(Console.ReadLine());
if(i > j)
Console.WriteLine(" i is greater than j ");
else
Console.WriteLine(" j is greater than i ");
Console.ReadLine();
}
Output: Enter i value
10
Enter j value
5
i is greater than j
Eg:
void main()
{
Console.Write Line("Enter i value");
int i = convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter j value")
int j = convert.ToInt32(Console.ReadLine());
if(i > j)
Console.WriteLine(" i is greater than j ");
Console.WriteLIne("i value is " +i);
else
Console.WriteLine(" j is greater than i ");
Console.WriteLIne("j value is " +j);
Console.ReadLine();
}
Output: Enter i value
10
Enter j value
10
i is greater than j
i value is 10
if elseif: Whenever we want to check the multiple conditions then we go for "if-elseif".
Syntax: if(<condition>)
statement1;
elseif(<condition2>)
statement2;
elseif(<condition3>)
statement3;
else
statement4;
Eg:
void main()
{
Console.Write Line("Enter i value");
int i = convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter j value")
int j = convert.ToInt32(Console.ReadLine());
if(i > j)
Console.WriteLine(" i is greater than j ");
elseif(j > i)
Console.WriteLine(" j is greater than i ");
else
Console.WriteLine(" i is equal to j ");
Console.ReadLine();
}
Output: Enter i value
10
Enter j value
10
i is equal to j
Eg: For age related program:
void main()
{
Console.WriteLine("Enter your age");
int age = convert.ToInt32(Console.ReadLine());
if (age >= 58)
Console.WriteLine("You are a senior citizen");
elseif (age >= 25)
Console.WriteLine("You are a working citizen");
elseif (age >= 16)
Console.WriteLine("You are a college student");
elseif (age >= 4)
Console.WriteLine("You are a school going boy");
elseif (age >= 1)
Console.WriteLine("You are a playing kid ");
else
Console.WriteLine("Invalid age");
Console.ReadLine();
}
Output: Enter your age
58
Your are a senior citizen
{
Console.Write Line("Enter a value");
int a = convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter b value")
int b = convert.ToInt32(Console.ReadLine());
if(b == 0)
{
Console.WriteLine("Please enter b as other than zero");
b = Convert.ToInt32(Console.ReadLine());
}
int c = a/b;
Console.WriteLine("division result is " +c)
Console.ReadLine();
}
Output: Enter a value
10
Enter b value
0
Please enter b as other than zero
2
Division result is 5
b. if else:
Syntax: if (<Condition>)
Statement 1;
else
Statement 2;
Eg: for comparing two numbers:
void main()
{
Console.Write Line("Enter i value");
int i = convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter j value")
int j = convert.ToInt32(Console.ReadLine());
if(i > j)
Console.WriteLine(" i is greater than j ");
else
Console.WriteLine(" j is greater than i ");
Console.ReadLine();
}
Output: Enter i value
10
Enter j value
5
i is greater than j
Eg:
void main()
{
Console.Write Line("Enter i value");
int i = convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter j value")
int j = convert.ToInt32(Console.ReadLine());
if(i > j)
Console.WriteLine(" i is greater than j ");
Console.WriteLIne("i value is " +i);
else
Console.WriteLine(" j is greater than i ");
Console.WriteLIne("j value is " +j);
Console.ReadLine();
}
Output: Enter i value
10
Enter j value
10
i is greater than j
i value is 10
if elseif: Whenever we want to check the multiple conditions then we go for "if-elseif".
Syntax: if(<condition>)
statement1;
elseif(<condition2>)
statement2;
elseif(<condition3>)
statement3;
else
statement4;
Eg:
void main()
{
Console.Write Line("Enter i value");
int i = convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter j value")
int j = convert.ToInt32(Console.ReadLine());
if(i > j)
Console.WriteLine(" i is greater than j ");
elseif(j > i)
Console.WriteLine(" j is greater than i ");
else
Console.WriteLine(" i is equal to j ");
Console.ReadLine();
}
Output: Enter i value
10
Enter j value
10
i is equal to j
Eg: For age related program:
void main()
{
Console.WriteLine("Enter your age");
int age = convert.ToInt32(Console.ReadLine());
if (age >= 58)
Console.WriteLine("You are a senior citizen");
elseif (age >= 25)
Console.WriteLine("You are a working citizen");
elseif (age >= 16)
Console.WriteLine("You are a college student");
elseif (age >= 4)
Console.WriteLine("You are a school going boy");
elseif (age >= 1)
Console.WriteLine("You are a playing kid ");
else
Console.WriteLine("Invalid age");
Console.ReadLine();
}
Output: Enter your age
58
Your are a senior citizen