Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

asp
conditional
communitycreator

What are ASP Conditionals?

Jezreel Franklin

ASP is an acronym that stands for Active Server Pages. It is a web technology developed by Microsoft that aids in the development of web applications, APIs and Services.

In the course of writing computer programs to build applications, one may be tasked with the following:

  1. Describing things by leveraging data structures.
  2. Performing actions using functions / methods.
  3. Making decisions writing conditional statements
  4. Repeating by leveraging loops.

Any non-trivial application aims to attain maximum robustness. This can only be made possible if decision making at runtime is automated. Programming languages give developers the power to wire up this kind of behavior through conditionals.

This shot will explore the varying constructs the C# language provides to implement conditionals in ASP applications.

Have you ever wanted to buy stuff from the grocery store on a budget? If we assume you are not certain how much an item might cost, and you only have $20 to spend, the decision making process might play out in the following manner:

  • If an item costs $5, buy 4 pieces.
  • If the item cost is greater than $5 but less than $10, buy 2 pieces of the item.
  • Otherwise, go back home.

Just like you make decisions in life based on certain constraints and affordances, we can make our programs emulate this kind of behavior using conditionals.

If we replicate the grocery store scenario in code, we would have a program that looks like this:

using System;
    public static class Budgeter
    {
        private const decimal HowMuchIHave = 20.00M;

        private static void BuyItem(int pieces)
        {
            Console.WriteLine($"Bought {pieces} pieces");
        }

        private static void GoBackHome()
        {
            Console.WriteLine("Go Back Home");
        }

        public static void MakePurchase(decimal itemCost)
        {
            if (itemCost == 5.00M)
            {
                BuyItem(4);
            }
            else if (itemCost > 5.00M && itemCost <= 10.00M)
            {
                BuyItem(2);
            }
            else
            {
                GoBackHome();
            }
        }
    }

class HelloWorld
{
    static void Main()
    {
        Budgeter.MakePurchase(5);
    }
}

As trivial as the above example might seem, we have distilled our decision making process into a set of computer understandable statements through the use of if, else if, and else constructs provided by the C# language.

  • if statements: if a condition is true it is used to specify execution for a block of code.

  • else if statements: this specifies a new test if the preceding condition is false.

  • else statements: here, if the same condition is false, it specifies the execution for a block of code. In a sequence of if and Else if conditions, the code block specified within the else block is usually the last resort.

Switch statements

When you write programs, you may want to perform an operation based on the evaluation of certain expression(s) or parameters. if else conditionals might be the tool for this, but it will get convoluted quickly. However, switch statements let you represent your logic in a succinct way. Switch statements are, in essence, a pattern matching approach to conditionally executing arbitrary logic.

Let’s write a scheduler program that reminds us of tasks to perform on certain weekdays:

using System;

public enum WeekDays
{
  Sunday,
  Monday,
  Tuesday,
  Wednesday,
  Thursday,
  Friday,
  Saturday
}

public static class Scheduler 
{
	public static void WhatToDo(WeekDays weekday)
	{
		switch(weekday)
		{
			case WeekDays.Sunday:
				Console.WriteLine("Go to Church");
				break;

			case WeekDays.Monday:
				Console.WriteLine("Walk the dog");
				break;

			// Insert code for every other case here
			default: 
				Console.WriteLine("There's nothing to do");
				break;
		}
	} 
}

class HelloWorld
{
    static void Main()
    {
        Scheduler.WhatToDo(WeekDays.Monday);
    }
}

For the purpose of brevity, cases for other weekdays have been omitted.

Whenever the value within the parenthesis, that is preceded by the switch keyword, matches the value preceded by the case keyword, the code block right beneath it is executed. When C# reaches the break keyword, it breaks out of the switch block. This stops the execution of more code and case testing inside the block. There is no a need for more cases to be tested once a matching case is found. The break statement ensures that behavior.

If there is no match found, the one beneath the default keyword is executed instead.

RELATED TAGS

asp
conditional
communitycreator
RELATED COURSES

View all Courses

Keep Exploring