首页/文章/ 详情

HyperWork必备知识-templex语言3-循环结构

3月前浏览813


foreach循环






{num = 0}{gmat = {10, 20, 30, 40}}{foreach(elem = gmat); num++}   Element {num}: {elem}{endloop}



我们可以来完成阶乘计算的功能










{num = 0}{gmat = {1, 2, 3, 4,5,6,7,8,9}}{foreach(elem = gmat); num++}   {fact = 1}   {for(i = 1; i <= elem; i++)}      {fact = fact * i}   {endloop}   Element {num} ({elem}!): {fact}{endloop}



通过for计算指定角度的正弦和余弦值:





{for (a=0; a<=3.1415926; a+=3.1415926/12)}   Angle:{a, %6.4f} Sin:{sin(a), %6.4f} Cos:{cos(a), %6.4f}{endloop}


while循环输出字符,cr()在这里表示换行符,大括号可以同时包括多行代码。








{ i = 0; while(i < 5); i; cr(); i++ endloop}


计算 1 到 10 的每个数字的平方,同时检查平方结果是否是偶数或奇数。











{i = 1;}{while(i <= 10);}   {square = i * i;}   {if(square % 2 == 0)}     Number {i} squared is {square}, which is even.   {else}     Number {i} squared is {square}, which is odd.   {endif}   {i++;} {endloop}


同时应用while,for,if的案例


















{numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}{stop_number = 10}{i = 0}{while(i < stop_number)}{current_number = numbers[i]}{if(current_number % 2 == 0)}{  {fact = 1}}{  for(j = 1; j <= current_number; j++)}{    fact = fact * j}{  endloop}   Number {current_number} is even, its factorial is {fact}.{else}   {  square = current_number * current_number}      Number {current_number} is odd, its square is {square}.{endif}   {i++}   {endloop}

九九乘法的显示










{for (i = 1; i <= 9; i++)}  {for (j = 1; j <= 9; j++)}    {product = i * j}    {if(j == 1)}      {cr()}    {endif}    {product, %2d} x {j, %2d} = {i*j, %2d}  {endloop}{endloop}



来源:TodayCAEer
ACTUM
著作权归作者所有,欢迎分享,未经许可,不得转载
首次发布时间:2024-08-14
最近编辑:3月前
TodayCAEer
本科 签名征集中
获赞 18粉丝 27文章 242课程 0
点赞
收藏
作者推荐

HyperWork必备知识-templex语言5-逻辑判断

关键字if,elseif,else,endif输入{numer=5}{denom=4}{if(numer==0)}Thenumeratoris0,sotheresultis0.{elseif(denom==0)}Thedenominatoris0,soaresultisimpossible.{else}Theresultis:{numer/denom}.{endif}输出Output:Theresultis:1.25.输入{x=5;y=4}{if(x%y)}Theremainderis{x%y}.{else}{x}isamultipleof{y}.{endif}输出Output:Theremainderis1.通过判断传入的运算符采用不同的计算方式并返回结果{num1=10}{num2=5}{operation=&quot;divide&quot;}{if(operation==&quot;add&quot;)}Resultofaddition:{num1+num2}{elseif(operation==&quot;subtract&quot;)}Resultofsubtraction:{num1-num2}{elseif(operation==&quot;multiply&quot;)}Resultofmultiplication:{num1*num2}{elseif(operation==&quot;divide&quot;)}{if(num2==0)}Divisionbyzeroisnotpossible.{else}Resultofdivision:{num1/num2}{endif}{else}Unknownoperation.{endif}我们可以把相同的代码写成函数的形式{defineuser_calculate(num1,num2,operation)}{if(operation==&quot;add&quot;)}Resultofaddition:{num1+num2}{elseif(operation==&quot;subtract&quot;)}Resultofsubtraction:{num1-num2}{elseif(operation==&quot;multiply&quot;)}Resultofmultiplication:{num1*num2}{elseif(operation==&quot;divide&quot;)}{if(num2==0)}Divisionbyzeroisnotpossible.{else}Resultofdivision:{num1/num2}{endif}{else}Unknownoperation.{endif}{enddefine}{num1=10086}{num2=5}{operation=&quot;divide&quot;}{user_calculate(num1,num2,operation)}来源:TodayCAEer

未登录
还没有评论
课程
培训
服务
行家
VIP会员 学习 福利任务 兑换礼品
下载APP
联系我们
帮助与反馈