Difference between statement and expression
In programming language terminology, an “expression” is a combination of values and functions that are combined and interpreted by the compiler to create a new value, as opposed to a “statement” which is just a standalone unit of execution and doesn’t return anything.
C# Expressions
An expression in C# is a combination of operands (variables, literals, method calls) and operators that can be evaluated to a single value. To be precise, an expression must have at least one operand but may not have any operator.
Let's look at the example below:
float price;
price = 42.05;
Here,
42.05
is an expression. Also, price = 42.05
is an expression too.int x, y, z, sum;
sum = x + y + z;
Here, x
+ y + z
is an expression.if (age>=18 && age<58)
Console.WriteLine("Eligible to work");
Here,
(age>=18 && age<58)
is an expression that returns a boolean
value. "Eligible to work"
is also an expression.C# Statements
A statement is a basic unit of execution of a program. A program consists of multiple statements.
For example:
int age = 21;
Int marks = 90;
In the above example, both lines above are statements.
keep up
ReplyDelete