Object-C,迴圈語句for,while,do-while
演示迴圈語句,for,while,do-while,和C系列的語言,語法基本一致1到10,求和
//
// main.m
// for-while
//
// Created by fansunion on 15/11/29.
// Copyright (c) 2015年 demo. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
//for迴圈
int number = 10;
int sum =0;
//int n = 0; 也可以在上面定義int變數n
for(int n=1;n<=number;n++){
sum += n;
}
NSLog(@"The sum is %i",sum);
//while迴圈
int n2 = 1;
int sum2 = 0;
while(n2 <= 10){
sum2 += n2;
n2++;
}
NSLog(@"The sum2 is %i",sum2);
//do-while迴圈
int n3=1;
int sum3=0;
do{
sum3 += n3;
n3++;
}while(n3<=10);
NSLog(@"The sum3 is %i",sum3);
}
return 0;
}
程式輸出
2015-11-29 14:25:08.975 for-while[2983:192052] The sum is 55
2015-11-29 14:25:08.977 for-while[2983:192052] The sum2 is 55
2015-11-29 14:25:08.978 for-while[2983:192052] The sum3 is 55
Program ended with exit code: 0
相關文章
- C語言——迴圈結構(for迴圈,while迴圈,do-while迴圈)C語言While
- 七 while迴圈語句While
- 實驗5 迴圈結構程式設計(while、do-while語句的應用)程式設計While
- 實驗5迴圈結構程式設計(while、do-while語句的應用)程式設計While
- C#程式設計基礎第七課:C#中的基本迴圈語句:while迴圈、do-while迴圈、for迴圈、foreach迴圈的使用C#程式設計While
- Java基礎 迴圈語句 for while do.....while語句JavaWhile
- do-while語句和while的區別While
- JavaScript 流程控制語句詳解:if語句、switch語句、while迴圈、for迴圈等JavaScriptWhile
- Python基礎-While迴圈語句PythonWhile
- python04: while迴圈語句 break continue for in 迴圈PythonWhile
- C#練習,應用for,while,do-while迴圈C#While
- 實驗三:分別用for、while和do-while迴圈語句以及遞迴方法計算n!,並輸出算式While遞迴
- c語言中,while(1)語句使用break語句跳出迴圈C語言While
- Shell指令碼應用 – for、while迴圈語句指令碼While
- Python趣味入門5:迴圈語句whilePythonWhile
- 【js迴圈語句】for與while的區別JSWhile
- while迴圈以及do while迴圈While
- Python 為什麼不設計 do-while 迴圈結構?PythonWhile
- 你只用do-while來實現迴圈?太浪費了!While
- 分支語句和迴圈語句
- for 迴圈與 while 迴圈While
- while迴圈 case迴圈While
- Ruby迴圈語句
- TypeScript 迴圈語句TypeScript
- MySQL迴圈語句MySql
- JavaScript for 迴圈語句JavaScript
- C語言程式設計學習中while迴圈和do……while迴圈C語言程式設計While
- while迴圈While
- Python的迴圈語句Python
- 7-迴圈語句
- JavaScript跳出for迴圈語句JavaScript
- PHP For & While 迴圈PHPWhile
- python while迴圈PythonWhile
- 善用 do-whileWhile
- Java 迴圈 - for, while 及 do…whileJavaWhile
- C語言中迴圈語句while 中判斷條件出現 || 和 && 的區別C語言While
- Python-條件語句和迴圈語句Python
- Python 迴圈語句的使用Python
- MyBatis xml foreach迴圈語句MyBatisXML