0% found this document useful (0 votes)
9 views6 pages

IF Examples

The document contains 11 code examples demonstrating the use of conditional statements like if, else if, and else in C#. The examples show if/else statements being used to check conditions, compare values, validate input, and determine the largest of three numbers. Nested if/else statements and conditional logic with boolean operators like && and || are also illustrated.

Uploaded by

Aram Shexbzeny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

IF Examples

The document contains 11 code examples demonstrating the use of conditional statements like if, else if, and else in C#. The examples show if/else statements being used to check conditions, compare values, validate input, and determine the largest of three numbers. Nested if/else statements and conditional logic with boolean operators like && and || are also illustrated.

Uploaded by

Aram Shexbzeny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

- First Example:

using System;
namespace Conditional
{
class IfStatement
{
public static void Main(string[] args)
{
int number = 2;
if (number < 5)
{
Console.WriteLine("{0} is less than 5", number);}
Console.WriteLine("This statement is always executed.");
} } }

- Second Example:
using System;
namespace Conditional
{
class IfElseStatement
{
public static void Main(string[] args)
{
int number = 12;

if (number < 5)
{
Console.WriteLine("{0} is less than 5", number);
}
else
{
Console.WriteLine("{0} is greater than or equal to 5", number);
}

Console.WriteLine("This statement is always executed.");


} } }

- Third Example:
using System;
namespace Conditional
{

class IfElseIfStatement
{
public static void Main(string[] args)
{
int number = 12;
if (number < 5)
{
Console.WriteLine("{0} is less than 5", number);
}
else if (number > 5)
{
Console.WriteLine("{0} is greater than 5", number);
}
else
{
Console.WriteLine("{0} is equal to 5");
}}}}
- Fourth Example:
using System;
namespace Conditional
{

class Nested
{
public static void Main(string[] args)
{
int first = 7, second = -23, third = 13;
if (first > second)
{
if (first > third)
{
Console.WriteLine("{0} is the largest", first);
}
else
{
Console.WriteLine("{0} is the largest", third);
}
}
else
{
if (second > third)
{
Console.WriteLine("{0} is the largest", second);
}
else
{
Console.WriteLine("{0} is the largest", third);
} } } } }

- Fifth Example:
using System;
class Test
{
static void Main(string[] args)
{
int n;
Console.WriteLine("Enter a number");
n = Convert.ToInt32(Console.ReadLine());
if(n%2 == 0)
{
Console.WriteLine("Number is even");
}
else
{
Console.WriteLine("Number is odd");
}}}
- Sixth Example:
using System;
class Test
{
static void Main(string[] args)
{
int x = 4, y = 5, z = 1;
if((x>y) && (x>z))
{
Console.WriteLine("x is the greatest number");
}
else if((y>x) && (y>z))
{
Console.WriteLine("y is the greatest number");
}
else
{
Console.WriteLine("z is the greatest number");
} } }

- Seventh Example:
using System;
class Test
{
static void Main(string[] args)
{
char grade = 'A';
if( grade == 'A' )
{
Console.WriteLine("Excellent !");
}
else if(grade == 'B')
{
Console.WriteLine("Outstanding !");
}
else if(grade == 'C')
{
Console.WriteLine("Good !");
}
else if(grade == 'D')
{
Console.WriteLine("Can do better");
}
else if(grade == 'E')
{
Console.WriteLine("Just passed");
}
else if(grade == 'F')
{
Console.WriteLine("You failed");
}
else
{
Console.WriteLine("Invalid grade");

} } }
- Eight Example:
string username = null;
Console.WriteLine("Enter username: ");
username = Console.ReadLine();
if (username == "Asim")
{ Console.WriteLine("Welcome " + username); }
Else
{ Console.WriteLine("Unkown user"); }
Console.ReadLine();

- Ninth Example:
string username = null;
string password = null;
string message = "Welcome " + username;
if (username == "Asim" && password == "12345")
{ Console.WriteLine(message); }
else if (username == "Dave" && password == "54321")
{ Console.WriteLine(message); }
else if (username == "admin" && password == "superadmin")
{ Console.WriteLine(message); }
Else
{ Console.WriteLine("Sorry Password and username incorrect!"); }
Console.ReadLine();
- Tenth Example:
using System;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Statements Sample!");
int a = -1; int b = 10; int c;
if (a < 0 && b < 0)
{
Console.WriteLine("Both a and b are negative.");
}
else if (a < 0 || b < 0)
{
if (b > 0 && b <= 10)
{
c = a + b;
Console.WriteLine("Total: {0}", c);
}
Console.WriteLine("One number is negative.");
}
Else
{
Console.WriteLine("Both numbers are positive.");
}
Console.ReadKey();
} }
- Eleventh Example:
using System;
public class IfExample
{
public static void Main(string[] args)
{
Console.WriteLine("Enter a number to check grade:");
int num = Convert.ToInt32(Console.ReadLine());
if (num <0 || num >100)
{
Console.WriteLine("wrong number");
}
else if (num >= 0 && num < 50)
{
Console.WriteLine("Fail");
}
else if (num >= 50 && num < 60)
{
Console.WriteLine("D Grade");
}
else if (num >= 60 && num < 70)
{
Console.WriteLine("C Grade");
}
else if (num >= 70 && num < 80)
{
Console.WriteLine("B Grade");
}
else if (num >= 80 && num < 90)
{
Console.WriteLine("A Grade");
}
else if (num >= 90 && num <= 100)
{
Console.WriteLine("A+ Grade");
} } }

You might also like