Skoochapp
Skoochapp

Reputation: 31

parsing 2 double values in 1 statement - C#

how can I parse 2 double values in 1 statement instead of 2 if statements ?

my code :

double a, b;
while (true)
{
    if (Double.TryParse(Console.ReadLine(), out a))
    {
    }
    else
    {


        continue;
    }

    if (Double.TryParse(Console.ReadLine(), out b))
    {

    }
    else
    {

        continue;
    }
    break;
}

I have already searched for it but did not found any good result

Upvotes: 0

Views: 91

Answers (2)

InBetween
InBetween

Reputation: 32750

The if is redundant here, you don't need it and it makes the code less readable with an unnecessary continue.

double a, b;
while (!(double.TryParse(Console.ReadLine(), out a) &&
         double.TryParse(Console.ReadLine(), out b))
{
}

//a and b successfully parsed.

Upvotes: 0

Zein Makki
Zein Makki

Reputation: 30022

Something like this:

if (Double.TryParse(Console.ReadLine(), out a) 
    && Double.TryParse(Console.ReadLine(), out b))
{

}
else
{

    continue;
}

Note that the if block is only entered if both values are successfully parsed.

Upvotes: 6

Related Questions