欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Object-C,循环语句for,while,do-while

程序员文章站 2022-03-24 22:23:22
演示循环语句,for,while,do-while,和c系列的语言,语法基本一致1到10,求和 // // main.m // for-while // // created by...
演示循环语句,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 


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