the if statement help{noob}

first of all i just want to say that i am a noob when it comes to c# programming., also i think this is an if statement situation, i could be wrong

I have google my query and cannot fing an answer :/

so heres my question: Lets say i have the equation x + y = z (both x and y are numbers that are entered in at the console) now if i enter 5 for x and y in the console i will get

5 + 5 =10 However if i want it to say something else if 'z' goes above 10,

how do i go about it?

eg. 6 + 6 = 12 Here 12 is greater 10, an so i want it to say 6 + 6 = "TRust to large"

It might be hard to understand what im looking for here, ino its very badly written!

Jon Skeet
people
quotationmark

It sounds like you're after something as simple as:

Console.WriteLine("{0} + {1} = {2}", x, y, x + y);
if (x + y > 10)
{
    Console.WriteLine("TRust too large");
}

An if statement is basically just a condition, with some code to execute if that condition is met - and potentially another piece of code to execute if the condition isn't met (via else).

Note that the above code will always print the first line. As an example of an if / else you could make it only print the result if it's in range:

if (x + y > 10)
{
    Console.WriteLine("TRust too large");
}
else
{ 
    Console.WriteLine("{0} + {1} = {2}", x, y, x + y);
}

Or if you find it more readable to put the "happy path" first:

if (x + y <= 10)
{
    Console.WriteLine("{0} + {1} = {2}", x, y, x + y);
}
else
{ 
    Console.WriteLine("TRust too large");
}

Or having extracted the common code, to avoid problems if you ever change the formula:

int sum = x + y;
if (sum <= 10)
{
    Console.WriteLine("{0} + {1} = {2}", x, y, sum);
}
else
{ 
    Console.WriteLine("TRust too large");
}

people

See more on this question at Stackoverflow