Jump to content

operador ternario


escrapy01

Recommended Posts

Este es un tip que muchos lenguajes de programacion lo tienen como php y c++ pero casi nadie sabe que existe, espero traer mas aportes como este otroo dia

 

El operador ternario es una manera diferente de usar el if else. Se expresa de la siguiente manera

 

(Condicion)?"mostrar si verdadero":"mostrar si falso";

 

Donde en condicion ingresas la condicion booleana

 

En mostrar si verdadero puedes ingresar tanto variables,textos o combinar variables con texto

 

De la misma manera en mostrar si falso

 

Ejemplos serian

 

$Login= (users::user_exist($user)==true && users::user_pass($user, $pass))?"logeo con existo":"datos erroneos";

 

O

 

Return ($edad>=18)?true:false;

 

Que es mas practico que usar

If($edad>=18){

Return true;

}

Else{

Return false;

}

 

Es algo simple, pero de algo les a de servir

Link to comment
Share on other sites

dejo ejemplos en lua y python:

 

bVar = True

print "true condition sentence" if bVar else "false condition sentence"

> "true condition sentence"

 

bVar = false

print(bVar and "true condition sentence" or "false condition sentence")

> "false condition sentence"

 

nota: en python también se puede utilizar el and/or.

Link to comment
Share on other sites

dejo ejemplos en lua y python:

 

bVar = True

print "true condition sentence" if bVar else "false condition sentence"

> "true condition sentence"

 

bVar = false

print(bVar and "true condition sentence" or "false condition sentence")

> "false condition sentence"

 

nota: en python también se puede utilizar el and/or.

 

nota2: en python se puede usar de esa forma en versiones >= 2.5.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...