AnswerBun.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!

Related Questions

“Live” data capable alternative for Google Earth KML

1  Asked on February 15, 2021 by christoph1197

   

React component dispatching generic payload

1  Asked on February 14, 2021 by andyroo

   

CSS animted gradient

1  Asked on February 14, 2021

     

Running a value through an array of functions

2  Asked on February 14, 2021 by lars-holdaas

   

How to revert back to python 2.7?

1  Asked on February 14, 2021 by nikhil-shrivastava

         

Using same name for attribute and getter

1  Asked on February 13, 2021 by janpeterka

     

What is the purpose to use className beside functionName using colon(:)

1  Asked on February 13, 2021 by mahedi-hasan-durjoy

 

Python: access a variable in a module from another file

2  Asked on February 13, 2021 by yeroduk

 

How to get elements of a sublist in Java

5  Asked on February 13, 2021 by alvira

     

`SyntaxError` in if-else one-liner

2  Asked on February 13, 2021 by evgeniy-golovin

         

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir