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

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

跳过回圈剩下的部分,继续新的回圈


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值