版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Chapter 2: X+ Control StatementsCHAPTER 2: X+ CONTROL STATEMENTSObjectivesThe objectives are:Declare and use extended data types for variables. Use the various operators available in X+.Control program flow using conditional statements in X+. Repetitively call the same blocks of code by using Loop s
2、tatements.Use standard functions that are built in to the application.Use outputds to display data and messages to the user.IntroductionThis course explains how to use control statements in X+. These statements control the logic flow in the program. This course also describes how to use some built-i
3、n functions in Microsoft Dynamics AX to communicate with the end-user.ScenarioIsaac, the Systems Developer, is implementing a project that requires a modification to code. He has been asked to become familiar with the typical statements in X+ used to control program flow, and to communicate with the
4、 user.2-1Microsoft Official Training Materials for Microsoft DynamicsYour use of this content is subject to your current services agreementDevelopment II in Microsoft Dynamics AX 2012Introduction to VariablesVariables hold data when a block of code executes. Variables all have scope. Scope is the ar
5、ea where the variable can be accessed. Some different types of scope include:Use the type of variable that matches the type of data that you want to store. Youcan name the variable, as long as you do not use names the system aly uses,such asds or keywords. A list ofwords is located in theDeveloper H
6、elp under the Keywords lesson.BEST PRACTICE: Do not use names like string1. Always give a variable ameaningful name so its use is easier to identify whening the code.DeclarationAll variables must be declared before use. When a variable is declared, a smallamount of memory is. The syntax of the decla
7、ration is the same,whether it is a simple variable or an object variable.You cannot mix variable declarations with other X+ statements. Variables must be declared before the statements.There are two rules to use when declaring variables:Declare all variables before anything else in the code.Use a se
8、micolon after each declaration.You might want a variable to have a value other than the default when the variable is declared. X+ supports initialization of variables in the Declaration statement. Initialization is performed by adding the assignment-statement to the variable declaration. For example
9、:Simple Data TypesX+ implements primitive data types that can be used to declare variables in X+ code. You can also create Extended Data Types on the basis of primitive types; this is discussed in the Development I in Microsoft Dynamics 2012 course.2-2Microsoft Official Training Materials for Micros
10、oft DynamicsYour use of this content is subject to your current services agreementint a = 10; / a is assigned the value 10ScopeDescriptionGlobal (to a class)These are variables defined in the classDeclaration of a class.Local (to a method)These are variables defined in a method of a class.Local (to
11、an embedded function)These are variables defined in a function embedded in a method of a class.Chapter 2: X+ Control StatementsThe following table provides an overview of the simple data types available in Microsoft Dynamics AX:2-3Microsoft Official Training Materials for Microsoft DynamicsYour use
12、of this content is subject to your current services agreementData TypeDescriptionExampleDeclaration KeywordStringA string is a number of characters. X+ supports several types of strings: Left aligned, right aligned, fixed length or not fixed length. Theum length of a string is 999 characters.Namestr
13、IntegerAn integer, also named a natural figure, is a number without a decimal point.1090intRealReals, also named decimals, are numbers with a decimal point.3.14realDateThe date type contains day, month, and year.10291978dateUTCDateTimeContains year, month, day, hour, minute and second.9/28/200807:11
14、:02 amutcDateTim eEnumEnum values are represented internally as integers. The first literal has the number 0, the next number 1, the next number 2, and so on. You can use enums as integers in expressions.NoYesMust be declared as a Base Enum firstBooleanBooleans can only contain the values false and
15、true. The enum values false and true are predefined in X+ and recognized by the compiler.TRUEbooleanTimeContains hours, minutes, and seconds. To declare a time, use the system type timeOfDay.15:23:08timeOfDayGUIDGlobal Unique Identifier (GUID) is a reference number which is unique in any context.3F2
16、504E0- 4F89-11D3-9A0C-0305E82C3301guidInt64A large integer, represented by 64 bits.5637144579Int64Development II in Microsoft Dynamics AX 2012The simple variable declaration is frequently used.The syntax for the simple variable declaration is as follows:The data type can be any of the data types in
17、X+. Here are some examples:When declaring a variable, you can also declare them as an extended data type. This is a good practice because it can highlight errors in code at compile time when a variable is used incorrectly.It is common to name the variable the same as the extended data type, when pos
18、sible.Initializing VariablesThe following statements show how to declare the variables for use later in the code. You can declare several variables of the same type with different names. You can also assign a value to the variable when you declare it or later in the code.2-4Microsoft Official Traini
19、ng Materials for Microsoft DynamicsYour use of this content is subject to your current services agreementintcounter, toCount; intfromCount = 1;custcust;transDatetransDate;amountMSTamountDebit, amountCredit;intintegerVariable; realrealVariable;strunboundStringVariable;str 30 boundStringVariable; / ma
20、x of 30 chars datedateVariable;boolean booleanVariable;dataTypevariableIdentifier;Chapter 2: X+ Control StatementsComposite Data TypesIn addition to the simple data types, you can use composite data types when declaring variables.The following composite data types are available:ArraysArrays can be d
21、eclared by adding brackets ( ).You can set the brackets.um number of array elements by putting the number in theArray values can be set by specifying the index when assigning the value.ContainersA container variable can contain different types and values of simple and extended data types, including
22、arrays and other container variables. Classes cannot be put into containers.There are many functions that manipulate container variables. The following functions are available:2-5Microsoft Official Training Materials for Microsoft DynamicsYour use of this content is subject to your current services
23、agreementFunctionDescriptionconPeekReturns the value being held in a specific position in the container.conDelRemoves a value from a specific position in the container.conNullReturns an empty container.realrealUnlimtedArray; / Unlimited index values realrealLimitedArray10; /um of 10 valuesrealLimite
24、dArray2 = 3.142;Data typeDescriptionArrayAn array is a list of items with the same data type and the same name; only the index differs.ContainerA container is a dynamic list of items that can contain primitive data types and some composite data types.ClassesA class is a type definition that describe
25、s both variables and methods for instances (objects) of the class.TablesAll tables defined in the database (in the data dictionary) can be handled as class definitions.Development II in Microsoft Dynamics AX 2012The following examples have a container variable that contains four values, including th
26、ree different data types.OperatorsOperators are used to manipulate variable and field values and to control the logical program flow based on the values in variables and fields. The following types of operators are available.Assignment operators modify the contents of a variable or field.Arithmetic
27、operators perform mathematical operations on the values in a variable or field.Relational operators evaluate how two values relate to one another and return either True or False according to the result.Assignment OperatorsAssignment operators modify the contents of a variable or field. The following
28、 table defines available operators.2-6Microsoft Official Training Materials for Microsoft DynamicsYour use of this content is subject to your current services agreementOperatorDescription=Assigns the expression on the right of the equal sign to the variable on the left.+=Increments the variable on t
29、he left by the value on the right.+Increments the variable on the left by one.-=Decrements the variable on the left by the value on the right.-Decrements the variable on the left by one.container c;/ the container is declared inti, j;strtxt;c = 10, 20, test; / the container has 3 values set print co
30、nPeek(c, 3);/ the third element is printed i,j,txt = c; / other variables are set from the containerFunctionDescriptionconFindFinds the position in the container that a certain value is being held (if found).conInsInserts a value into a specific position in the container.conPokeReplaces the value be
31、ing held in a specific position in the container, with a new value.conLenReturns the number of elements in the container.Chapter 2: X+ Control StatementsArithmetic OperatorsArithmetic operators perform calculations in X+. Arithmetic operators are used like relational operators except for , +, -, +=,
32、 and -=. The following table defines available arithmetic operators:The following are some examples of these arithmetic operators. For all examples, the variable i is an integer.2-7Microsoft Official Training Materials for Microsoft DynamicsYour use of this content is subject to your current service
33、s agreementEvaluated ExpressionReturn Valuei+;Increments the i variable by one.i-;Decrements the i variable by one.i += 2;Increments the i variable by two every time.i -= 3;Decrements the i variable by three every time.i = 3 3i = 24 (i = 3*2*2*2)OperatorTermDescription+PlusAdds expression1 to expres
34、sion2.-MinusSubtracts expression2 from expression1.*MultiplyMultiplies expression1 with expression2./DivideDivides expression1 with expression2.DIVInteger divisionPerforms an integer division of expression1 with expression2.MODInteger remainderReturns the rest of an integer division of expression1 w
35、ith expression2.NotUnary operator: performs a binary not- operation.&Binary AndPerforms a binary and-operation on expression1 and expression2.Binary XORPerforms a binary XOR-operation on expression1 and expression2.|Binary OrPerforms a binary or-operation on expression1 and expression2.Right shiftPe
36、rforms expression2 right shift (a division by two) on expression1.?Ternary operatorTakes three expressions: expression1 ? expression2 : expression3. If expression1 is true, expression2 is returned otherwise expression3 is returned.Development II in Microsoft Dynamics AX 2012NOTE: View more examples
37、of arithmetic operators in the X+ Online Help Guide.Relational OperatorsRelational operators, except for !, are placed between two expressions. The following table defines available relational operators:2-8Microsoft Official Training Materials for Microsoft DynamicsYour use of this content is subjec
38、t to your current services agreementOperatorTermDescription=equalReturns true if both expressions are equal.=greater than or equalReturns true if expression1 is greater than or equal to expression2.greater thanReturns true if expression1 is greater than expression2. 2i = 6 (i = 24/2/2)i = 80 DIV 13i
39、 = 6 (6 is the largest number that 13 can be multiplied by, where the result is less than or equal to 80. In this case, 6*13 = 78, remainder 2)i = 80 MOD 13i = 2 (2 is the remainder after dividing 80 by 13)Chapter 2: X+ Control StatementsThe following are examples using relational operators and thei
40、r return values:Operator PrecedenceYou can use X+ to create complex statements using multiple operators when data types on all parts of the statements are equivalent. When multiple operators are used in one statement, precedence for the operators must be in place for statement evaluation. The follow
41、ing table lists the precedence for operators. The highest precedence is at the beginning of the table; precedence gets lower as you move down the table.2-9Microsoft Official Training Materials for Microsoft DynamicsYour use of this content is subject to your current services agreementOperator TypeOp
42、erator Syntaxpostfix operators . (params) expr+ expr-unary operators+expr -expr +expr -expr !creationnew (type)exprmultiplicative* /additive+ -relational =equality= !=bitwise AND&shiftbitwise exclusive ORbitwise inclusive OR|logical operators (AND, OR)& |conditional? :assignment= += -=Evaluated Expr
43、essionReturn Valueabcdef like abc*TRUE: the * is equal to any string of characters.abcdef like abc?FALSE: the ? is equivalent to one character.9 != 10TRUE: these values are not equal to one another.(10 9) & (11 9)TRUE: the first expression returns true.Development II in Microsoft Dynamics AX 2012Con
44、ditional StatementsConditional statements in programdefine conditions under which certainfunctions are performed. Conditional statements use logical expressions that are evaluated and return a value of either true or false. There are three primaryconditional statements:If statementSwitch statementTe
45、rnary operatorsAll these statements are evaluated using operators.IfThe if statement is the simplest control statement. It checks whether a condition is true or false. If the condition is satisfied, all the code within the braces is executed. The syntax for an if statement is as follows:The followin
46、g is an example of an if statement. If the variable a is greater than 10, the value of a will be printed to the screen.The following is an example of an if statement using multiple expressions to evaluate the condition.2-10Microsoft Official Training Materials for Microsoft DynamicsYour use of this
47、content is subject to your current services agreementif(a 10)print a;if (a 10)print a;if (condition)/if true these statements are executedChapter 2: X+ Control StatementsIf.elseAn if statement checks for only one possibility and ignores all other conditions. An ifelse statement checks one condition
48、and if true, the block of code is executed. Otherwise, an alternative statement is executed. The syntax for an ifelse statement is as follows:The previous conditional formulas allow for only two alternative outcomes. A program might have to check more than two alternatives. To check for multiple alt
49、ernatives, you can use an ifelse.if statement. The syntax for this statement is as follows:2-11Microsoft Official Training Materials for Microsoft DynamicsYour use of this content is subject to your current services agreementif (condition1)/statement1elseif (condition2)/statement2else/statement3inti
50、 = 12;intj = 10;intmax;if (i j)max = i;elsemax = j;if (condition)/if true these statements are executedelse/if false these statements are executedDevelopment II in Microsoft Dynamics AX 2012The system checks condition 1 first, and if satisfied, statement1 is executed. If the condition 1 is not satis
51、fied, it moves to condition2. If condition2 is satisfied, statement2 is executed. If condition2 is not satisfied, then statement3 executes. You can use as many conditions as necessary.You might need to have a condition in a condition. You can do this using nested if statements.A mathematics teacher
52、uses the following criteria to determine who passes or fails the class:Pass the final examPass the homework sectionsCheck the logic in the following order:1.2.If theIf thehas failed the exam, then thehas passed the exam, then check thefails the course.shomework sections.3.If thehas passed the exam and the homework sections, the passes the course.4.Otherwise, thefails.Ternary OperatorThis conditional statement beactly like an ifelse statement. The mainreason to use the ternary operator is convenience in coding. Its syntax is as follows:2-12Microsoft Official Training Materials f
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 电商行业社交电商与直播带货方案
- 质量管理体系建设指导书模板
- 采购供应商信息评估与分析工具
- 采购成本控制流程与操作指南模板
- 员工离职信息安全管理团队预案
- 产品质量担保期限延长承诺书(6篇)
- 活动赞助资金支配承诺书(9篇)
- 湖北省南漳县2026年初三第一次诊断考试英语试题文试题含解析
- 隐秘信息严格保护承诺书(8篇)
- 2026九年级语文下册第五单元阅读重点突破作业课件新人教版
- ecotect教程教学课件
- 综合实践活动(4年级下册)第4课时 换季衣物巧收纳-课件
- GB/T 42903-2023金属材料蠕变裂纹及蠕变-疲劳裂纹扩展速率测定方法
- 幼儿园优质公开课:中班健康《健康精灵》课件
- 肾囊肿围手术期护理查房
- GB/T 43091-2023粉末抗压强度测试方法
- 化工管道更换施工方案
- 2023年江苏省高中生物学竞赛初赛试题
- 不锈钢护栏施工方案方案
- 母亲的白发阅读及答案
- GB/T 6003.1-2022试验筛技术要求和检验第1部分:金属丝编织网试验筛
评论
0/150
提交评论