A formula is one that operands are connected to each with the help of operators to calculate the value. An operand could be an expression reference, a variable or array element, or an integer.
Let’s take a look at the following example
a-b;
In the above example, the character minus (“-” in the above expression)) represents an operator and a and is the pair of operands.
The following four kinds of expressions within C:
- Arithmetic expressions
- Expressions that are related
- Logical expressions
- Expressions with conditions
Every type of expression uses specific types of operands and utilizes a certain group of operators. A particular expression’s evaluation results in a particular number.
For instance:
x = 9/2 + a-b;
The whole above line is not an expression. The part that follows the equals represents an expression.
Arithmetic Expressions
Arithmetic expressions are expressions composed of arithmetic operators and operands. Arithmetic expressions compute an int or double, floating or int.
If an expression only contains integral operands, it is referred to as a pure integer expression. If it is only composed of real operands. In this case, it is referred to for its pure expression and if it includes both real and integral operands, it’s known for mixed-mode.
Assessment of Arithmetic Expressions
The formulas are evaluated by performing only one task at a. The order of precedence and the associativity of operators determine the order of the evaluation of each operation.
If individual actions are being performed in HTML0, the following scenarios can occur:
- If both operands are integers Arithmetic is done, and the output of the operation is an integer number. For example, 3/2 would result in 1. 1.5 because the fractional portion is not taken into consideration.
- If both operands are of the type floating, then arithmetic would be done and the outcome of the operation will be an actual number. For instance, 2.0/2.0 will yield 1.0 and not 1.
- If one of the operands is an integer while another is of the type real then the mixed arithmetic is executed. In this instance the first operand is converted to an actual operand and then arithmetic done to create the real value. For instance, 6/2.0 will yield 3.0 because the initial number of six is transformed to 6.0 and the arithmetic process is then performed to create 3.0.
Let’s look at an example to understand.6*2/ (2+1 * 2/3 + 6) + 8 * (8/4)
Evaluation of expression | The description of every operation |
---|---|
6*2/( 2+1 * 2/3 +6) +8 * (8/4) | An expression is used. |
6*2/(2+2/3 + 6) + 8 * (8/4) | 2 is multiplied by 1 which gives a value of 2. |
6*2/(2+0+6) + 8 * (8/4) | 2 is then divided by 3, giving the value 0. |
6*2/ 8+ 8 * (8/4) | 2, when added, is given the value of 8. |
6*2/8 + 8 * 2 | 8, when divided, is 4, giving the value of 2. |
12/8 +8 * 2 | 6 . This is then multiplied by 2 which gives a value of 12. |
1 + 8 * 2 | The 12th number is multiplied by eight giving the value 1. |
1 + 16 | The 8th number is then multiplied by 2 which gives a value of 16. |
17 | 1 . This value is then added 16, giving value 17. |
Expressions of Relationship
- The term “relational” refers to a term used to evaluate two operands.
- A condition that is used to determine if the decision should be made or not.
- When using relational expressions a numerical value is not able to be compared with strings.
- The result of a relational expression could be either a zero or non-zero value. In this case, zero corresponds to false, while the zero value is equivalent to a true.
Expression of Relationship | Description |
---|---|
x%2 = = 0 | This test is used to determine whether it is an odd number, or not. The expression results in a value of 1 if there is an even value for x, and if not, it returns a value of 0. |
a!=b | It’s used to test whether a is equal to the value b. The relational expression will result in 1 if a not greater than b or it is 0. |
a+b = = x+y | It’s used to verify that the equation “a+b” is the same as that expression “x+y”. |
a>=9 | This is to determine that the number of points value is higher than or less than 9. |
Let’s look at a basic example:
#include <stdio.h>
int main()
{
int x=4;
if(x%2==0)
{
printf("The number x is even");
}
else
printf("The number x is not even");
return 0;
}
Output
Logical Expressions
- A logic expression is one which computes an un-zero or zero value.
- It’s a complicated test to make the right decision.
Let’s look at some examples of logic behind the expressions.
Logical Expressions | Description |
---|---|
( greater than 4 ) and ( x ) | It’s a test to test whether the number x is greater than 4 and x is smaller than 6. The outcome of the test is valid only if both of the conditions are met. |
x > 10 || y <11 | This is an example of a test used to verify that the value of x is greater than 10, or y is lower than 11. The test is valid when either one of the conditions is true. |
! ( x > 10 ) && ( y = = 2 ) | This is an example that is used to determine if it isn’t greater than 10, and that y equals 2. The outcome of the test is true when both conditions are true. |
Let’s look at a basic program for an “&&” operator.
#include <stdio.h>
int main()
{
int x = 4;
int y = 10;
if ( (x <10) && (y>5))
{
printf("Condition is true");
}
else
printf("Condition is false");
return 0;
}
Output
Let’s see a simple example of the “| |” operator
#include <stdio.h>
int main()
{
int x = 4;
int y = 9;
if ( (x <6) || (y>10))
{
printf("Condition is true");
}
else
printf("Condition is false");
return 0;
}
Conditional Expressions
- An expression that is conditional can be one that will return 1 if the condition is true, otherwise it returns 0.
- A conditional operator may also be called a ternary operator.
A Syntax for Conditional operator
Imagine exp1 is exp1, exp2, and exp3. three different expressions.
exp1? exp2: exp3
The expression above is a conditional one that is determined by the value of exp1’s expression. In the event that the condition in exp1 is true, the expression that is finalized as a conditional expression will be expressed by exp2 or by exp3.
Let’s look at it with an illustration.
#include<stdio.h>
#include<string.h>
int main()
{
int age = 25;
char status;
status = (age>22) ? 'M': 'U';
if(status == 'M')
printf("Married");
else
printf("Unmarried");
return 0;
}
Output