Solutions to Lua syntax defects and alternative measures

Source: Internet
Author: User
Tags integer division

SolutionLua syntaxMissing and alternative measures are the content to be introduced in this article, mainly to understandLUA syntax.LuaA design policy is simple. If you do not want to add any language features, try not to add them. So,LuaSome of the commonSyntax. The following is a reference in C language.

The continue statement is missing, although it has no essential impact on logical implementation. However, when programming, we feel that the code structure is not flexible.

The switch-case syntax is missing and can only be replaced by if-elseif-else. When there are many options, the code is ugly and inefficient.

There is no logical operator AND, OR, NOT, which causes a lot of inconvenience in the expression of some flag combination expressions.

In addition, I also want to talk about the following points about Lua's design:

The non-equal sign is ~ =, Not! =. Personally think! = It is more accurate to express the non-equals meaning ,~ It means "not", and "not" is different from "not equal. Of course, this may be the reason why the author is different from ours.

By default, the Division is a floating-point Division without any integer division operation. To obtain an integer, you must use the truncation function of the math library.

The Continue statement is implemented by patching.

Logical operation ,! = Operator, logical operator, bitwise operator, and shift operator are also implemented through a patch.

However, we did not find the Switch patch on Lua's official website. We found that we can use the table features of Lua, Which is elegant.

The alternative syntax of the Switch statement is one of the best, most concise, and most efficient solutions that best reflect the characteristics of Lua)

 
 
  1. action = {  
  2.   [1] = function (x) print(x) end,  
  3.   [2] = function (x) print( 2 * x ) end,  
  4.   ["nop"] = function (x) print(math.random()) end,  
  5.   ["my name"] = function (x) print("fred") end,  
  6. }  
  7.  
  8. while true do  
  9.     key = getChar()  
  10.     x = math.ramdon()  
  11.     action[key](x)  
  12. end 

Summary: The content of the Lua syntax and alternative measures has been introduced. I hope this article will help you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.