您的位置:首页 > 理论基础

coursera《计算机程式设计》学习笔记2

2014-09-18 19:41 141 查看
week2

1. if then

if (condition)<span style="font-family: Arial, Helvetica, sans-serif;">{</span>
statement1;
statement2;
statement3;
}

2. if then else
<pre name="code" class="html">if(condition)
statement1;
else
statement2;


3. else if

if (condition1)
statement1;
else if (condition2)
statement2;
else
statement3;

4. 判断式值
(condition) ? expression1 : expression2

5. swith判断
swith(flag) {
case 1:
statement1;
break;
case 2:
statement2;
break;
. . .
case n:
statementn;
break;
default:
default_statement;
}flag必须是变数,而非算式。
case之后必须是常熟,而非算式。

break不可省。

当执行完对应的statement之后必须跳出switch。

为了程式的正确性,必须在每个switch加上default用以处理例外的情况。

6. while回圈

while (condition)
statement;
while (condition){
statement1;
statement2;
statement3;
}


7. for 回圈
for (initialization; condition; adjustment)
statement;


8. 判断正整数n是否为质数的方法
试着用2到根号n去除n,如果余数为0,则设定 j 为1;如果 j 始终为0,则 n 为质数。

j 作为一个旗标。

9. do while 回圈

do
statement;
while (condition);do while 至少会执行一次

10. break

跳出回圈

continue

跳过回圈剩下的部分,继续新的回圈
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: