zoukankan      html  css  js  c++  java
  • volcanol的工控博客

      这一节描述基本语法中的流程语句: 条件语句 IF语句、 选择语句 Case语句、循环语句  while/repeat/for、以及continue、break语句,还有终止程序

    运行流程Exit、Halt方法。

      废话不多说,直接贴代码。

    {       Delphi语句
    1、if语句
    2、case语句
    3、循环语句
    4、用于循环的 continue  和 break 语句
    5、程序终止或中止功能 Exit、Halt、Terminate方法
    }
    
    program Statement;
    
    {$APPTYPE CONSOLE}
    
    uses
      SysUtils,
      StrUtils, //引用字符串操作函数
      Windows;  //引用系统函数
    
    type
        charSet = set of ansichar;
    
    var
        //注意Delphi2010里面String变量相当于WideString
        dirPath:string;
        dirAnsiPath:ansistring;
    
        //定义一个整型变量
        strVar:string;
        nVar:integer;
    
        //定义一个ansichar变量
        chVar:ansichar;
        charSetVar:charSet;
    
    procedure showExit();
    begin
        WriteLn('Go into procedure showExit');
        Exit();
        //由于调用Exit()方法,因此会退出当前函数的执行,下面的语句不会被执行
        WriteLn('Go out procedure showExit()');
    end;
    
    procedure showHalt();
    begin
        {
        1、Halt可以返回一个错误码,如果不带参数则不能有 ( ) 函数调用符
        2、如果用 ( ) 函数调用符,则必须带参数
        }
        Halt(2);
    end;
    
    procedure showTerminate();
    begin
        //Terminate() 方法用于终止 GUI程序的执行,这里就不说明啦
    end;
    
    
    begin
        //通过系统API函数获取系统路径
        GetWindowsDirectory(PWideChar(dirPath),256);
        dirAnsiPath := dirPath;
        dirAnsiPath := 'Window have install in' + dirAnsiPath;
        WriteLn(dirAnsiPath);
    
        {       IF语句
        1、IF语句的第一种形式
        2、如果有else分支,则then后面的 begin/end 语句块 end后面不能有分号
        3、如果有else分支,则then后面的语句必须为语句块 begin/end
        }
        if dirPath = 'C:windows' then
        begin
            WriteLn('Windows have install in default partion.');
        end
        else
        begin
            WriteLn('Windows have install in default partion.');
        end;
    
        {   IF语句
        1、不带esle子句的if语句
        }
        if True then
            WriteLn('This is simple if statement');
    
    
        {
        if .... then
            ...
        else if ...  then
            ...
        esle if ...  then
            ...
        else
            ...
        }
        //通过函数 Read 读取数字
        Read(nVar);
        if nVar = 1 then
        begin
              WriteLn('status 1');
        end
        else if nVar = 2 then
        begin
            WriteLn('status 2');
        end
        else if nVar = 3 then
        begin
            WriteLn('status 3');
        end
        else
        begin
            WriteLn('other status');
        end;
    
    
        {   case 语句
    
        }
        case nVar of
            1:
                WriteLn('*');
            2:
                WriteLn('**');
            3:
                WriteLn('***');
            4:
                WriteLn('****');
        else
            WriteLn('Input is not 1..4');
        end;
    
        {       循环语句
        1、while循环
        2、repeat循环
        3、for循环
        }
        nVar := 0;
    
        {
        1、while循环语句
        }
        while not ( nVar = 10 )  do  //注意 not的优先级比 关闭比较符的优先级要高
        begin
            Inc(nVar);
            WriteLn(nVar);
        end;
    
        {       Repet
        1、repeat循环语句,类似于C语言中的 Do...while,就是循环体至少会执行一次
        }
        repeat
            WriteLn(nVar);
            Dec(nVar);
        until nVar = 0;
    
        {       for循环语句
        1、for有两种形式,
        2、语法格式1, 计数器向上增加
                for 计数器 := 初始值 to 终值  do
                    循环体
        3、语法格式2, 计数器向下减小
                for 计数器 := 初始值 downto 终值 do
                    循环体
        }
        for nVar := 3 to 10 do
        begin
            WriteLn(nVar);
        end;
    
        for nVar := 10 downto 5 do
        begin
            WriteLn('This is downto for loop.');
            WriteLn(nVar);
        end;
    
        {   基于集合的for循环语句
        1、针对集合中的元素进行循环
        2、for I in set do 循环,中的set必须是已经初始化的集合变量
           不能使集合类型,或者是未初始化的集合变量
        3、这个格式的for循环还可以变量数组、记录、字符串、类和接口
        }
        //初始化集合变量
        charSetVar := ['a'..'z'];
        for chVar in charSetVar do
        begin
            WriteLn(chVar);
        end;
    
        {       continue语句
        1、continue语句的用法和 C语言中的用法一样,用于中止本次循环
        }
        for nVar := 0 to 100 do
        begin
            if nVar <= 90 then
                begin
                    Continue;
                end
            else
                begin
                    WriteLn(nVar);
                end;
        end;
    
        {       break语句
        1、break语句与 C 语言中个的用法一样
        }
        while nVar > 70 do
        begin
            Dec(nVar);
            if nVar < 76  then
            begin
                break;
            end
            else
                WriteLn(nVar);
        end;
    
    
        {       Exit 方法
        1、Exit用于退出当前正在执行的程序块,但是不会退出整个程序的执行
        2、当Exit用于主程序的程序块的时候就是退出程序
        3、在try....finally...调用 Exit 会被当做异常处理
        }
        showExit();
        WriteLn('In Main block.');
    
    
        {       Halt 方法
        1、Halt用于退出当前正在执行的程序
        }
        ShowHalt();
        WriteLn('This statement can never be excuted.');
    
    
        readln;
        readln;
    end.

      欢迎转载本系列文章,转载请注明来源。

  • 相关阅读:
    2015.2.27 UltraEdit中显示XML结构
    2015.1.31 DataGridView自动滚动到某行
    2015.1.15 利用函数实现将一行记录拆分成多行记录 (多年想要的效果)
    2015.1.15 利用Oracle函数返回表结果 重大技术进步!
    2015.1.15 利用Oracle函数插入表结构 Bulk collect into 不用循环,简洁高效
    2015.1.8 Left join 左连接
    2015.1.10 解决DataGridView SelectionChanged事件自动触发问题
    delphi 遍历窗口
    delphi 访问 protected 属性 哈哈
    clientdataset 读取excel 如果excel 文件不存在的时候 相应的gird 会不显示数据, 鼠标掠过 gird 格子 才会显示数据。 这是一个bug 哈哈
  • 原文地址:https://www.cnblogs.com/volcanol/p/5326976.html
Copyright © 2011-2022 走看看