文档详情

第5章 条件语句、循环语句.ppt

发布:2016-07-21约1.14万字共58页下载文档
文本预览下载声明
第5章 条件语句、循环语句、块语句与生成语句;5.1 条件语句(if_else语句);(1)if(表达式)语句 例如: if ( a b ) out1 = int1; (2)if(表达式) 语句1 else 语句2 例如: if(ab) out1=int1; else out1=int2;;(3) if(表达式1) 语句1; else if(表达式2) 语句2; else if(表达式3) 语句3; ........ else if(表达式m) 语句m; else 语句n;;注意(书上6点): 在if和else后面可以包含一个内嵌的操作语句(如上例),也可以有多个操作语句,此时用begin和end这两个关键词将几个语句包含起来成为一个复合块语句。 if语句的嵌套 在if语句中又包含一个或多个if语句称为if语句的嵌套。 应当注意if与else的配对关系,else总是与它上面的最近的if配对。如果if与else的数目不一样,为了实现程序设计者的企图,可以用begin_end块语句来确定配对关系。 ;if(index0) for(scani=0;scaniindex;scani=scani+1) if(memory[scani]0) begin $display(...); memory[scani]=0; end else /*WRONG*/ $display(error-indexiszero);;正确的写法应当是这样的: if(index0) begin for(scani=0;scaniindex;scani=scani+1) if(memory[scani]0) begin $display(...); memory[scani]=0; end end else /*WRONG*/ $display(error-indexiszero);;module counter16(q,clk); output[3:0] q; input clk; reg[3:0] q; always @(posedge clk) q=q+1b1; endmodule ;module counter16b(q,clk,clr); output[3:0] q; input clk,clr; reg[3:0] q=4b0000;//ModelSim仿真用,QuartusII不用初值 always @(posedge clk or posedge clr) begin if(clr) q=4b0000; else q=q+1b1; end endmodule ;module counter10b(q,clk,clr); output[3:0] q; input clk,clr; reg[3:0] q=4b0000;//ModelSim仿真用,QuartusII不用初值 always @(posedge clk or posedge clr) begin if(clr) q=4b0000; else begin if (q==4b1001) q=4b0000; else q=q+1b1; end end endmodule ;module counter10c(q,c,clk,clr); output[3:0] q; output c; input clk,clr; reg[3:0] q=4b0000;//ModelSim仿真用,QuartusII不用初值 reg c=1b0;; always @(posedge clk or posedge clr) begin if(clr) begin q=4b0000; c=1b0; end else ; begin if(q==4b1000) begin q=q+1b1; c=1b1; end else if (q==4b1001) begin q=4b0000; c=1b0; end else begin q=q+1b1; c=1b0; end en
显示全部
相似文档