Question I don't understand if statement

RavCoder

Member
Joined
Aug 28, 2019
Messages
5
Programming Experience
Beginner
Hi everyone,
I'm beginner developer in C# ,this is my first post in this forum and I ask to explain this code because I don't understand if statement:
C#:
using System;

public class Program
{
    public static void Main()
    {
        if (144 != 12 * 12) {
            Console.WriteLine("Math isn't in this world, kid!");
        }
        else {
            Console.WriteLine("Surprise, man!");
        }
    }
}
I found this in a course online about C# language ( Codeasy.net).
Thanks and regards,
P.s Sorry for my English, but it's not my first language!!!
 
Last edited by a moderator:

Sheepings

Senior Programmer
Joined
Sep 5, 2018
Messages
345
Location
UK
Programming Experience
10+
If a condition is not met in if ( [CONDITION] )
else will execute the else { } block of that statement.
You would not do maths inside of a condition. The condition is where you would compare the value or value(s) of one or more item(s).
When you do 12 * 12 inside an if statement, as you've done; you are only performing an operation between the two numbers where a conditional value is expected. This won't work as you are not producing an output for the condition to evaluate. Assign your variable to your maths like so int value = 12 * 12; if (value != 144) and declare your variable and perform your maths operation before your conditional statement runs. See the link in my signature for Conditional Logic IfElse. Expand the spoiler and you will see the links.
 

RavCoder

Member
Joined
Aug 28, 2019
Messages
5
Programming Experience
Beginner
Thanks!!!
I think It was an exercises to understand if condition and numbers type.
However I will read the link that you send me.
 

Sheepings

Senior Programmer
Joined
Sep 5, 2018
Messages
345
Location
UK
Programming Experience
10+
Please only write in English on this forum. There are an abundance of translators out there if you need them.

And you're welcome.
 

RavCoder

Member
Joined
Aug 28, 2019
Messages
5
Programming Experience
Beginner
Please only write in English on this forum. There are an abundance of translators out there if you need them.

And you're welcome.
I really sorry, in fact, right now, I correct the answer in English 🙂.
 

Sheepings

Senior Programmer
Joined
Sep 5, 2018
Messages
345
Location
UK
Programming Experience
10+
No problem. If you have any more questions, come back and let us know. There is always someone online to help.
 
Top Bottom