Skip to content
pranithcodes edited this page Apr 6, 2018 · 1 revision

For Loop allows us through items of an Array, Collection, Map, Iterator or Enumeration, e.g.

public static void main(String[] args) throws ScriptException {

	ArrayList<String> data = new ArrayList<String>();
	data.add("FOR");
	data.add("LOOP");
	data.add("TO");
	data.add("ITERATE");

	String script = "for(item : list ){ out.println(item)}";

	JexlScriptEngine jexlScriptEngine = new JexlScriptEngine();

	Bindings bindings = new SimpleBindings();
	bindings.put("list", data);
	bindings.put("out", System.out);

	jexlScriptEngine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);

	jexlScriptEngine.eval(script);

}

While Loop:

public static void main(String[] args) throws ScriptException {

	String script = "while(i < 10 ){ i = i+1; out.println(i)}";

	JexlScriptEngine jexlScriptEngine = new JexlScriptEngine();

	Bindings bindings = new SimpleBindings();
	bindings.put("i", 0);
	bindings.put("out", System.out);

	jexlScriptEngine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);

	jexlScriptEngine.eval(script);

}
Clone this wiki locally