This chapter introduces Control Structures (also known as Loops) to meet the following objectives:
(i) Condition Statements
C# supports three kinds of Contol Structures. They are
(i)Condiitional Statements, (ii)Iteration Statements, and
(iii)Jump Statements
for developing and controlling the execution of the program.
//Example Listing 4.1
using System;
namespace QSSTraining
{
///
/// if else statement
///
public class Program
{
public static void Main(string[] args)
{
int a = 10;
int b = 20;
int max = 0;
if (a > b)
{
max = a;
}
else
{
max = b;
}
Console.WriteLine("Maximum value = {0}", max);
}
}
}
Ternary Operator: C# provides a ternary operator that is an alternative to the if… else statement.
//Example Listing 4.2
using System;
namespace QSSTraining
{
///
/// ternary operator statement
///
public class Program
{
public static void Main(string[] args)
{
int a = 10;
int b = 20;
int max = 0;
max = a > b ? a : b;
Console.WriteLine("Maximum value = {0}", max);
}
}
}
Switch Case statement:
A switch case statement acts much like multiple if... else statements.
//Example Listing 4.3
using System;
namespace QSSTraining
{
///
/// switch case statement
///
public class Program
{
//SwitchCase
public static void Main(string[] args)
{
Console.Write("Enter State Code: ");
string stateCode = Console.ReadLine();
switch (stateCode)
{
case "AL":
Console.WriteLine("Alabama");
break;
case "GA":
Console.WriteLine("Georgia");
break;
case "FL":
Console.WriteLine("Florida");
break;
case "TX":
Console.WriteLine("Texas");
break;
default:
Console.WriteLine("No valid state");
break;
}
}
}
}
//Example Listing 4.4
using System;
namespace QSSTraining
{
///
/// while Loop
///
public class Program
{
public static void Main(string[] args)
{
int i = 0;
while (i < 10)
{
Console.WriteLine("i = " + i);
i++;
}
}
}
}
//Example Listing 4.5
using System;
namespace QSSTraining
{
///
/// Iteration Statements
///
public class Program
{
// do while Loop
public static void Main(string[] args)
{
int i = 0;
do
{
Console.WriteLine("i = " + i);
i++;
}
while (i < 10);
}
}
}
//Example Listing 4.6
using System;
namespace QSSTraining
{
///
/// Iteration Statements
///
public class Program
{
// for Loop
public static void Main(string[] args)
{
int iCount = 10;
for (int i = 0; i < iCount; i++)
{
Console.WriteLine("The Numbers are: {0}", i);
}
}
}
}
//Example Listing 4.7
using System;
namespace QSSTraining
{
///
/// Iteration Statements
///
public class Program
{
// foreach Loop
public static void Main(string[] args)
{
string[] arrNames = { "Kirby", "Robert", "Neil", "Victoria" };
Console.WriteLine("The List of Names: ");
foreach (string names in arrNames)
{
Console.WriteLine("{0}", names);
}
}
}
}
//Example Listing 4.8
using System;
namespace QSSTraining
{
///
/// Jump Statements
///
public class Program
{
// break
public static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
{
if (i == 5)
break;
Console.WriteLine("i = " + i);
}
}
}
}
//Example Listing 4.9
using System;
namespace QSSTraining
{
///
/// Jump Statements
///
public class Program
{
// continue
public static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
{
if (i == 5)
continue;
Console.WriteLine("i = " + i);
}
}
}
}
//Example Listing 4.10
using System;
namespace QSSTraining
{
///
/// Jump Statements
///
public class Program
{
// goto
public static void Main(string[] args)
{
Console.WriteLine("Usage of goto");
goto MyLabel;
Console.WriteLine("This statement doesn't execute");
MyLabel:
Console.WriteLine("Jumped into MyLabel statement");
}
}
}
//Example Listing 4.11
using System;
namespace QSSTraining
{
///
/// Jump Statements
///
public class Program
{
// return
public static int Main(string[] args)
{
Console.WriteLine("Exiting from the Application");
return 0;
}
}
}