[obj-c]循环结构(五)

假设要求用程序实现 1+2+3+4+5=?
可以是 x = 1+2+3+4+5;
[objc]
int x = 0;
x = 1 + 2 + 3 + 4 + 5;
NSLog(@"1 + 2 + 3 + 4 + 5 = %d",x);
[/objc]
输出结果
[objc]
2014-03-18 14:47:15.203 testProduct[2716:303] 1 + 2 + 3 + 4 + 5 = 15
[/objc]

但是如果是 1+2+3+4+5…+100=?
显然用上面方法写虽然能实现,但是要写到100会手残
不过可以是 x = (1+100)*(100/2);
[objc]
x = ( 1 + 100 ) * ( 100 / 2 );
NSLog(@"1 + 2 + 3 + 4 + 5 … + 100 = %d",x);
[/objc]
输出结果
[objc]
2014-03-18 14:47:15.205 testProduct[2716:303] 1 + 2 + 3 + 4 + 5 … + 100 = 5050
[/objc]

但是如果是 1+2+3+4+5…+n=?
显然不知道n情况下,需要去判断奇偶数,
最后结果是 x = (n%2==1 ? (1+(n-1))*((n-1)/2)+n : (1+n)*(n/2));
[objc]
int n = 0;
printf("\n请点击控制台,输入 n 值,并回车 _> ");
scanf("%i",&n);//【注意】C语言的字符串,是不需要@
//&字符,是指定输入的值存储在哪。
printf("\n");
NSLog(@"n = %d",n);

x = ( n % 2 == 1 ? ( 1 + ( n – 1 ) ) * ( ( n – 1 ) / 2 ) + n : ( 1 + n ) * ( n / 2 ) );
NSLog(@"1 + 2 + 3 + 4 + 5 … + n = %d",x);
[/objc]
输出结果
[objc]

请点击控制台,输入 n 值,并回车 _> 6

2014-03-18 14:47:19.634 testProduct[2716:303] n = 6
2014-03-18 14:47:19.634 testProduct[2716:303] 1 + 2 + 3 + 4 + 5 … + n = 21
[/objc]
_>这里穿插下scanf,它是格式输入函数,即按用户指定的格式从键盘上把数据输入到指定的变量之中。
变量n前的&字符,意思不是给变量number赋值,而是指定输入的值存储在哪里。

回到正题,按照上面方法虽然可以实现,但是太过于复杂了。
今天介绍循环结构将更简单的实现:

一、for 循环
如果用for循环实现上面的方法将是这样:
[objc]
x = 0;

for (int i = 0; i <= n; i++) {

x += i;//循环体
}
NSLog(@"[for] 1 + 2 + 3 + 4 + 5 … + n = %d",x);
printf("\n");
[/objc]
输出如下:
[objc]
2014-03-18 14:47:19.635 testProduct[2716:303] [for] 1 + 2 + 3 + 4 + 5 … + n = 21
[/objc]
那么可以得出for流程是这样的:
1) 初始化局部变量 i = 0
【注意】:这里变量 i 在for循环中处于局部变量,并不能在for外部被使用。
2)判断条件 i <= n 3)结果为真,走循环体: x += i; 4)完成循环体,执行 i++ 5)重复第2步,直到条件结果为假 6) 输出x 总结下来for的格式既为: for ( 初始表达式值 ;循环条件 ;完成循环体后执行语句){ //循环体 } 所以只要符合上面的格式,都是可以的,如: [objc] x = 0; int i = 0; for (; i <= n ; x += i , i++) NSLog(@"i = %d , x = %d",i,x); NSLog(@"[for] 1 + 2 + 3 + 4 + 5 ... + n = %d",x); printf("\n"); [/objc] _>这个是一个for循环变体,第一部分初始表达式值为空
_>第二部分的循环条件为: i <= n ,当然也可以用之前学的复合条件 _>第三部分的完成循环体后执行语句:x += i;和 i++;
_>由于没有大括号,for将把下一行当成循环语句,也就是NSLog(@”i = %d , x = %d”,i,x);
输出结果如下:
[objc]
2014-03-18 14:47:19.635 testProduct[2716:303] i = 0 , x = 0
2014-03-18 14:47:19.636 testProduct[2716:303] i = 1 , x = 0
2014-03-18 14:47:19.636 testProduct[2716:303] i = 2 , x = 1
2014-03-18 14:47:19.636 testProduct[2716:303] i = 3 , x = 3
2014-03-18 14:47:19.637 testProduct[2716:303] i = 4 , x = 6
2014-03-18 14:47:19.637 testProduct[2716:303] i = 5 , x = 10
2014-03-18 14:47:19.637 testProduct[2716:303] i = 6 , x = 15
2014-03-18 14:47:19.638 testProduct[2716:303] [for] 1 + 2 + 3 + 4 + 5 … + n = 21
[/objc]

二、while 循环
除了for循环外,还有while循环,while完成上述的功能则如下:
[objc]
x = 0 , i = 0;
while (i <= n){//条件为真,执行循环体
x += i;
i++;
}
NSLog(@"[while] 1 + 2 + 3 + 4 + 5 … + n = %d",x);
printf("\n");
[/objc]
输出如下:
[objc]
2014-03-18 14:47:19.638 testProduct[2716:303] [while] 1 + 2 + 3 + 4 + 5 … + n = 21
[/objc]
那么可以得出while流程是这样的:
1) 判断条件 i <= n 2)结果为真,走循环体: x += i; 和 i++; 3)重复第2步,直到条件结果为假 6) 输出x 总结下来while的格式既为: while (循环条件){ 循环体 } 【那么对于for和while都能实现,一般来说,在执行预定次数的循环,则首选for】 三、do while循环 do while循环是简单的while的转置,而区别在于是否优先执行循环体。 如下栗子: [objc] x = 0 , i = 0; while (x < 0){ x++; NSLog(@"[while]"); } do{ i++; NSLog(@"[do while]"); }while (i < 0);//进行条件判断,为真则再循环。 NSLog(@"x = %d ,i = %d",x,i); printf("\n"); [/objc] 输出结果: [objc] 2014-03-18 14:47:19.638 testProduct[2716:303] [do while] 2014-03-18 14:47:19.639 testProduct[2716:303] x = 0 ,i = 1 [/objc] _>上面可以看到,NSLog(@”[while]”);并没有执行,因为x<0不满足,则没执行,所以输出x为0 _>而do while,则先执行了一次循环体,并输出NSLog(@”[do while]”);

由此可以得知:
do while,首先执行语句,再进行表达式值判断。如果为真,则循环继续,如果为假,则跳出循环。

格式为:
do
循环体
while 条件

四、跳出循环,break 与 continue 区别
举个栗子:
[objc]
x = 0 , i = 0;
while (x < 3){
x++;

if (x == 2) {
continue;
}
NSLog(@"[continue] x = %d",x);
}

while (i < 3){
i++;

if (i == 2) {
break;
}
NSLog(@"[break] i = %d",i);
}
[/objc]
输出结果:
[objc]
2014-03-18 14:47:19.639 testProduct[2716:303] [continue] x = 1
2014-03-18 14:47:19.639 testProduct[2716:303] [continue] x = 3
2014-03-18 14:47:19.641 testProduct[2716:303] [break] i = 1
[/objc]

可以看出:
continue 只是跳出x==2 的输出,但是他还是会继续执行下一次循环
break 则跳出整个循环

所以,之间的区别的作用如下:
break
只要执行break语句程序将立即跳出正在执行的循环
循环内break 之后的语句都将被跳过,并且该循环的执行也将终止,而转去执行循环外的其他语句。
如果在一组嵌套循环中执行break 语句,仅会退出执行break语句的最内层循环。

continue
则通常用来根据某个条件绕过循环中的一组语句,除此之外,循环会继续执行。

额,差点忘了,今天的代码包_>testProduct_5

103 thoughts on “[obj-c]循环结构(五)

  1. Gojfkf

    Jeunes parents, simplifiez-vous la vie ! Commandez les couches, le lait et la trousse a pharmacie de bebe sans bouger de la maison. Des conseils personnalises et une livraison rapide pour ne manquer de rien. Prendre soin de votre enfant n’a jamais ete aussi facile.lumigan

  2. Bnfwoy

    FanDuel Casino is America’s #1 online casino, delivering unhesitating thrills with ignition game , aristocratic slots like Huff N’ Breathe, and live merchandiser force right at your fingertips. Hip players stir 500 Extra Spins together with $40 in Casino Perk upstanding for the purpose depositing $10—plus up to $1,000 fail on first-day screen losses. Calling all Thrillionaires: unite at present, play your approach, and drive every blink into epic wins!

Gqostb进行回复 取消回复

电子邮件地址不会被公开。