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
- 實驗5 迴圈結構程式設計(while、do-while語句的應用)程式設計While
- 實驗5迴圈結構程式設計(while、do-while語句的應用)程式設計While
- C#程式設計基礎第七課:C#中的基本迴圈語句:while迴圈、do-while迴圈、for迴圈、foreach迴圈的使用C#程式設計While
- Matlab的if語句switch語句for迴圈while迴圈語句練習MatlabWhile
- Java基礎 迴圈語句 for while do.....while語句JavaWhile
- do-while語句和while的區別While
- C#練習,應用for,while,do-while迴圈C#While
- PLSQL Language Referenc-PL/SQL控制語句-迴圈語句-WHILE迴圈SQLWhile
- JavaScript 流程控制語句詳解:if語句、switch語句、while迴圈、for迴圈等JavaScriptWhile
- 實驗三:分別用for、while和do-while迴圈語句以及遞迴方法計算n!,並輸出算式While遞迴
- Python基礎-While迴圈語句PythonWhile
- c語言中,while(1)語句使用break語句跳出迴圈C語言While
- python04: while迴圈語句 break continue for in 迴圈PythonWhile
- Shell指令碼應用 – for、while迴圈語句指令碼While
- 【js迴圈語句】for與while的區別JSWhile
- Python趣味入門5:迴圈語句whilePythonWhile
- 你只用do-while來實現迴圈?太浪費了!While
- while迴圈以及do while迴圈While
- Python 為什麼不設計 do-while 迴圈結構?PythonWhile
- while迴圈While
- C語言程式設計學習中while迴圈和do……while迴圈C語言程式設計While
- TypeScript 迴圈語句TypeScript
- JavaScript for 迴圈語句JavaScript
- MySQL迴圈語句MySql
- Oracle迴圈語句Oracle
- PLSQL Language Referenc-PL/SQL控制語句-迴圈語句-FOR迴圈SQL
- PLSQL Language Referenc-PL/SQL控制語句-迴圈語句-基本迴圈(EXIT語句)SQL
- PLSQL Language Referenc-PL/SQL控制語句-迴圈語句-FOR迴圈-FOR迴圈中的索引SQL索引
- 使用while迴圈操作DML語句While
- PHP For & While 迴圈PHPWhile
- JavaScript跳出for迴圈語句JavaScript
- java 迴圈語句(轉)Java
- Java 迴圈 - for, while 及 do…whileJavaWhile
- 04流程控制 for迴圈,while迴圈While
- PLSQL Language Referenc-PL/SQL控制語句-迴圈語句-FOR迴圈-下限和上限SQL
- C語言中迴圈語句while 中判斷條件出現 || 和 && 的區別C語言While
- python while迴圈PythonWhile