TransWikia.com

Skipping the first step in a loop for the first iteration

Stack Overflow Asked by Qua on December 19, 2020

I am learning C# using the C# programming yellow book by Rob Miles. In this he is discussing an example of a calculator for determining the total glass area needed in square meters. I recreated this code and wanted to try to improve on it with things that made sense for me. For example in his code he used a lot of blank lines which didn’t look appealing to me. I wanted to try making it look a bit more clean. I mainly want to eliminate this first blank line, but make sure the blank line would appear on the next one.

This is the original code.

double width, height, woodLength, glassArea;

        const double MAX_WIDTH = 5.0;
        const double MIN_WIDTH = 0.5;
        const double MAX_HEIGHT = 3.0;
        const double MIN_HEIGHT = 0.75;

        string widthString, heightString;

        do {
            Console.Write("nGive the width of the window between " + MIN_WIDTH + " and " + MAX_WIDTH + ": ");
            widthString = Console.ReadLine();
            width = double.Parse(widthString);

            if (width < MIN_WIDTH){
                Console.WriteLine("Width is too small.");
            }

            if (width > MAX_WIDTH){
                Console.WriteLine("Width is too large.");
            }
        } while (width < MIN_WIDTH || width > MAX_WIDTH);

I tried to use a goto, but because the loop is a different method(?) it didn’t work. Through some googling I found out that goto is very bad practice, but I can’t think of any other methods to use.
(nor do i know any)

            Console.Write("Give the width of the window between " + MIN_WIDTH + " and " + MAX_WIDTH + ": ");

        goto first_loop;

        do {
            Console.Write("nGive the width of the window between " + MIN_WIDTH + " and " + MAX_WIDTH + ": ");
            first_loop:
            widthString = Console.ReadLine();
            width = double.Parse(widthString);

            if (width < MIN_WIDTH){
                Console.WriteLine("Width is too small.");
            }

            if (width > MAX_WIDTH){
                Console.WriteLine("Width is too large.");
            }
        } while (width < MIN_WIDTH || width > MAX_WIDTH);

2 Answers

I mainly want to eliminate this first blank line, but make sure the blank line would appear on the next one.

Remove the n from the prompt, and instead add it to the end of both of the error messages:

do
{
    Console.Write("Give the width of the window between " + MIN_WIDTH + " and " + MAX_WIDTH + ": ");
    widthString = Console.ReadLine();
    width = double.Parse(widthString);

    if (width < MIN_WIDTH)
    {
        Console.WriteLine("Width is too small.n");
    }
    else if (width > MAX_WIDTH)
    {
        Console.WriteLine("Width is too large.n");
    }
} while (width < MIN_WIDTH || width > MAX_WIDTH);

If I were writing this myself, though, I'd use a boolean to indicate the overall success and check that in the while loop conditional. I'd also use double.TryParse() as shown below:

bool valid = false;
do
{
    valid = true; // assume good until proven otherwise
    Console.Write("Give the width of the window between " + MIN_WIDTH + " and " + MAX_WIDTH + ": ");
    widthString = Console.ReadLine();
    if (double.TryParse(widthString, out width))
    {
        if (width < MIN_WIDTH || width >MAX_WIDTH )
        {
            valid = false;
            Console.WriteLine("Width is too " + ((width<MIN_WIDTH) ? "small" : "large") + ".");
        }
    }
    else
    {
        Console.WriteLine("Invalid width entry. Please try again.");
        valid = false;
    }
    
    if (!valid)
    {
        Console.WriteLine();
    }
} while (!valid);

Correct answer by Idle_Mind on December 19, 2020

If you also do not want additional blank lines at the end as in the solution by @Idle_Mind, you could do something like this

bool isFirstRun = true;
do
{
    if (!isFirstRun) Console.WriteLine();
    isFirstRun = false;

    Console.Write("Give the width of the window between " + MIN_WIDTH + " and " + MAX_WIDTH + ": ");
    widthString = Console.ReadLine();
    width = double.Parse(widthString);

    if (width < MIN_WIDTH)
    {
      Console.WriteLine("Width is too small.");
    }

    if (width > MAX_WIDTH)
    {
      Console.WriteLine("Width is too large.");
    }
} while (width < MIN_WIDTH || width > MAX_WIDTH);

Answered by Flat Eric on December 19, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP