Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
143 lines (116 sloc) 4.34 KB
using CSharpCertification;
using System;
namespace ControllingProgramFlow
{
class ConditionalInstructions : BaseSample
{
public override string Name
{
get { return "Conditional Instructions"; }
}
public override void Sample()
{
/////////////////////////////
// RELATIONAL OPERATORS
RelationalOperators();
/////////////////////////////
// BOOLEAN OPERATORS
BooleanOperators();
/////////////////////////////
// CONDITIONAL OPERATORS
ConditionalOperators();
}
void RelationalOperators()
{
Console.WriteLine("RELATIONAL OPERATORS");
Console.WriteLine("- Less Than");
bool sample1 = (3 < 5);
Console.WriteLine("3 < 5 = " + sample1);
Console.WriteLine();
Console.WriteLine("- Greater Than");
bool sample2 = (5 > 3);
Console.WriteLine("5 > 3 = " + sample2);
Console.WriteLine();
Console.WriteLine("- Less Than or Equal");
bool sample3 = (5 <= 5);
Console.WriteLine("5 <= 5 = " + sample3);
Console.WriteLine();
Console.WriteLine("- Greater Than or Equal");
bool sample4 = (5 >= 3);
Console.WriteLine("5 >= 3 = " + sample4);
Console.WriteLine();
Console.WriteLine("- Equality");
bool sample5 = (6 == 6);
Console.WriteLine("6 == 6 = " + sample5);
Console.WriteLine();
Console.WriteLine("- Not Equal");
bool sample6 = (6 != 7);
Console.WriteLine("6 != 7 = " + sample6);
Console.WriteLine();
}
void BooleanOperators()
{
Console.WriteLine("BOOLEAN OPERATORS");
Console.WriteLine("- & Operator");
bool sample1 = ReturnFalse() & ReturnTrue();
Console.WriteLine("false & true = " + sample1);
Console.WriteLine();
Console.WriteLine("- | Operator");
bool sample2 = ReturnTrue() | ReturnFalse();
Console.WriteLine("true | false = " + sample2);
Console.WriteLine();
Console.WriteLine("- ^ Operator");
bool sample3 = ReturnTrue() ^ ReturnTrue();
Console.WriteLine("true ^ true = " + sample3);
Console.WriteLine();
Console.WriteLine("- ! Operator");
bool sample4 = !ReturnTrue();
Console.WriteLine("!true = " + sample4);
Console.WriteLine();
Console.WriteLine("- && Operator");
bool sample5 = ReturnFalse() && ReturnTrue();
Console.WriteLine("false && true = " + sample5);
Console.WriteLine();
Console.WriteLine("- || Operator");
bool sample6 = ReturnTrue() || ReturnFalse();
Console.WriteLine("true && false = " + sample6);
Console.WriteLine();
}
void ConditionalOperators()
{
Console.WriteLine("CONDITIONAL OPERATOR");
Console.WriteLine("- ? Operator");
string result = (5 > 3) ? "greater" : "shorter";
Console.WriteLine("(5>3) ? \"greater\" : \"shorter\" = " + result);
Console.WriteLine();
Console.WriteLine("- ?? Operator");
object result2 = ReturnNull() ?? "object was null";
Console.WriteLine("ReturnNull() == \"object was null\" = " + result2.ToString());
Console.WriteLine();
Console.WriteLine("- ?? Operator");
object result3 = ReturnNotNull() ?? "object was null";
Console.WriteLine("ReturnNotNull() == \"object was null\" = " + result3.ToString());
Console.WriteLine();
}
bool ReturnTrue()
{
Console.WriteLine(".. inside ReturnTrue ..");
return true;
}
bool ReturnFalse()
{
Console.WriteLine(".. inside ReturnFalse ..");
return false;
}
object ReturnNull()
{
Console.WriteLine(".. inside ReturnNull ..");
return null;
}
object ReturnNotNull()
{
Console.WriteLine(".. inside ReturnNotNull ..");
return "Hello World!";
}
}
}