Simple expression forming
Forming expressions with simple variables.
Learning Objectives
- Making expressions with simple variables.
- Concept of formula making with formula only on the right hand side of equation.
Source Code
|
|
Run Output
Code Understanding
float a=1.3f,b=2.5f,c;
Here we are declaring three float variables where a and b are initialised with fraction values. Since these values are float value so f suffix is desired.. c is not initialised as it will be filled very soon based on values deduced from expression based on a and b.
c=a+(a+b)/a*b;
Here we build a formulas based on left hand side containing just some variable name and right hand side with an expression like (a+b)/a*b . This expression shall be evaluated based on the precedence of operators. For example here brackets will be evaluated first then division will occur and then multiplication will occur. This is similar to BODMAS rule we learnt in middle school classes.
Remember for formula building only right hand side is used to write expressions, which is equated to some variable on the left hand side.
System.out.println(“Output c = “+c);
Here we print the output of expression which was earlier filled in the variable c.
Notes
- An expression is defined as a set of entities which can be evaluated to give a single value.
- You can have expressions within expressions. For e.g. in (a+b)/a*b, you have further smaller expressions which are (a+b), (a+b)/a etc.
Suggested Filename(s): SimpleExp.java
Created: 5-Mar-2018 12:21 | Updated: 5-Mar-2018 12:21|
