Home

Formulating Expressions

 

Counting and Looping

 

While a Statement is True

The C# language provides a set of control statements that allows you to conditionally control data input and output. These controls are referred to as loops.

The while statement examines or evaluates a condition. The syntax of the while statement is:

while(Condition) Statement;
Flowchart: while
  

To execute this expression, the compiler first examines the Condition. If the Condition is true, then it executes the Statement. After executing the Statement, the Condition is checked again. AS LONG AS the Condition is true, it will keep executing the Statement. When or once the Condition becomes false, it exits the loop.

Here is an example:

int Number;
	
while( Number <= 12 )
{
	Console.Write("Number ");
	Console.WriteLine(Number);
	Number++;
}

To effectively execute a while condition, you should make sure you provide a mechanism for the compiler to use a get a reference value for the condition, variable, or expression being checked. This is sometimes in the form of a variable being initialized although it could be some other expression. Such a while condition could be illustrated as follows:

Second while Flowchart

An example would be:

using System;

class NewProject
{
	static void Main()
	{
		int Number = 0;
	
		while( Number <= 12 )
		{
			Console.Write("Number ");
			Console.WriteLine(Number);
			Number++;
		}
		
		Console.WriteLine();
	}
}

This would produce:

Number 0
Number 1
Number 2
Number 3
Number 4
Number 5
Number 6
Number 7
Number 8
Number 9
Number 10
Number 11
Number 12

Press any key to continue
 

Practical LearningPractical Learning: Introducing Conditional Statements

  1. Start Microsoft Visual C# or Visual Studio .NET
  2. On the main menu, click File -> New -> Project
  3. If necessary, in the Project Types list of the New Project dialog box, click Visual C# Projects
  4. In the Templates list, click Empty Project
  5. In the Name text box, replace the name with DeptStore1 and specify the desired path in the Location
  6. Click OK
  7. To create a source file for the project, on the main menu, click Project -> Add New Item...
  8. In the Add New Item dialog box, in the Templates list, click Code File
  9. In the Name text box, delete the suggested name and replace it with Exercise
  10. Click Open
  11. In the empty file, type the following:
     
    using System;
    
    enum TypeOfApplication
    {
        NewDriversLicense = 1,
        UpgradeFromIDCard,
        TransferFromAnotherState
    };
    
    class Exercise
    {
        static void Main()
        {
    	TypeOfApplication AppType;
            	string FirstName, LastName;
    	string OrganDonorAnswer;
    	char Sex;
    	string Gender;
    	char DLClass;
    	
    	Console.WriteLine(" -=- Motor Vehicle Administration -=-");
    	Console.WriteLine(" --- Driver's License Application ---");
    
    	Console.WriteLine(" - Select the type of application -");
    	Console.WriteLine("1 - Applying for a brand new Driver's License");
    	Console.WriteLine("2 - Applicant already had an ID Card and is applying for a Driver's License");
    	Console.WriteLine("3 - Applicant is transferring his/her Driver's License from another state");
    	Console.Write("Your Choice: ");
    	int type = int.Parse(Console.ReadLine());
    
    	Console.Write("First Name: ");
    	FirstName = Console.ReadLine();
    	Console.Write("Last Name:  ");
    	LastName = Console.ReadLine();
    
    	Console.Write("Sex(F=Female/M=Male): ");
    	Sex = char.Parse(Console.ReadLine());
    
    	Console.WriteLine(" - Driver's License Class -");
    	Console.WriteLine("A - All Non-commercial vehicles except motorcycles");
    	Console.WriteLine("B - Non-commercial vehicles up to and including 26,001/more lbs.");
    	Console.WriteLine("C - Cars, pick-up trucks, non-commercial vehicles 26,000 lbs.");
    	Console.WriteLine("K - Mopeds");
    	Console.WriteLine("M - Motorcycles");
    	Console.Write("Your Choice: ");
    	DLClass = char.Parse(Console.ReadLine());
    
    	Console.Write("Are you willing to be an Organ Donor(1=Yes/0=No)? ");
    	OrganDonorAnswer = Console.ReadLine();
    
    	if( Sex == 'f' )
    	    Gender = "Female";
    	else if( Sex == 'F' )
    	    Gender = "Female";
    	else if( Sex == 'm' )
    	    Gender = "Male";
    	else if( Sex == 'M' )
    	    Gender = "Male";
    	else
    	    Gender = "Unknown";
    
    	Console.WriteLine("\n -=- Motor Vehicle Administration -=-");
    	Console.WriteLine(" --- Driver's License Information ---");
    
    	AppType = (TypeOfApplication)type;
    	Console.Write("Type of Application: ");
    	switch(AppType)
    	{
    	case TypeOfApplication.NewDriversLicense:
    	    Console.WriteLine("New Driver's License");
    	    break;
        	case TypeOfApplication.UpgradeFromIDCard:
    	    Console.WriteLine("Upgrade From Identity Card");
    	    break;
        	case TypeOfApplication.TransferFromAnotherState:
    	    Console.WriteLine("Transfer From Another State");
    	    break;
    	default:
    	    Console.WriteLine("Not Specified");
    	    break;
    	}
    
    	Console.WriteLine("Full Name:   {0} {1}", FirstName, LastName);
    	Console.WriteLine("Sex:         {0}", Gender);
    
    	switch(DLClass)
    	{
    	case 'a':
    	case 'A':
    	    Console.WriteLine("Class:       A");
    	    break;
    	case 'b':
    	case 'B':
    	    Console.WriteLine("Class:       B");
    	    break;
    	case 'c':
    	case 'C':
    	    Console.WriteLine("Class:       C");
    	    break;
    	case 'k':
    	case 'K':
    	    Console.WriteLine("Class:       K");
    	    break;
    	case 'm':
    	case 'M':
    	    Console.WriteLine("Class:       M");
    	    break;
    	default:
    	    Console.WriteLine("Class:       Unknown");
    	    break;
    	}
    
    	Console.Write("Organ Donor? ");
    	Console.WriteLine(OrganDonorAnswer == "1" ? "Yes" : (OrganDonorAnswer == "0" ? "No" : "Invalid Answer"));
    
            return 0;
        }
    }
  12. To execute the application, on the main menu, click Debug -> Start Without Debugging. Here is an example:
     
    -=- Motor Vehicle Administration -=-
     --- Driver's License Application ---
     - Select the type of application -
    1 - Applying for a brand new Driver's License
    2 - Applicant already had an ID Card and is applying for a Driver's License
    3 - Applicant is transferring his/her Driver's License from another state
    Your Choice: 6
    First Name: Henry
    Last Name:  Dricks
    Sex(F=Female/M=Male): P
     - Driver's License Class -
    A - All Non-commercial vehicles except motorcycles
    B - Non-commercial vehicles up to and including 26,001/more lbs.
    C - Cars, pick-up trucks, non-commercial vehicles 26,000 lbs.
    K - Mopeds
    M - Motorcycles
    Your Choice: G
    Are you willing to be an Organ Donor(1=Yes/0=No)? 4
    
     -=- Motor Vehicle Administration -=-
     --- Driver's License Information ---
    Type of Application: Not Specified
    Full Name:   Henry Dricks
    Sex:         Unknown
    Class:       Unknown
    Organ Donor? Invalid Answer
  13. After using it, Return to Visual C#

The do...while Statement

The do…while statement uses the following syntax:

do Statement while (Condition);
 
  

The do…while condition executes a Statement first. After the first execution of the Statement, it examines the Condition. If the Condition is true, then it executes the Statement again. It will keep executing the Statement AS LONG AS the Condition is true. Once the Condition becomes false, the looping (the execution of the Statement) would stop.

If the Statement is a short one, such as made of one line, simply write it after the do keyword. Like the if and the while statements, the Condition being checked must be included between parentheses. The whole do…while statement must end with a semicolon.

Another version of the counting program seen previously would be:

using System;

class NewProject
{
	static void Main()
	{
		int Number = 0;
	
		do {
			Console.Write("Number ");
			Console.WriteLine(Number);
			Number++;
		}while( Number <= 12 );
		
		Console.WriteLine();
	}
}

If the Statement is long and should span more than one line, start it with an opening curly bracket and end it with a closing curly bracket.

The do…while statement can be used to insist on getting a specific value from the user. For example, since our ergonomic program would like the user to sit down for the subsequent exercise, you can modify your program to continue only once she is sitting down. Here is an example on how you would accomplish that:

using System;

class NewProject
{
	static void Main()
	{
		char SittingDown;
		string SitDown;
		
		Console.Write("For the next exercise, you need to be sitting down\n");
		
		do {
			Console.Write("Are you sitting down now(y/n)? ");
			SitDown = Console.ReadLine();
			SittingDown = char.Parse(SitDown);
		} while( !(SittingDown == 'y') );
		
		Console.WriteLine();
	}
}

Here is an example of running the program:

For the next exercise, you need to be sitting down
Are you sitting down now(y/n)? t
Are you sitting down now(y/n)? h
Are you sitting down now(y/n)? G
Are you sitting down now(y/n)? y

Practical Learning Practical Learning: Using do...While

  1. Change the file as follows:
     
    using System;
    
    enum TypeOfApplication
    {
        NewDriversLicense = 1,
        UpgradeFromIDCard,
        TransferFromAnotherState
    };
    
    class Exercise
    {
        static void Main()
        {
    	TypeOfApplication AppType;
    	int type;
            	string FirstName, LastName;
    	string OrganDonorAnswer;
    	char Sex;
    	string Gender;
    	char DLClass;
    	
    	Console.WriteLine(" -=- Motor Vehicle Administration -=-");
    	Console.WriteLine(" --- Driver's License Application ---");
    	
    	do {
                        Console.WriteLine(" - Select the type of application -");
    	    Console.WriteLine("1 - Applying for a brand new Driver's License");
    	    Console.WriteLine("2 - Applicant already had an ID Card and is applying for a Driver's License");
    	    Console.WriteLine("3 - Applicant is transferring his/her Driver's License from another state");
    	    Console.Write("Your Choice: ");
    	    type = int.Parse(Console.ReadLine());
    	    Console.WriteLine("");
    	} while(type > 3);
    
    	Console.Write("First Name: ");
    	FirstName = Console.ReadLine();
    	Console.Write("Last Name:  ");
    	LastName = Console.ReadLine();
    
    	Console.Write("\nSex(F=Female/M=Male): ");
    	Sex = char.Parse(Console.ReadLine());
    
    	Console.WriteLine("\n - Driver's License Class -");
    	Console.WriteLine("A - All Non-commercial vehicles except motorcycles");
    	Console.WriteLine("B - Non-commercial vehicles up to and including 26,001/more lbs.");
    	Console.WriteLine("C - Cars, pick-up trucks, non-commercial vehicles 26,000 lbs.");
    	Console.WriteLine("K - Mopeds");
    	Console.WriteLine("M - Motorcycles");
    	Console.Write("Your Choice: ");
    	DLClass = char.Parse(Console.ReadLine());
    
    	Console.Write("\nAre you willing to be an Organ Donor(1=Yes/0=No)? ");
    	OrganDonorAnswer = Console.ReadLine();
    
    	if( Sex == 'f' )
    	    Gender = "Female";
    	else if( Sex == 'F' )
    	    Gender = "Female";
    	else if( Sex == 'm' )
    	    Gender = "Male";
    	else if( Sex == 'M' )
    	    Gender = "Male";
    	else
    	    Gender = "Unknown";
    
    	Console.WriteLine("\n -=- Motor Vehicle Administration -=-");
    	Console.WriteLine(" --- Driver's License Information ---");
    
    	AppType = (TypeOfApplication)type;
    	Console.Write("Type of Application: ");
    	switch(AppType)
    	{
    	case TypeOfApplication.NewDriversLicense:
    	    Console.WriteLine("New Driver's License");
    	    break;
        	case TypeOfApplication.UpgradeFromIDCard:
    	    Console.WriteLine("Upgrade From Identity Card");
    	    break;
        	case TypeOfApplication.TransferFromAnotherState:
    	    Console.WriteLine("Transfer From Another State");
    	    break;
    	default:
    	    Console.WriteLine("Not Specified");
    	    break;
    	}
    
    	Console.WriteLine("Full Name: {0} {1}", FirstName, LastName);
    	Console.WriteLine("Sex:           {0}", Gender);
    
    	switch(DLClass)
    	{
    	case 'a':
    	case 'A':
    	    Console.WriteLine("Class:       A");
    	    break;
    	case 'b':
    	case 'B':
    	    Console.WriteLine("Class:       B");
    	    break;
    	case 'c':
    	case 'C':
    	    Console.WriteLine("Class:       C");
    	    break;
    	case 'k':
    	case 'K':
    	    Console.WriteLine("Class:       K");
    	    break;
    	case 'm':
    	case 'M':
    	    Console.WriteLine("Class:       M");
    	    break;
    	default:
    	    Console.WriteLine("Class:       Unknown");
    	    break;
    	}
    
    	Console.Write("Organ Donor? ");
    	Console.WriteLine(OrganDonorAnswer == "1" ? "Yes" : (OrganDonorAnswer == "0" ? "No" : "Invalid Answer"));
    
            return 0;
        }
    }
  2. Save, compile, and test it. Here is an example:
     
    -=- Motor Vehicle Administration -=-
     --- Driver's License Application ---
     - Select the type of application -
    1 - Applying for a brand new Driver's License
    2 - Applicant already had an ID Card and is applying for a Driver's License
    3 - Applicant is transferring his/her Driver's License from another state
    Your Choice: 6
    
     - Select the type of application -
    1 - Applying for a brand new Driver's License
    2 - Applicant already had an ID Card and is applying for a Driver's License
    3 - Applicant is transferring his/her Driver's License from another state
    Your Choice: 8
    
     - Select the type of application -
    1 - Applying for a brand new Driver's License
    2 - Applicant already had an ID Card and is applying for a Driver's License
    3 - Applicant is transferring his/her Driver's License from another state
    Your Choice: 3
    
    First Name: Alexandria
    Last Name:  McArthur
    
    Sex(F=Female/M=Male): M
    
     - Driver's License Class -
    A - All Non-commercial vehicles except motorcycles
    B - Non-commercial vehicles up to and including 26,001/more lbs.
    C - Cars, pick-up trucks, non-commercial vehicles 26,000 lbs.
    K - Mopeds
    M - Motorcycles
    Your Choice: C
    
    Are you willing to be an Organ Donor(1=Yes/0=No)? 0
    
     -=- Motor Vehicle Administration -=-
     --- Driver's License Information ---
    Type of Application: Transfer From Another State
    Full Name:   Alexandria McArthur
    Sex:         Male
    Class:       C
    Organ Donor? No
  3. Return to Visual C#

The for Statement

The for statement is typically used to count a number of items. At its regular structure, it is divided in three parts. The first section specifies the starting point for the count. The second section sets the counting limit. The last section determines the counting frequency. The syntax of the for statement is:

for(Start; End; Frequency) Statement;

The Start expression is a variable assigned the starting value. This could be Count = 0;

The End expression sets the criteria for ending the counting. An example would be Count < 24; which means that the counting would continue as long as the Count variable is less than 24. When the count is about to rich 24, because in this case 24 is excluded, the counting would stop. To include the counting limit, use the <= or >= comparison operators depending on how you are counting.

The Frequency expression lets the compiler know how many numbers to add or subtract before continuing with the loop. This expression could be an increment operation such as ++Count.

Here is an example that applies the for statement:

using System;

class NewProject
{
	static void Main()
	{
		for(int Count = 0; Count <= 12; Count++)
		{
			Console.Write("Number ");
			Console.WriteLine(Count);
		}
		
		Console.WriteLine();
	}
}

A variable declared as the counter of a for loop is available only in that for loop. This means that the scope of the counting variable is confined only to the for loop. This allows different for loops to use the same counter variable. Here is an example:

using System;

class NewProject
{
	static void Main()
	{
		
		for(int Count = 0; Count <= 12; Count++)
		{
			Console.Write("Number ");
			Console.WriteLine(Count);
		}
		
		for(int Count = 10; Count >= 2; Count--)
		{
			Console.Write("Number ");
			Console.WriteLine(Count);
		}
		
		Console.WriteLine();
	}
}

Techniques of Writing Conditional Statements

 

Conditional Nesting

A condition can be created inside of another to write a more effective statement. This is referred to as nesting one condition inside of another. Almost any condition can be part of another and multiple conditions can be included inside of others.

As we have learned, different conditional statements are applied in specific circumstances. In some situations, they are interchangeable or one can be applied just like another, which becomes a matter of choice. Statements can be combined to render a better result with each playing an appropriate role.

Here is an example of an if condition nested inside of a do...while loop:

using System;

class NewProject
{
    static void Main()
    {
	char SittingDown;
	string SitDown;

	do {
	    Console.Write("Are you sitting down now(y/n)? ");
	    SitDown = Console.ReadLine();
	    SittingDown = char.Parse(SitDown);
		
	    if( SittingDown != 'y' )
		Console.WriteLine("Could you please sit down for the next exercise? ");
	} while( !(SittingDown == 'y') );
		
	Console.WriteLine();
    }
}

Here is an example of running the program:

Are you sitting down now(y/n)? n
Could you please sit down for the next exercise?
Are you sitting down now(y/n)? g
Could you please sit down for the next exercise?
Are you sitting down now(y/n)? y

One of the reasons you would need to nest conditions is because one would lead to another. Sometimes, before checking one condition, another primary condition would have to be met. The ergonomic program we have been simulating so far is asking the user whether she is sitting down. Once the user is sitting down, you would write an exercise she would perform. Depending on her strength, at a certain time, one user will be tired and want to stop while for the same amount of previous exercises, another user would like to continue. Before continuing with a subsequent exercise, you may want to check whether the user would like to continue. Of course, this would be easily done with:

using System;

class NewProject
{
    static void Main()
    {
	char SittingDown;
	string SitDown;

	do {
		Console.Write("Are you sitting down now(y/n)? ");
		SitDown = Console.ReadLine();
		SittingDown = char.Parse(SitDown);
		
		if( SittingDown != 'y' )
			Console.WriteLine("Could you please sit down for the next exercise? ");
	} while( !(SittingDown == 'y') );
		
	Console.Write("Wonderful. Now we will continue today's exercise...");
	Console.WriteLine("\n...\nEnd of exercise");
	
	char WantToContinue;
	
	Console.Write("\nDo you want to continue(y=Yes/n=No)? ");
	string ToContinue = Console.ReadLine();
	WantToContinue = char.Parse(ToContinue);

	Console.WriteLine();
    }
}

If the user answers No, you can stop the program. If she answers Yes, you would need to continue the program with another exercise. Because the user answered Yes, the subsequent exercise would be included in the previous condition because it does not apply for a user who wants to stop. In this case, one “if” could be inserted inside of another. Here is an example:

using System;

class NewProject
{
	static void Main()
	{
		char SittingDown;
		string SitDown;
	
		do 
		{
			Console.Write("Are you sitting down now(y/n)? ");
			SitDown = Console.ReadLine();
			SittingDown = char.Parse(SitDown);
		
			if( SittingDown != 'y' )
				Console.WriteLine("Could you please sit down for the next exercise? ");
		}while( SittingDown != 'y' );
	
		Console.WriteLine("Wonderful. Now we will continue today's exercise...");
		Console.WriteLine("\n...\nEnd of exercise\n");
	
		char WantToContinue;
	
		Console.Write("\nDo you want to continue(1=Yes/0=No)? ");
		string ToContinue = Console.ReadLine();
		WantToContinue = char.Parse(ToContinue);
	
		if(WantToContinue == '1')
		{
			char LayOnBack;
		
			Console.WriteLine("Good. For the next exercise, you should lay on your back");
			Console.Write("Are you laying on your back(1=Yes/0=No)? ");
			string Lay = Console.ReadLine();
			LayOnBack = char.Parse(Lay);
		
			if(LayOnBack == '1')
				Console.WriteLine("Great.\nNow we will start the next exercise.");
			else
				Console.WriteLine("\nWell, it looks like you are getting tired...");
		}
		else
			Console.WriteLine("We had enough today");
	
		Console.WriteLine("We will stop the session now\nThanks.\n");
	}
}

Practical Learning Practical Learning: Nesting Conditions

  1. Change the file as follows:
     
    using System;
    
    enum TypeOfApplication
    {
        NewDriversLicense = 1,
        UpgradeFromIDCard,
        TransferFromAnotherState
    };
    
    class Exercise
    {
        static void Main()
        {
    	. . . No Change
    	
    	Console.WriteLine(" -=- Motor Vehicle Administration -=-");
    	Console.WriteLine(" --- Driver's License Application ---");
    
    	do {
                	    Console.WriteLine(" - Select the type of application -");
    	    Console.WriteLine("1 - Applying for a brand new Driver's License");
    	    Console.WriteLine("2 - Applicant already had an ID Card and is applying for a Driver's License");
    	    Console.WriteLine("3 - Applicant is transferring his/her Driver's License from another state");
    	    Console.Write("Your Choice: ");
    	    type = int.Parse(Console.ReadLine());
    	
    	    if( type > 3 )
    		Console.WriteLine("Please enter a valid number as 1, 2, or 3");
    	} while(type > 3);
    	
    
    	. . . No Change
    
    	
            return 0;
        }
    }
  2. Save, compile, and test the program

The break Statement

The break statement is used to stop a loop for any reason or condition the programmer considers fit. The break statement can be used in a while condition to stop an ongoing action. The syntax of the break statement is simply:

break;

Although made of only one word, the break statement is a complete statement. Therefore, it can (and should always) stay on its own line (this makes the program easy to read). The break statement applies to the most previous conditional statement to it, provided that previous statement is applicable. The break statement is typically used to handle the cases in a switch statement as we saw it in the previous lesson.

Besides the switch statement, the break statement can also be used in other conditional statements. For example, you can use a break statement inside of a for loop to interrupt its count. The following program would display letter from d to n but it is interrupted by a break when it encounters k:

using System;

class Exercise
{
	static void Main()
	{
		for (char c = 'd'; c <= 'n'; c++) 
		{
			if (c == 'k') 
				break;
			Console.WriteLine(c);
		}

		Console.WriteLine();
		return 0;
	}
}

This would produce:

d
e
f
g
h
i
j

The break statement can also be used in a do…while or a for loop the same way. 

 

The continue Statement

The continue statement uses the following formula:

continue;

When processing a loop, if the statement finds a false value, you can use the continue statement inside of a while, do…while or a for conditional statements to ignore the subsequent statement or to jump from a false Boolean value to the subsequent valid value, unlike the break statement that would exit the loop. Like the break statement, the continue keyword applies to the most previous conditional statement and should stay on its own line.

The following programs asks the user to type 4 positive numbers and calculates the sum of the numbers by considering only the positive ones. If the user types a negative number, the program manages to ignore the numbers that do not fit in the specified category:

using System;

class Exercise
{
	static void Main()
	{
		// Declare necessary variables
		double Number, Sum = 0;
		string Nbr;
	
		// Request 4 positive numbers from the user
		Console.WriteLine("Type 4 positive numbers.");
		// Make sure the user types 4 positive numbers
		for( int Count = 1; Count <= 4; Count++ )
		{
			Console.Write("Number: ");
			Nbr = Console.ReadLine();
			Number = double.Parse(Nbr);
		
			// If the number typed is not positive, ignore it
			if( Number < 0 )
				continue;
		
			// Add each number to the sum
			Sum += Number;
		}
	
		// Display the sum
		Console.WriteLine("Sum of the numbers you entered = {0}\n", Sum);
		return 0;
	}
}

This would produce:

Type 4 positive numbers.
Number: 24.55
Number: 826.08
Number: 1450.85
Number: 262.58
Sum of the numbers you entered = 2564.06

The goto Statement

The goto statement allows a program execution to jump to another section of the function in which it is being used. In order to use the goto statement, insert a name on a particular section of your function so you can refer to that name. The name, also called a label, is made of one word and follows the rules we have learned about C# names (the name can be anything), then followed by a colon.

The following program uses a for loop to count from 0 to 12, but when it encounters 5, it jumps to a designated section of the program:

using System;

class NewProject
{
	static void Main()
	{
		for(int Count = 0; Count <= 12; ++Count)
		{
			Console.WriteLine("Count {0}", Count);
		
			if( Count == 5 )
				goto MamaMia;
		}

		MamaMia:
			Console.WriteLine("\nStopped at 5");

		Console.WriteLine();
	}
}

This would produce:

Count 0
Count 1
Count 2
Count 3
Count 4
Count 5

Stopped at 5

Logical Operations on Statements

 

Introduction

The conditional statements we have used so far were applied to single situations. You can combine statements using techniques of logical operations to create more complex and complete expressions. One way to do this is by making sure that two conditions are met for the whole expression to be true. On the other hand, one or the other of two conditions can produce a true condition, as long as one of them is true. This is done with logical conjunction or disjunction.

In the beginning of the previous lesson, we described ways to write clear, simple, and logical statements. The techniques are even more important when combining statements because, once again, you should be able to make a decision based on a short, truthful, and clear statement.

Logical Conjunction: AND

One of the operations used in combining two statements consists of validating an expression made of two parts only if both parts of the statement are true. Imagine that a sport camp states on an application form that an interested candidate must be "a man at least 18 years old". This condition can be divided in two statements as follows:

  1. A - The applicant is a man
  2. B - The applicant is at least 18 years old

The program to process an application can look like this:

using System;

class SportCamp
{
	static void Main()
	{
		string FullName;
		int Gender;
		int Age;

		Console.WriteLine("Enter the following pieces of information");
		Console.Write("Full Name: ");
		FullName = Console.ReadLine();
		Console.Write("Gender(1=Male/0=Female): ");
		Gender = int.Parse(Console.ReadLine());
		Console.Write("Age: ");
		Age = int.Parse(Console.ReadLine());

		Console.WriteLine();
	}
}

Here is an example of running the program:

Enter the following pieces of information
Full Name: Ernestine Monay
Gender(1=Male/0=Female): 6
Age: 16

Every time a new person has filled an application, his or her information would be evaluated according to the above statements:

  • If the applicant is a man, the first statement, represented here as A would be true
  • If the applicant is 18 or older, the second statement will be rendered true.

If we evaluate these statement separately, we can tabulate them as follows:

A
 
B
 

Suppose an applicant answers the first question as being a female, but the statement on the application said that an applicant must be a male. Once this statement produces a false result. We don't care how old the applicant is, the whole statement would be false. This would produce the following table:

A B Statement
False Don't Care False

Here is an example of processing and validating the first question:

using System;

class SportCamp
{
	static void Main()
	{
		string FullName;
		int Gender;
		int Age;

		Console.WriteLine("Enter the following pieces of information");
		Console.Write("Full Name: ");
		FullName = Console.ReadLine();
		Console.Write("Gender(1=Male/0=Female): ");
		Gender = int.Parse(Console.ReadLine());

		if( Gender == 1 )
		{
			// If the applicant is a man, then we can continue
			Console.Write("Age: ");
			Age = int.Parse(Console.ReadLine());
		}
		else // If the applicant is not a man, let the applicant know
			Console.WriteLine("You don't qualify");

		Console.WriteLine();
	}
}

Here is an example of running the program:

Enter the following pieces of information
Full Name: Samuel Golfer
Gender(1=Male/0=Female): 1
Age: 16

Here is another example of running the same program:

Enter the following pieces of information
Full Name: Germain Gobb
Gender(1=Male/0=Female): 6
You don't qualify

If an application answers the first question and indicates that he is a man, then we can process the second question. If the applicant indicates that he is younger than 18, the second statement is false. Consequently, the applicant doesn't qualify. The process this second question, we can nest a condition and change the program as follows:

using System;

class SportCamp
{
	static void Main()
	{
		string FullName;
		int Gender;
		int Age;

		Console.WriteLine("Enter the following pieces of information");
		Console.Write("Full Name: ");
		FullName = Console.ReadLine();
		Console.Write("Gender(1=Male/0=Female): ");
		Gender = int.Parse(Console.ReadLine());

		if( Gender == 1 )
		{
			// If the applicant is a man, then we can continue
			Console.Write("Age: ");
			Age = int.Parse(Console.ReadLine());

			if( Age < 18 )
				Console.WriteLine("You are not old enough!");
		}
		else // If the applicant is not a man, let the applicant know
			Console.WriteLine("You don't qualify");

		Console.WriteLine();
	}
}

Here is an example of running the program:

Enter the following pieces of information
Full Name: Joseph Davis
Gender(1=Male/0=Female): 5
You don't qualify

Here is another example of running the same program:

Enter the following pieces of information
Full Name: Thomas Lemm
Gender(1=Male/0=Female): 1
Age: 16
You are not old enough!

This would produce the following table:

A B Statement
Don't Care False False

In the same way, if the applicant answers to be a female and younger than 18, the whole statement is still false. This would produce the following table:

A B Statement
False False False

As a consequence, an applicant qualifies only if he is a male and at least 18. The program that evaluates this c an be written as follows:

using System;

class SportCamp
{
	static void Main()
	{
		string FullName;
		int Gender;
		int Age;

		Console.WriteLine("Enter the following pieces of information");
		Console.Write("Full Name: ");
		FullName = Console.ReadLine();
		Console.Write("Gender(1=Male/0=Female): ");
		Gender = int.Parse(Console.ReadLine());

		if( Gender == 1 )
		{
			// If the applicant is a man, then we can continue
			Console.Write("Age: ");
			Age = int.Parse(Console.ReadLine());

			if( Age < 18 )
				Console.WriteLine("You are not old enough!");
			else
				Console.WriteLine("Your application is valid");
		}
		else // If the applicant is not a man, let the applicant know
			Console.WriteLine("You don't qualify");

		Console.WriteLine();
	}
}

This shows that an applicant qualifies only if both statements are true.

In the above section, to evaluate related statements, we proceeded by nesting them, which made our program complete but longer. To process this type of related statements, you can combine them using the Boolean AND operator. Based on this, a statement such as "The applicant must be a man at least 18 years old" can be segmented in two statements joined as follows:

"The applicant is a man" AND "The applicant is at least 18 years old"

In C#, the logical conjunction is performed using the && operator. A combination of A AND B can be represented as follows:

A
 
B
 
A && B
 

The expression that evaluates this combined statement can be written as follows:

using System;

class SportCamp
{
	static void Main()
	{
		string FullName;
		int Gender;
		int Age;

		Console.WriteLine("Enter the following pieces of information");
		Console.Write("Full Name: ");
		FullName = Console.ReadLine();
		Console.Write("Gender(1=Male/0=Female): ");
		Gender = int.Parse(Console.ReadLine());

		Console.Write("Age: ");
		Age = int.Parse(Console.ReadLine());

		if( Gender == 1 && Age >= 18 )
			Console.WriteLine("Your application is valid");
		else
			Console.WriteLine("You don't qualify");

		Console.WriteLine();
	}
}

Based on the rules of operator, this logical operation will work. The compiler will evaluate the "Gender == 1" condition first, then it will evaluate the "Age >= 18" condition, then it will combine the first to the second. Sometime this expression can appear confusing to read. If you want, you can include each part of the combination in its own parentheses. Here is an example:

using System;

class SportCamp
{
	static void Main()
	{
		string FullName;
		int Gender;
		int Age;

		Console.WriteLine("Enter the following pieces of information");
		Console.Write("Full Name: ");
		FullName = Console.ReadLine();
		Console.Write("Gender(1=Male/0=Female): ");
		Gender = int.Parse(Console.ReadLine());

		Console.Write("Age: ");
		Age = int.Parse(Console.ReadLine());

		if( (Gender == 1) && (Age >= 18) )
			Console.WriteLine("Your application is valid");
		else
			Console.WriteLine("You don't qualify");

		Console.WriteLine();
	}
}

This makes the program easier to read.

  • If the current applicant is a woman, our first statement, represented as A, is false
  • If the same applicant is younger than 18, our second statement, represented as B, is false
  • If both statements are false, the combined statement, represented as A&&B, or C, is false.

This can be represented in a truth table as follows:

A B A && B
False False False

Based on this table, here is an example of running the program:

Enter the following pieces of information
Full Name: Germain Asse
Gender(1=Male/0=Female): 0
Age: 16
You don't qualify
  • If the current applicant is a woman, our first statement is false
  • If the same applicant is at least 18 years old, our second statement is true
  • If the first statement is false and the second is true, the combined statement is false

This can be represented in a truth table as follows:

A B A && B
False True False

Based on this table, here is an example of running the program:

Enter the following pieces of information
Full Name: Germain Asse
Gender(1=Male/0=Female): 0
Age: 24
You don't qualify

In the same way

  • If the current applicant is a man, our first statement is true
  • If the same applicant is at least 18 years old, our second statement is false
  • If the first statement is true and the second is false, the combined statement is false

This can be represented in a truth table as follows:

A B A && B
True False False

Based on this table, here is an example of running the program:

Enter the following pieces of information
Full Name: Germain Asse
Gender(1=Male/0=Female): 1
Age: 14
You don't qualify
  • If the current applicant is a man, our first statement is true
  • If the same applicant is at least 18 years old, our second statement is true
  • If both statements are true, the combined statement is true.

This results in:

A B A && B
True True True

Based on this table, here is an example of running the program:

Enter the following pieces of information
Full Name: Germain Asse
Gender(1=Male/0=Female): 1
Age: 22
Your application is valid

The above tables can be combined into one as follows:

A B A && B
False False False
False True False
True False False
True True True

 

 

Practical Learning Practical Learning: Using the Logical AND Operator

  1. To use logical conjunction, change the program as follows:
     
    using System;
    
    enum TypeOfApplication
    {
    	NewDriversLicense = 1,
    	UpgradeFromIDCard,
    	TransferFromAnotherState
    };
    
    class Exercise
    {
    	static void Main()
    	{
    		TypeOfApplication AppType;
    		int type;
    		string FirstName, LastName;
    		string OrganDonorAnswer;
    		char Sex;
    		string Gender;
    		char DLClass;
    	
    		Console.WriteLine(" -=- Motor Vehicle Administration -=-");
    		Console.WriteLine(" --- Driver's License Application ---");
    
    		do 
    		{
    			Console.WriteLine(" - Select the type of application -");
    			Console.WriteLine("1 - Applying for a brand new Driver's License");
    			Console.WriteLine("2 - Applicant already had an ID Card and is applying for a Driver's License");
    			Console.WriteLine("3 - Applicant is transferring his/her Driver's License from another state");
    			Console.Write("Your Choice: ");
    			type = int.Parse(Console.ReadLine());
    	
    			if( type > 3 )
    				Console.WriteLine("Please enter a valid number as 1, 2, or 3");
    			Console.WriteLine("");
    		} while(type > 3);
    
    		Console.Write("First Name: ");
    		FirstName = Console.ReadLine();
    		Console.Write("Last Name:  ");
    		LastName = Console.ReadLine();
    
    		do 
    		{
    			Console.Write("Sex(F=Female/M=Male): ");
    			Sex = char.Parse(Console.ReadLine());
    			if( (Sex != 'f') && (Sex != 'F') && (Sex != 'm') && (Sex != 'M') )
    				Console.WriteLine("\nInvalid Answer - Try Again");
    		} while( (Sex != 'f') && (Sex != 'F') && (Sex != 'm') && (Sex != 'M') );
    
    		if( Sex == 'f' )
    			Gender = "Female";
    		else if( Sex == 'F' )
    			Gender = "Female";
    		else if( Sex == 'm' )
    			Gender = "Male";
    		else if( Sex == 'M' )
    			Gender = "Male";
    		else
    			Gender = "Unknown";
    
    		Console.WriteLine("  - Driver's License Class -");
    		Console.WriteLine("A - All Non-commercial vehicles except motorcycles");
    		Console.WriteLine("B - Non-commercial vehicles up to and including 26,001/more lbs.");
    		Console.WriteLine("C - Cars, pick-up trucks, non-commercial vehicles 26,000 lbs.");
    		Console.WriteLine("K - Mopeds");
    		Console.WriteLine("M - Motorcycles");
    		Console.Write("Your Choice: ");
    		DLClass = char.Parse(Console.ReadLine());
    
    		do 
    		{
    			Console.Write("Are you willing to be an Organ Donor(y=Yes/n=No)? ");
    			OrganDonorAnswer = Console.ReadLine();
    			if( (OrganDonorAnswer != "y") && (OrganDonorAnswer != "Y") &&
    				(OrganDonorAnswer != "n") && (OrganDonorAnswer != "N") )
    				Console.WriteLine("\nInvalid Answer");
    		} while( (OrganDonorAnswer != "y") && (OrganDonorAnswer != "Y") &&
    		             (OrganDonorAnswer != "n") && (OrganDonorAnswer != "N") );
    
    		Console.WriteLine("\n -=- Motor Vehicle Administration -=-");
    		Console.WriteLine(" --- Driver's License Information ---");
    
    		AppType = (TypeOfApplication)type;
    		Console.Write("Type of Application: ");
    		switch(AppType)
    		{
    			case TypeOfApplication.NewDriversLicense:
    				Console.WriteLine("New Driver's License");
    				break;
    			case TypeOfApplication.UpgradeFromIDCard:
    				Console.WriteLine("Upgrade From Identity Card");
    				break;
    			case TypeOfApplication.TransferFromAnotherState:
    				Console.WriteLine("Transfer From Another State");
    				break;
    			default:
    				Console.WriteLine("Not Specified");
    				break;
    		}
    
    		Console.WriteLine("Full Name: {0} {1}", FirstName, LastName);
    		Console.WriteLine("Sex:           {0}", Gender);
    
    		switch(DLClass)
    		{
    			case 'a':
    			case 'A':
    				Console.WriteLine("Class:       A");
    				break;
    			case 'b':
    			case 'B':
    				Console.WriteLine("Class:       B");
    				break;
    			case 'c':
    			case 'C':
    				Console.WriteLine("Class:       C");
    				break;
    			case 'k':
    			case 'K':
    				Console.WriteLine("Class:       K");
    				break;
    			case 'm':
    			case 'M':
    				Console.WriteLine("Class:       M");
    				break;
    			default:
    				Console.WriteLine("Class:       Unknown");
    				break;
    		}
    
    		Console.Write("Organ Donor? ");
    		Console.WriteLine(OrganDonorAnswer == "1" ? "Yes" : (OrganDonorAnswer == "0" ? "No" : "Invalid Answer"));
    
    		return 0;
    	}
    }
  2. Save, compile, and test the program. Here is an example:
     
    -=- Motor Vehicle Administration -=-
     --- Driver's License Application ---
     - Select the type of application -
    1 - Applying for a brand new Driver's License
    2 - Applicant already had an ID Card and is applying for a Driver's License
    3 - Applicant is transferring his/her Driver's License from another state
    Your Choice: 6
    Please enter a valid number as 1, 2, or 3
    
     - Select the type of application -
    1 - Applying for a brand new Driver's License
    2 - Applicant already had an ID Card and is applying for a Driver's License
    3 - Applicant is transferring his/her Driver's License from another state
    Your Choice: 3
    
    First Name: Larry
    Last Name:  Edmonston
    Sex(F=Female/M=Male): g
    
    Invalid Answer - Try Again
    Sex(F=Female/M=Male): m
      - Driver's License Class -
    A - All Non-commercial vehicles except motorcycles
    B - Non-commercial vehicles up to and including 26,001/more lbs.
    C - Cars, pick-up trucks, non-commercial vehicles 26,000 lbs.
    K - Mopeds
    M - Motorcycles
    Your Choice: b
    Are you willing to be an Organ Donor(1=Yes/0=No)? 8
    
    Invalid Answer
    Are you willing to be an Organ Donor(1=Yes/0=No)? 1
    
     -=- Motor Vehicle Administration -=-
     --- Driver's License Information ---
    Type of Application: Transfer From Another State
    Full Name:   Larry Edmonston
    Sex:         Male
    Class:       B
    Organ Donor? Yes
    Press any key to continue
  3. Return to Visual C#

Logical Disjunction: OR

Imagine you are looking for a job and you come to an ad that states, "Programmer needed, an applicant should be a Microsoft Certified Application Developer (MCAD). If you are not an MCAD, you must have at least a bachelor's degree". This announcement can be resumed to the following statement: "The applicant must either be an MCAD or must have a bachelor's degree". It can be further segmented in two sub-statements as follows:

  • The applicant is an MCAD
  • The application has a bachelor's degree

The program to process an application can appear as follows:

using System;

class JobApplication
{
	static void Main()
	{
		string FullName;
		int MCAD;
		int Degree;

		Console.WriteLine("Enter the following pieces of information");
		Console.Write("Applicant's Name: ");
		FullName = Console.ReadLine();
		Console.Write("Are you an MCAD (1=Yes/0=No)? ");
		MCAD = int.Parse(Console.ReadLine());
		Console.Write("Do you hold a bachelor's degree (1=Yes/0=No)? ");
		Degree = int.Parse(Console.ReadLine());

		Console.WriteLine();
	}
}

Here is an example of running the program:

Enter the following pieces of information
Applicant's Name: Paul Brownies
Are you an MCAD (1=Yes/0=No)? 4
Do you hold a bachelor's degree (1=Yes/0=No)? 6

When a new applicant applies for a job, his or her information is evaluated:

  • If the applicant is an MCAD, the first statement, represented here as A, is true
  • If the applicant has a bachelor's degree, the second statement, represented as B, is true

If an applicant indicates in the first question that he or she is an MCAD, we wouldn't care about the second question. This resulting statement can be represented as follows:

A B Statement
True Don't Care True

Here is an example of processing and validating the first question:

using System;

class JobApplication
{
	static void Main()
	{
		string FullName;
		int MCAD;
		int Degree;

		Console.WriteLine("Enter the following pieces of information");
		Console.Write("Applicant's Name: ");
		FullName = Console.ReadLine();
		Console.Write("Are you an MCAD (1=Yes/0=No)? ");
		MCAD = int.Parse(Console.ReadLine());
		
		if( MCAD == 1 )
			Console.WriteLine("Since you are an MCAD, you qualify");

		Console.WriteLine();
	}
}

Here is an example of running the program:

Enter the following pieces of information
Applicant's Name: Saul Kemmel
Are you an MCAD (1=Yes/0=No)? 4

Here is another example of running the same program:

Enter the following pieces of information
Applicant's Name: Saul Kemmel
Are you an MCAD (1=Yes/0=No)? 1
Since you are an MCAD, you qualify

If the answer to the first question indicates that an applicant is not an MCAD, then we would be interested in the answer to the second question. If the second answers shows that the applicant has a bachelor's degree, then he or she qualifies. In fact in this case, the answer to the first question would not be important:

A B Statement
Don't Care True True

The second question can be addressed as follows:

using System;

class JobApplication
{
	static void Main()
	{
		string FullName;
		int MCAD;
		int Degree;

		Console.WriteLine("Enter the following pieces of information");
		Console.Write("Applicant's Name: ");
		FullName = Console.ReadLine();
		Console.Write("Are you an MCAD (1=Yes/0=No)? ");
		MCAD = int.Parse(Console.ReadLine());
		Console.Write("Do you hold a bachelor's degree (1=Yes/0=No)? ");
		Degree = int.Parse(Console.ReadLine());

		Console.WriteLine();

		if( MCAD == 1 )
			Console.WriteLine("Since you are an MCAD, you qualify");
		if( Degree == 1 )
			Console.WriteLine("Since you have the necessary degree, you qualify");
		

		Console.WriteLine();
	}
}

Here is an example of running the program:

Enter the following pieces of information
Applicant's Name: Annie Baugh
Are you an MCAD (1=Yes/0=No)? 8
Do you hold a bachelor's degree (1=Yes/0=No)? 1

Since you have the necessary degree, you qualify

In the same way, if an applicant is an MCAD and holds a bachelor's degree, he or she still qualifies. This produce the following table:

A B Statement
True True True

Based on this, here is an example of running the program:

Enter the following pieces of information
Applicant's Name: Jamie Konlo
Are you an MCAD (1=Yes/0=No)? 1
Do you hold a bachelor's degree (1=Yes/0=No)? 1

Since you are an MCAD, you qualify
Since you have the necessary degree, you qualify

Therefore, if an application either is an MCAD or has a degree or fulfills both criteria, he or she qualifies. If a candidate neither is an MCAD nor has the degree, he or she cannot have the job. This can be resumed as follows:

A B Statement
False False False

Writing both statements separately can appear redundant. Instead, if you are evaluating two statements but the truthfulness of only one is enough to render both as true, you can perform a Boolean disjunction operation. This is also called an OR operator. Using this operator, our two statements can be combined as "The applicant is an MCAD" OR "The applicant has a bachelor's degree". 

In C#, the logical disjunction is performed using the || operator. A combination of A || B can be represented as follows:

A B A || B
True False True
False True True
True True True
False False False

The expression that evaluates this combined statement can be written as follows:

using System;

class JobApplication
{
	static void Main()
	{
		string FullName;
		int MCAD;
		int Degree;

		Console.WriteLine("Enter the following pieces of information");
		Console.Write("Applicant's Name: ");
		FullName = Console.ReadLine();
		Console.Write("Are you an MCAD (1=Yes/0=No)? ");
		MCAD = int.Parse(Console.ReadLine());
		Console.Write("Do you hold a bachelor's degree (1=Yes/0=No)? ");
		Degree = int.Parse(Console.ReadLine());

		Console.WriteLine();

		if( MCAD == 1 || Degree == 1 )
			Console.WriteLine("Since you are an MCAD OR you have the necessary degree, you qualify");
		else
			Console.WriteLine("We will get back to you");

		Console.WriteLine();
	}
}

Once again, based on the rules of operators precedence, the "MCAD == 1 || Degree == 1" expression will work fine. If it appears difficult to read, you can include each part in its parentheses as (MCAD == 1) || (Degree == 1).

Here is an example of running the program:

Enter the following pieces of information
Applicant's Name: Sally Kirkfield
Are you an MCAD (1=Yes/0=No)? 1
Do you hold a bachelor's degree (1=Yes/0=No)? 8

Since you are an MCAD OR you have the necessary degree, you qualify

Here is another example of running the same program:

Enter the following pieces of information
Applicant's Name: Leslie Stanislaw
Are you an MCAD (1=Yes/0=No)? 0
Do you hold a bachelor's degree (1=Yes/0=No)? 0

We will get back to you

Practical Learning Practical Learning: Using the Logical OR Operator

  1. To write logical disjunctions, change the program as follows:
     
    using System;
    
    enum TypeOfApplication
    {
    	NewDriversLicense = 1,
    	UpgradeFromIDCard,
    	TransferFromAnotherState
    };
    
    class Exercise
    {
    	static void Main()
    	{
    		TypeOfApplication AppType;
    		int type;
    		string FirstName, LastName;
    		string OrganDonorAnswer;
    		char Sex;
    		string Gender = null;
    		char DLClass;
    	
    		Console.WriteLine(" -=- Motor Vehicle Administration -=-");
    		Console.WriteLine(" --- Driver's License Application ---");
    
    		do 
    		{
    			Console.WriteLine("  - Select the type of application -");
    			Console.WriteLine("1 - Applying for a brand new Driver's License");
    	Console.WriteLine("2 - Applicant already had an ID Card and is applying for a Driver's License");
    	Console.WriteLine("3 - Applicant is transferring his/her Driver's License from another state");
    			Console.Write("Your Choice: ");
    			type = int.Parse(Console.ReadLine());
    	
    			if( (type < 1) || (type > 3) )
    				Console.WriteLine("\nPlease enter a valid number as 1, 2, or 3");
    		} while( (type < 1) || (type > 3) );
    
    		Console.WriteLine();
    
    		Console.Write("First Name: ");
    		FirstName = Console.ReadLine();
    		Console.Write("Last Name:  ");
    		LastName = Console.ReadLine();
    
    		do 
    		{
    			Console.Write("Sex(F=Female/M=Male): ");
    			Sex = char.Parse(Console.ReadLine());
    			if( (Sex != 'f') && (Sex != 'F') && (Sex != 'm') && (Sex != 'M') )
    				Console.WriteLine("\nInvalid Answer - Try Again");
    		} while( (Sex != 'f') && (Sex != 'F') && (Sex != 'm') && (Sex != 'M') );
    		
    		if( (Sex == 'f') || (Sex == 'F') )
    			Gender = "Female";
    		else if( (Sex == 'm') || (Sex == 'M') )
    			Gender = "Male";
    
    		Console.WriteLine();
    
    		Console.WriteLine("  - Driver's License Class -");
    		Console.WriteLine("A - All Non-commercial vehicles except motorcycles");
    		Console.WriteLine("B - Non-commercial vehicles up to and including 26,001/more lbs.");
    		Console.WriteLine("C - Cars, pick-up trucks, non-commercial vehicles 26,000 lbs.");
    		Console.WriteLine("K - Mopeds");
    		Console.WriteLine("M - Motorcycles");
    		Console.Write("Your Choice: ");
    		DLClass = char.Parse(Console.ReadLine());
    
    		Console.WriteLine();
    
    		do 
    		{
    			Console.Write("Are you willing to be an Organ Donor(y=Yes/n=No)? ");
    			OrganDonorAnswer = Console.ReadLine();
    			if( (OrganDonorAnswer != "y") && (OrganDonorAnswer != "Y") &&
    				(OrganDonorAnswer != "n") && (OrganDonorAnswer != "N") )
    				Console.WriteLine("\nInvalid Answer");
    		} while( (OrganDonorAnswer != "y") && (OrganDonorAnswer != "Y") &&
    				 (OrganDonorAnswer != "n") && (OrganDonorAnswer != "N") );
    
    		Console.WriteLine("\n=======================================");
    		Console.WriteLine(" -=- Motor Vehicle Administration -=-");
    		Console.WriteLine(" --- Driver's License Information ---");
    
    		AppType = (TypeOfApplication)type;
    		Console.Write("Type of Application: ");
    		switch(AppType)
    		{
    			case TypeOfApplication.NewDriversLicense:
    				Console.WriteLine("New Driver's License");
    				break;
    			case TypeOfApplication.UpgradeFromIDCard:
    				Console.WriteLine("Upgrade From Identity Card");
    				break;
    			case TypeOfApplication.TransferFromAnotherState:
    				Console.WriteLine("Transfer From Another State");
    				break;
    			default:
    				Console.WriteLine("Not Specified");
    				break;
    		}
    
    		Console.WriteLine("Full Name:   {0} {1}", FirstName, LastName);
    		Console.WriteLine("Sex:         {0}", Gender);
    
    		switch(DLClass)
    		{
    			case 'a':
    			case 'A':
    				Console.WriteLine("Class:       A");
    				break;
    			case 'b':
    			case 'B':
    				Console.WriteLine("Class:       B");
    				break;
    			case 'c':
    			case 'C':
    				Console.WriteLine("Class:       C");
    				break;
    			case 'k':
    			case 'K':
    				Console.WriteLine("Class:       K");
    				break;
    			case 'm':
    			case 'M':
    				Console.WriteLine("Class:       M");
    				break;
    			default:
    				Console.WriteLine("Class:       Unknown");
    				break;
    		}
    
    		Console.Write("Organ Donor? ");
    		if( (OrganDonorAnswer == "y") || (OrganDonorAnswer == "Y") )
    			Console.WriteLine("Yes");
    		else
    			Console.WriteLine("No");
    		Console.WriteLine("=======================================");
    
    		Console.WriteLine();
    		return 0;
    	}
    }		
  2. Save the file. Compile and test it. Here is an example:
     
    -=- Motor Vehicle Administration -=-
     --- Driver's License Application ---
      - Select the type of application -
    1 - Applying for a brand new Driver's License
    2 - Applicant already had an ID Card and is applying for a Driver's License
    3 - Applicant is transferring his/her Driver's License from another state
    Your Choice: 0
    
    Please enter a valid number as 1, 2, or 3
      - Select the type of application -
    1 - Applying for a brand new Driver's License
    2 - Applicant already had an ID Card and is applying for a Driver's License
    3 - Applicant is transferring his/her Driver's License from another state
    Your Choice: 4
    
    Please enter a valid number as 1, 2, or 3
      - Select the type of application -
    1 - Applying for a brand new Driver's License
    2 - Applicant already had an ID Card and is applying for a Driver's License
    3 - Applicant is transferring his/her Driver's License from another state
    Your Choice: 1
    
    First Name: Jocelyne
    Last Name:  Aladiere
    Sex(F=Female/M=Male): D
    
    Invalid Answer - Try Again
    Sex(F=Female/M=Male): g
    
    Invalid Answer - Try Again
    Sex(F=Female/M=Male): f
      - Driver's License Class -
    A - All Non-commercial vehicles except motorcycles
    B - Non-commercial vehicles up to and including 26,001/more lbs.
    C - Cars, pick-up trucks, non-commercial vehicles 26,000 lbs.
    K - Mopeds
    M - Motorcycles
    Your Choice: m
    
    Are you willing to be an Organ Donor(y=Yes/n=No)? d
    
    Invalid Answer
    Are you willing to be an Organ Donor(y=Yes/n=No)? p
    
    Invalid Answer
    Are you willing to be an Organ Donor(y=Yes/n=No)? Y
    
    =======================================
     -=- Motor Vehicle Administration -=-
     --- Driver's License Information ---
    Type of Application: New Driver's License
    Full Name:   Jocelyne Aladiere
    Sex:         Female
    Class:       M
    Organ Donor? Yes
    =======================================
  3. Close
 

Previous Copyright © 2004-2012, FunctionX Next