- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Inline conditions in Lua (a == b ? “yes” : “no”)
You might have noticed ternary operators in different programming languages, but since there’s no ternary operator in Lua, as per the official documentation, we can create one for ourselves with the help of the Lua operators.
Let’s first understand what a ternary operator is and why we need one.
Example
Consider the example shown below, which depicts a simple if else condition in lua.
a = 3 b = 4 if a == b then print("blah") else print("nah nah") end
Output
nah nah
In the above if else condition, we wrote multiple lines of code and also used many statements that the lua language provides, but what if we can write the exact same logic without writing multiple lines of code, and using less statements.
Example
It is possible to write the same exact logic of code in a single line. Consider the example shown below −
print("Yo: " .. (a == b and "blah" or "nah nah"))
Output
Yo: blah
- Related Articles
- How to create a dialog with “yes” and “no” options in JavaScript?
- Why does Lua have no “continue” statement?
- The mean is one of the numbers in the given data :(a) Yes (b) No (c) Not always
- How to Allow Only Yes or No Entry in Excel?
- MySQL IF() to display custom YES or NO messages
- How to Calculate the Percentage of Yes and No from a List in Excel?
- Do you think IPL should be banned? Yes/No….Why?
- How can I create a dialog box in Java with Yes No and cancel buttons?
- How to make JOptionPane to handle Yes, No and Closed buttons in Java?
- How to create JavaScript alert with 3 buttons (Yes, No and Cancel)?
- Select count of values (Yes, No) with same ids but different corresponding records in MySQL?
- Inline-level Elements and Inline Boxes in CSS
- How to Use lua-mongo library in Lua Programming?
- Install Lua in Linux and implement PHP Lua Extension
- Inline Functions in C++

Advertisements