Halcon控制语句

*选择语句:

•if语句:if-->endif
•switch语句:switch-->endswitch
•清空窗体:dev_clear_window ()
•文本显示:disp_message()
cs 复制代码
*// 选择语句:if-else switch-case

*// 获取句柄
dev_get_window (WindowHandle)
*// 清空窗口内容
dev_clear_window ()

*// if-else语句的作用区域是: if-->endif
*// 单支情形
num := 10
if (num>5)
    *// 在图形窗口显示文本信息
    *// 参数1 图形窗口句柄(指定显示窗口)
    *// 参数2 OK 要显示的文本内容
    *// 参数3 window 坐标参考基准(窗口左上角为原点)
    *// 参数4 12 文本Y坐标(向下为正)
    *// 参数5 12 文本X坐标(向右为正)
    *// 参数6 black 文本显示颜色
    *// 参数7 true 显示前清空窗口内容
    disp_message (WindowHandle, 'OK', 'window', 12, 12, 'black', 'true')
else
    disp_message (WindowHandle, 'NG', 'window', 12, 12, 'black', 'true')
endif

*// 多支情形
light:='red'
if (light=='red')
     disp_message (WindowHandle, '禁止通行', 'window', 52, 12, 'black', 'true')
elseif (light=='green')
     disp_message (WindowHandle, '通行', 'window', 52, 12, 'black', 'true')
else
     disp_message (WindowHandle, '等一等', 'window', 52, 12, 'black', 'true')
endif

*// 嵌套情形
salary := 8000
sex := '男'
if (sex == '男')
    if (salary>=8000)
        disp_message (WindowHandle, '结婚吧', 'window', 82, 12, 'black', 'true')
    else
        disp_message (WindowHandle, '加油兄弟', 'window', 82, 12, 'black', 'true')
    endif
elseif (sex=='女')
    if (salary>=5000)
        disp_message (WindowHandle, '结婚吧', 'window', 82, 12, 'black', 'true')
    else
        disp_message (WindowHandle, '加油姑娘', 'window', 82, 12, 'black', 'true')
    endif   
endif

*// switch-case语句
*// 星期判断
week := 5
switch (week)
    case 1:
         disp_message (WindowHandle, '星期一', 'window', 112, 12, 'black', 'true')
    break
    case 2:
         disp_message (WindowHandle, '星期二', 'window', 112, 12, 'black', 'true')
    break
    case 3:
         disp_message (WindowHandle, '星期三', 'window', 112, 12, 'black', 'true')
    break
    case 4:
         disp_message (WindowHandle, '星期四', 'window', 112, 12, 'black', 'true')
    break
    case 5:
         disp_message (WindowHandle, '星期五', 'window', 112, 12, 'black', 'true')
    break
    case 6:
         disp_message (WindowHandle, '星期六', 'window', 112, 12, 'black', 'true')
    break
    case 7:
         disp_message (WindowHandle, '星期日', 'window', 112, 12, 'black', 'true')
    break
    default:
        disp_message (WindowHandle, '异常情况', 'window', 112, 12, 'black', 'true')
    break
endswitch

*// 练习月份天数判断
year := 2026
month := 2
switch (month)
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
        disp_message (WindowHandle, '31天', 'window', 132, 12, 'black', 'true')
    break
    case 4:
    case 6:
    case 9:
    case 11:
        disp_message (WindowHandle, '30天', 'window', 132, 12, 'black', 'true')
    break 
    case 2:
        if ((year % 4 == 0 and year % 100 != 0) or (year % 400 == 0))
            disp_message (WindowHandle, '29天', 'window', 132, 12, 'black', 'true')
        else
            disp_message (WindowHandle, '28天', 'window', 132, 12, 'black', 'true')
        endif
        break
    default:
        disp_message (WindowHandle, '异常情况', 'window', 132, 12, 'black', 'true')
    break    
endswitch

*循环语句:

•for语句:for 初始变量 := 初始值 to 结束值 by 步长 .....endfor
•while语句:while(条件语句).....endwhile
•repeat-until语句:repeat....代码块内容 until(条件语句)
cs 复制代码
*// Halcon循环语句: for语句、while语句、repeat-until语句

*// for循环结构: for 初始变量 := 初始值 to 结束值 by 步长 .....endfor
*// 1-100之间的累加和
*// 定义初始值
sumAdd := 0
for Index := 1 to 100 by 1
    sumAdd := sumAdd + Index
endfor
 disp_message (WindowHandle, sumAdd, 'window', 162, 12, 'black', 'true')

*// 求1000以内的水仙花数
*// 水仙花数:每个各位立方和等于这个数本身就是水仙花数
str := ''
for Index1 := 100 to 999 by 1
    ge := Index1 % 10
    shi := (Index1 / 10) % 10
    bai := Index1 / 100
    sum1 := (ge * ge * ge) + (shi * shi * shi) + (bai * bai * bai)
    if (sum1 == Index1)
        str := str + Index1 + ' '
    endif   
endfor
 disp_message (WindowHandle,'1000以内的水仙花数是: ' + str + ' ', 'window', 192, 12, 'black', 'true')

*// 打印章节内容:总共有七章,每章有6节
 for i := 1 to 7 by 1
     for j := 1 to 6 by 1
         disp_message (WindowHandle, '第'+i+'章,第'+j+'节', 'window',12+i*40,12+j*80, 'black', 'true')     
     endfor
 endfor
 
*// 练习:使用for循环 在窗口中打印九九乘法表
str := ''
for x := 1 to 9 by 1
    for y := 1 to x by 1
        str := y + ' * ' + x + ' = ' + (y * x)
         disp_message (WindowHandle, str, 'window',12+x*40,12+y*80, 'black', 'true')
    endfor  
endfor
 
*// while循环语句//---------------------
*// 语法结构: while(条件语句).....endwhile
*// 求1-100之间的累加和
 Index := 1
 sumAdd := 0
 while (Index <= 100)
     sumAdd := sumAdd + Index
     Index := 1 + Index
 endwhile
 disp_message (WindowHandle, sumAdd, 'window', 12, 12, 'black', 'true')

*// repeat...until语句 重复执行....一直到满足条件循环结束//---------------------
*// 语法结构:repeat....代码块内容 until(条件语句)
*// 求1-100的累加和
Index := 1
sumAdd := 0
repeat
     sumAdd := sumAdd + Index
     Index := 1 + Index
until  (Index > 100)
disp_message (WindowHandle, sumAdd, 'window', 42, 12, 'black', 'true')
 
*// 使用repeat...until语句求1000以内的水仙花数
str := ''
Index := 100
repeat
    ge := Index % 10
    shi := (Index / 10) % 10
    bai := Index / 100
    sum1 := (ge * ge * ge) + (shi * shi * shi) + (bai * bai * bai)
    if (sum1 == Index)
        str := str + Index + ' '
    endif 
    Index := 1 + Index
until (Index > 999)
disp_message (WindowHandle, '1000以内的水仙花数是: ' + str + ' ', 'window', 72, 12, 'black', 'true')

*// 使用repeat...until语句求100以内的质数
*// 质数是指大于 1 的自然数,且满足:除了 1 和它本身之外,不能被任何其他自然数整除。
dev_get_window(WindowHandle)
*// 100以内的质数
index := 1
sumadd :=  ''
repeat
    for index2 := 2 to index by 1
        if ((index % index2 == 0))
            if ((index2 != index)and(index2 != index))
            break
            endif
            if (index2 = index)
            sumadd := sumadd+','+index
            endif
        endif
    endfor
    index := index+1
until (index>100)
*// 输出文本
disp_message(WindowHandle,sumadd,'window',12,12,'red','true')

*// 100以内的质数2
sumadd :=''
index :=1
repeat
    index2 :=2
    repeat
        if ((index % index2==0))
            if ((index2 != index)and(index2 != index))
            break
            endif
            if (index2 = index)
            sumadd := sumadd+','+index
            endif
        endif
        index2 := index2+1
    until (index2>index)
    index := index+1
until (index>100)
*// 输出文本
disp_message(WindowHandle,sumadd,'window',12,12,'red','true')

*跳转语句:

•continue:跳转语句
•break:结束当前它所在的循环
•return():返回同时还有结束函数
•exit():关闭当前它所打开的窗口
•stop():停止正在运行的程序
cs 复制代码
*// Halcon跳转语句:continue、 break、 return()、 exit()、 stop()

*// 求1-100的累加和
dev_get_window (WindowHandle)
sumCount := 0

for Index2 := 1 to 100 by 1
    *// 碰到偶数跳过, 不累加
    if (Index2 % 2 == 0)
        *// 跳出本次循环, 继续下次循环, 如果没有下次跳出整个循环
        continue
    endif
    
    *// 当累加到80(循环变量)时, 结束程序
    if (Index2 == 80)
        *// 返回同时还有结束函数, 此刻使用在main函数当中, 结束整个程序
        return ()
    endif
    
    *// 每累加10个值, 程序停止一下
    if (Index2 % 10 == 0)
        *// 停止正在运行的程序, 如果想继续启动, 点运行从当前代码开始运行
        stop ()
    endif
    
    *// 当累加到7的倍数(不包含7), 结束循环
    if (sumCount % 7 == 0 and sumCount != 7)
        *// 结束当前它所在的循环, 如果碰到嵌套循环, 它只能跳出它所在的内层循环, 外层的循环不能结束
        break
    endif
    sumCount := sumCount + Index2
endfor
disp_message (WindowHandle, sumCount, 'window', 12, 12, 'black', 'true')

*// 当展示玩累加和值, 退出程序
*// 关闭当前它所打开的窗口
*exit ()

*异常捕获语句:

•异常捕获语句:try-catch-endtry
cs 复制代码
*// Halcon异常捕获语句:try-catch-endtry 
*// 我们可以把可能产生异常的代码放入到try语句中,通过catch语句捕获异常,不管是否处理,不会影响程序后续执行
*// 除数不能为0
num1 := 1
num0 := 0
try
    num := num1/num0
catch (Exception)
    disp_message (WindowHandle, Exception, 'window', 12, 12, 'black', 'true')
endtry
*// 手动抛出异常 throw(['异常信息'])
throw ('登录异常')

希望对大家有所帮助, 感谢大家的关注和点赞。

相关推荐
一碗绿豆汤16 小时前
Java语言概述和开发环境-1
java·开发语言
薛定谔的猫198216 小时前
LlamaIndex(三) LlamaHub工具集
数据库·mysql·llamahub
桂花饼16 小时前
基于第三方中转的高效 Sora-2 接口集成方案
人工智能·aigc·ai视频生成·gemini 3 pro·gpt-5.2·ai绘画4k·sora_video2
golang学习记16 小时前
Zed 编辑器的 6 个隐藏技巧:提升开发效率的「冷知识」整理
人工智能
程序员Agions16 小时前
程序员武学修炼手册(二):进阶篇——小有所成,从能跑就行到知其所以然
前端·程序员
mit6.82416 小时前
山脉二分找中值|子集型回溯
算法
小画家~16 小时前
第四十六: channel 高级使用
java·前端·数据库
武汉大学-王浩宇16 小时前
LLaMa-Factory的继续训练(Resume Training)
人工智能·机器学习
晴天¥16 小时前
了解Oracle中的体系结构
数据库