Simple expression forming – Computer Sir Ki Class Code Learning #JAVA#3744    ## 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

 class SimpleExp { public static void main(String[] args) { float a=1.3f,b=2.5f,c; c=a+(a+b)/a*b; System.out.println("Output c = "+c); } } .linedwrap { border: 1px solid #c0c0c0; padding-top: 5px; padding-bottom: 5px; border-radius: 4px; background-color:#F8F8F8; } .linedtextarea { padding: 0px; margin: 0px; background-color:#F8F8F8; } .linedtextarea textarea, .linedwrap .codelines .lineno { font-size: 12pt; font-family: monospace; line-height: normal !important; } .linedtextarea textarea { padding-right:0.3em; padding-top:0.3em; border: 0; } .linedwrap .lines { margin-top: 0px; width: 20px; float: left; overflow: hidden; border-right: 1px solid #c0c0c0; margin-right: 10px; } .linedwrap .codelines { padding-top: 5px; } .linedwrap .codelines .lineno { color:#AAAAAA; padding-right: 2px; padding-top: 0.0em; text-align: right; white-space: nowrap; } .linedwrap .codelines .lineselect { color: red; }

## Run Output

``Output c = 8.607693``

## 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

sunmitra| Created: 5-Mar-2018 | Updated: 5-Mar-2018|

×