20 Nisan 2017 Perşembe

Ternary Operator in different programming Languages (TEK SATIRDA if else):

c , c++ :
---------------------------
? operatörü kullanılır.  şart ? deyim1 : deyim2;

value = <condition> ? <expression1> : <expression2>

int sayi;
const char *sonuc;
sayi = 14;
if (sayi < 10)
sonuc = " 0";
else
sonuc = "x";

  printf(sonuc);


yukardaki kodun eşdeğeri:

int sayi;
const char *sonuc;
sayi = 4;

sonuc = sayi < 10 ? " 0" : " ";

printf(sonuc);


python:
----------------------------------


<expression1> if <condition> else <expression2>
veya


value = <condition> and <expression1> or <expression2>


t = 0
if t == 0:
  m = 100
else:
  m = 5

yukardaki kodun eşdeğeri:


t = 0
m = 100 if t == 0 else 5

yukardaki kodun eşdeğeri:

 t = 0
m = t==0 and 100 or 5


kaynaklar:

http://stackoverflow.com/questions/394809/does-python-have-a-ternary-conditional-operator
http://stackoverflow.com/questions/8500374/python-statement-of-short-if-else
http://stackoverflow.com/questions/1686390/python-equivalent-of-short-form-if-in-c
http://www1.gantep.edu.tr/~bingul/c/index.php?ders=6


Hiç yorum yok:

Yorum Gönder