Question a problem with my if-condition statement

shiccorama

Member
Joined
Apr 24, 2021
Messages
6
Programming Experience
1-3
Dear all, how are you ?
this is sherif and I'm a newbie in learning c#, would you please help me ?
my function is this :
C#:
double FirstNum;
double SecondNum;
double ThatsMyNum;

public void LargestOfThree(double x, double y, double z)
{
    if (x > y)
    {
        x = FirstNum;
    }
    else if (z > y)
    {
        z = SecondNum;
    }
    else if (FirstNum > SecondNum)
    {
        ThatsMyNum = FirstNum;
    }
    else
    {
        ThatsMyNum = SecondNum;
    }

    Console.WriteLine("The Largest Number is : " + ThatsMyNum);
}
and the output is // The Largest Number is : 0


why "zero", what's wrong ????
thanks in advance ....
 
Last edited by a moderator:
You need to debug your code and you can work it out for yourself. Place a breakpoint at the top of the code and then, when execution breaks at that line, you can step through the code line by line. At each step, you can use the debugging tools that VS provides to examine the state of the application, e.g. the values of your variables and the path execution takes, and identify where the actual behaviour differs from your expectations. If you do that, you should be able to see that your logic doesn't come close to what is required to compare three numbers.

The code you have is an example of what happens when beginners try to write code without knowing what that code has to. You may have known what the end result was supposed to be but you clearly had no real idea of what the steps were supposed to be. This is why you don't start by writing code. You should work out the logic first and only write code once you know exactly what it has to do.

Forget that this is a programming problem and pick up a pen and paper and work out EXACTLY what steps you would perform if you had to perform this task manually. Write down what those steps are and follow them to the letter with a number of different data inputs. Only once you can do that and get the expected results should you consider writing code. When you write the code, you don't even have to consider the original task. The code should directly implement the steps in your algorithm. If you test the code and it doesn't work then you can compare it to your algorithm directly to see where it doesn't implement it properly.
 
As a clue, it should be obvious that, in order to determine the largest of three numbers, you need to perform at least two comparisons. With your code, if you start with x set to 10 and y set to 1, how many comparisons will be performed?
 
Slightly off topic:

This going to sound like an "In the old days of computing" story, because it is. :) When I was I. 5th Grade in the early 80s and learning to program, they wouldn't let use write any actual code until after we had drawn a flowchart of the program's logic. And then only after the flowchart, could we write BASIC code... on paper. We couldn't input the program into an actual computer until we could show how our code mapped back to the flowchart.

And all at that time, we were hearing about kids in the US getting to write Logo code straight into the computer. No requirements to do flowcharts first. No need to pre-write the program and prove it would work. *sigh*
 
Slightly off topic:
Around the same time and age I was writing BASIC code into a Commodore 64 (not bits, it was a 8bit gaming console actually) and saving it to a good old cassette tape :cool:
 
By the way, there is another way to get the largest number: LINQ Max
 
Back
Top Bottom