假设要求用程序实现 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
where to buy lipitor without a prescription lipitor 40mg price buy lipitor 10mg pills
oral cipro – purchase myambutol generic clavulanate cost
cipro price – buy myambutol pills amoxiclav online buy
metronidazole 400mg us – buy amoxil tablets zithromax 500mg canada
ciprofloxacin tablet – order chloromycetin online erythromycin 500mg generic
buy valtrex 500mg online cheap – order starlix 120 mg generic zovirax 800mg sale
stromectol 2mg – buy generic sumycin online generic tetracycline 250mg
order generic metronidazole – flagyl tablet order generic zithromax
buy acillin online cheap brand penicillin cost amoxicillin
furosemide price – order minipress 2mg for sale captopril generic
buy retrovir 300mg sale – order epivir generic buy zyloprim generic
brand glycomet 1000mg – septra where to buy where can i buy lincocin
purchase clozapine pill – aceon 8mg generic how to get famotidine without a prescription
seroquel 50mg drug – order trazodone 100mg without prescription buy eskalith paypal
buy atarax online cheap – cost nortriptyline purchase endep online
buy clomipramine 25mg – order abilify 30mg generic sinequan 75mg tablet
amoxil us – cephalexin us generic cipro 500mg
generic augmentin – order bactrim 480mg online order cipro 500mg online cheap
cleocin 150mg drug – cefpodoxime 200mg us chloramphenicol medication
zithromax 500mg tablet – buy sumycin 250mg pill buy ciplox 500 mg generic
purchase albuterol inhalator generic – order allegra 120mg buy theo-24 Cr 400mg online cheap
ivermectin dosage – oral aczone purchase cefaclor pills
purchase clarinex pill – buy cheap clarinex strongest over the counter antihistamine
methylprednisolone pill – buy fml-forte cheap buy azelastine 10ml generic
order micronase generic – cheap dapagliflozin brand dapagliflozin
repaglinide 1mg price – buy jardiance 10mg without prescription empagliflozin 10mg pill
buy glucophage online – cozaar sale buy acarbose online cheap
buy terbinafine online cheap – griseofulvin canada brand griseofulvin
buy rybelsus 14mg pills – glucovance sale desmopressin usa
ketoconazole 200 mg without prescription – order ketoconazole 200 mg generic oral sporanox 100mg
lanoxin 250mg without prescription – lanoxin ca furosemide where to buy
famvir 250mg uk – buy generic famvir 250mg valaciclovir pills
order hydrochlorothiazide without prescription – order hydrochlorothiazide sale order zebeta 5mg online
order metoprolol 50mg – buy hyzaar generic buy generic adalat online
buy nitroglycerin pills – buy nitroglycerin medication buy generic valsartan online
crestor pills sudden – rosuvastatin online whirl caduet online shake
zocor auditor – zocor apply atorvastatin cry
buy viagra professional conduct – viagra gold creep levitra oral jelly shaft
cenforce online intend – zenegra valentine brand viagra pills chair
dapoxetine finish – suhagra bush cialis with dapoxetine sad
brand cialis ponder – zhewitra window penisole lake
cialis soft tabs online hour – valif shoot viagra oral jelly queen
asthma treatment afford – inhalers for asthma expedition asthma treatment hunt
treatment for uti cut – uti treatment wood uti medication far
prostatitis pills need – prostatitis medications swear pills for treat prostatitis slow
valtrex pills agree – valacyclovir online sacrifice valtrex pills upright
loratadine frog – loratadine medication sun claritin pills westward
loratadine medication jack – claritin pills blade loratadine ankh
dapoxetine whip – dapoxetine bee priligy ink
promethazine operation – promethazine black promethazine sorry
ascorbic acid bomb – ascorbic acid recent ascorbic acid beauty
fludrocortisone pills recollect – nexium pills recall lansoprazole interval
clarithromycin pills lore – cytotec protest cytotec pills frog
dulcolax price – oxybutynin over the counter liv52 20mg brand
order aciphex 20mg – domperidone without prescription motilium buy online
purchase bactrim sale – cotrimoxazole cost tobra 5mg canada
buy hydroquinone online – purchase desogestrel how to get dydrogesterone without a prescription
dapagliflozin for sale – dapagliflozin order precose drug
order griseofulvin 250mg for sale – dipyridamole uk lopid online
enalapril ca – buy generic latanoprost xalatan uk
dramamine medication – order risedronate 35 mg online cheap actonel for sale online
hydrea where to buy – disulfiram 250mg sale methocarbamol order
purchase nootropil online – buy piracetam 800 mg for sale sinemet 20mg ca
purchase cytoxan generic – trimetazidine pills oral vastarel
purchase spironolactone – where can i buy dilantin buy naltrexone online
zofran uk – buy tolterodine paypal ropinirole online order
brand flexeril – buy cyclobenzaprine 15mg for sale order vasotec online
order ascorbic acid 500mg pills – compro cheap oral prochlorperazine
durex gel buy online – purchase zovirax eye drops zovirax oral
order minoxidil – how to get dutas without a prescription cost proscar
cost atenolol 100mg – purchase carvedilol carvedilol 25mg over the counter
buy verapamil tablets – diovan cheap tenoretic without prescription
atorlip price – where can i buy zestril nebivolol 20mg oral
purchase lasuna pill – diarex online buy himcolin pills
noroxin pills – purchase norfloxacin pills buy generic confido for sale
finasteride oral – doxazosin price alfuzosin 10mg drug
buy oxcarbazepine 300mg pill – purchase trileptal without prescription buy levothyroxine sale
真实的人类第三季高清完整官方版,海外华人可免费观看最新热播剧集。
Jackpot Real Money Pokies Australia Tonight Could Be Epic
范德沃克高清完整版,海外华人可免费观看最新热播剧集。
Ready to risk it all? burst game offers heart-pounding crossings with huge upside! Cash out early or go all-in — the choice defines your session. Jump in now!