# 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 + ": ");
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:
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 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 + ": ");
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 + ": ");
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 + ": ");
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

## 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

1  Asked on February 14, 2021

### Good way to update each item in an array inside a store

2  Asked on February 14, 2021 by hank

### Running a value through an array of functions

2  Asked on February 14, 2021 by lars-holdaas

### Possible dynamic allocation issues

1  Asked on February 14, 2021 by grango

### Pass primary key in success_url in Delete Class view in django

1  Asked on February 14, 2021 by ajay-kumar

### TyperError When trying to show an image in grayscale on Python

1  Asked on February 14, 2021 by andrew-vo

### ‘react-scripts’ is not recognized as an internal or external command, operable program or batch file

15  Asked on February 14, 2021 by david-essien

### How to revert back to python 2.7?

1  Asked on February 14, 2021 by nikhil-shrivastava

### Detecting the mouse releasing focus from Inspector in Unity

0  Asked on February 13, 2021 by elighne

### Adjusting a column value by subtracting previous running total from current row value in SQL Server

1  Asked on February 13, 2021 by usef-juan

### 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

### The contents of the input file are always undefined when I print formData, why? ReactJS

1  Asked on February 13, 2021 by bmalbusca

### Python: access a variable in a module from another file

2  Asked on February 13, 2021 by yeroduk

### How can I show a playback progress bar in WatchKit NowPlaying view

0  Asked on February 13, 2021 by alexw-h-b

### 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

### Handlebars if statement condition show content based on membershipID

2  Asked on February 13, 2021 by lv98