弱弱地问一下,if语句怎么用
if ... then
if ... then ...;这里加分号不加分号有什么区别
else
end
多重嵌套条件中的;是什么规则
[解决办法]
多看帮助
if语句有两种形式:if...then和if...then...else。if...then的语法是:
if expression then statement
这里的表达式expression返回一个布尔值。如果表达式expression的值为真(True),那么语句statement被执行,否则不被执行。例如
if J <> 0 then Result := I/J;
if...then...else语句的语法是:
if expression then statement1 else statement2
这里的表达式expression同样返回一个布尔值。如果表达式expression的值为真(True),那么语句statement1被执行,否则语句statement2被执行。例如
if J = 0 then
Exit
else
Result := I/J;
then和else子句各含有一个语句,这些子句可以是结构语句。例如
if J <> 0 then
begin
Result := I/J;
Count := Count + 1;
end
else if Count = Last then
Done := True
else
Exit;
注意,在then子句和保留字else之间决不要有分号(;)。为了将if语句与相同块的其他语句分开,可以在整个if语句之后放置一个分号(;),但在then和else之间,除了允许有空格和回车换行之外,不需要任何其他任何标记(注释是允许的)。在if语句中的保留字else之前放置分号(;),是一般的编程错误。
连在一起的嵌套if语句也是需要特别注意。问题的出现是由于一些if语句有else子句而一些没有,但是两种语句在语法上又没有明显的区别。在嵌套的条件系列种,如果else子句比if语句少,那么else子句与if的搭配将不太清楚。对于具有如下形式的语句:
if expression1 then if expression2 then statement1 else statement2;
这里可以出现两种解析结果:
if expression1 then [ if expression2 then statement1 else statement2 ];
if expression1 then [ if expression2 then statement1 ] else statement2;
对于此类情况,编译器总是按照第一种方式解析。简言之,在实际的代码中,语句
if ... { 表达式1 } then
if ... { 表达式2 } then
... { 语句1 }
else
... { 语句2 } ;
等价于
if ... { expression1 } then
begin
if ... { expression2 } then
... { statement1 }
else
... { statement2 }
end;
上面的规则可以概括为,嵌套条件总是解析成从最内层的条件开始,对每个else总是与其左边最近的可用的if搭配。要强制编译器按照第二中方式读出前面的例子,就必需明确写成
if ... { expression1 } then
begin
if ... { expression2 } then
... { statement1 }
end
else
... { statement2 } ;
主题组
相关主题
Case语句
结构语句:概述
编者注
要避免嵌套if语句产生歧义(明确地说,就是编写代码或阅读代码时人的理解与编译器根据规则作出的解析之间有偏差),可以通过尽量用if…then...else语句代替if...then...语句实现。(或者根据情况使用case语句,相关信息见Case语句中的编者注。)下面的两个例子就清楚地表达了代码的逻辑:
例子一:
if A then begin
P1;
end else begin
if B then begin
P2;
end else begin
P3;
end;
end;
例子二:
if A then begin
if B then begin
P1;
end else begin
P2;
end;
end else begin
P3;
end;
从上面两个例子可以看出,编写代码时,文本布局的风格也会影响到代码的可读性。例如,下面的语句读起来就比较费劲:
if A then
begin
P1;
end
else
begin
if B then
begin
P2;
end
else
begin
P3;
end;
end;
尽管这里的代码与“例子一”的程序逻辑是一致的。
[解决办法]
去google搜索Pascal精要
[解决办法]
object pascal
[解决办法]
加分号的句子应该就结束了吧
[解决办法]
2 楼的已经太过详细,
其实简单的说, if then else 是一个整个语句, 也就是说 分号 只能在最后.
如没有 else 那么分号 就 在 then 后面 .
无须要探索, 这个是语法问题, 知道就行, 如果这东西也要去想的话,那所有用delphi的人都先先去探索研究delphi开发历史了.
[解决办法]
1楼的怎么感觉这篇文章好熟悉列
[解决办法]
其实你就把它想象成中文造句
如果 (条件成立) 这时候做什么
若条件不成立 这时候做什么
就相当于
if then
else
[解决办法]
我知道楼主究竟想找什么,end后分号不写只有当有else时,才可不需要,如果if后的语句块没有begin..end,那么if后跟的那条语句也可以不写分号,如:
if true then
showmessage( '1 ') //这里必须没分号
else
showmessage( '2 ');
也可写成
if true then
begin
showmessage( '1 '); //这里必须有分号
end //这里没分号
else
showmessage( '2 ');
就这样了.
[解决办法]
if boolean then
begin
end else
begin
end;
[解决办法]
这样的问题也贴出来,不要学delphi了
[解决办法]
2楼的帅哥你也太详细了把
[解决办法]
2楼的牛,这么耐心阿
[解决办法]
接分
[解决办法]
接分
[解决办法]
if 你=笨笨 then
喝尿尿
else
喝酒;
[解决办法]
if ... then
if ... then ...;这里加分号不加分号有什么区别
else
end
多重嵌套条件中的;是什么规则
--
加分号,则编译错误;不加,则正确,下面else此应此if
[解决办法]
There are two forms of if statement: if...then and the if...then...else. The syntax of an if...then statement is
if expression then statement
where expression returns a Boolean value. If expression is True, then statement is executed; otherwise it is not. For example,
if J <> 0 then Result := I/J;
The syntax of an if...then...else statement is
if expression then statement1 else statement2
where expression returns a Boolean value. If expression is True, then statement1 is executed; otherwise statement2 is executed. For example,
if J = 0 then
Exit
else
Result := I/J;
The then and else clauses contain one statement each, but it can be a structured statement. For example,
if J <> 0 then
begin
Result := I/J;
Count := Count + 1;
end
else if Count = Last then
Done := True
else
Exit;
Notice that there is never a semicolon between the then clause and the word else. You can place a semicolon after an entire if statement to separate it from the next statement in its block, but the then and else clauses require nothing more than a space or carriage return between them. Placing a semicolon immediately before else (in an if statement) is a common programming error.
A special difficulty arises in connection with nested if statements. The problem arises because some if statements have else clauses while others do not, but the syntax for the two kinds of statement is otherwise the same. In a series of nested conditionals where there are fewer else clauses than if statements, it may not seem clear which else clauses are bound to which ifs. Consider a statement of the form
if expression1 then if expression2 then statement1 else statement2;
There would appear to be two ways to parse this:
if expression1 then [ if expression2 then statement1 else statement2 ];
if expression1 then [ if expression2 then statement1 ] else statement2;
The compiler always parses in the first way. That is, in real code, the statement
if ... { expression1 } then
if ... { expression2 } then
... { statement1 }
else
... { statement2 } ;
is equivalent to
if ... { expression1 } then
begin
if ... { expression2 } then
... { statement1 }
else
... { statement2 }
end;
The rule is that nested conditionals are parsed starting from the innermost conditional, with each else bound to the nearest available if on its left. To force the compiler to read our example in the second way, you would have to write it explicitly as
if ... { expression1 } then
begin
if ... { expression2 } then
... { statement1 }
end
else
... { statement2 } ;
[解决办法]
if ... then
if ... then ...;这里加分号不加分号有什么区别
else
加表示下面的else 跟外面层的if
不加表法跟里面层的if
if then else 是一个整体,就近匹配原则,ok ,over