What is Expression in programming? (feat. Statement)
What is Expression in programming? It’s like expressing something in the name of an expression, but what is it expressing? When you’re this confused, it’s helpful to compare it to other concepts. You can better understand the object through commonalities and differences by comparing the two.
Expression and Statement
In programming, we often need clarification on expressions and statements. It’s because they have similarities. But there’s definitely a difference. The reason why it’s confusing is that a statement is a concept that includes an expression. So we’re confused in the sense that the two images are mixed.
What is the Statement (to understand the Expression)
First, the statement represents the process. The statement expresses all processes, such as calculating values, changing the flow of actions, or printing them on the screen. Most statements, though not all languages, are separated by ;
. Especially in C
, C++
, it becomes a statement if the formula is followed by ;
. Conversely, however, subtracting ;
from all statements does not make a formula. The reverse doesn’t work. Why is that?
Then what is Expression?
That’s because, as I said before, the statement is a larger concept that includes the concept of a formula. You said earlier that statement represents the process. Now let’s look at today’s star, Expression. The expression represents a value. 2
, -5.3
“abc”
and i+3
are all Expressions. The expression consists of operators and operands. In some cases, you may not have one of them.
- Operatorless Expressions :
‘Hello world’
,-5.3
- Operand-free Expression :
function()
The expression consists of operators and operands and represents values. Easy? Expression is included in the statement because representing the value itself is included in the processing process. In other words, all Expression is a statements, but not all statements can be an expression.
Let’s look at the if statement, for example
If (true) {
return 100;
}
These if statements represent the operation process as well as the value. Therefore, it cannot be called just Expression.
Which element of Expression do we meet most often? It’s an operator. Expression includes a variety of operators that we know well.
- Arithmetic operator :
+
,-
,*
, etc - Relational operator :
>
,!=
,==
- Logical operators :
&&
,||
,!
In addition to these operators, depending on the language, operators such as ++
, ^
,|
are added.
In this way, the formula expresses the value as an operator and an operand. Thank you for reading it. 🙇🙇🙇