Sunday, June 26, 2011

(IF, ELSE IF and ELSE) with (? :) syntax

Al salam alikom ("hi all")
          all of us know how to write the IF : ELSE with this syntax 

(Condition) ? IF action goes here : Else action goes here;

today I'll explain how to write  IF, IF Else, and Else with this syntax with very simple sample :)



//Java Script
var x = 0;
(x < 0) ? alert("Error: Number can't be minus...!") : (x >= 1) ? alert(x) : alert("Zero");



// C#
int x = 0;
string result = (x < 0) ? "Error: Number can't be minus...!" : (x >= 1) ? x.ToString() : "Zero";

Easy and simple, wish you like it.

No comments: