Object-C, loop statement for, while, do-while

Source: Internet
Author: User

Object-C, loop statement for, while, do-while
Demonstrate the loop statements, for, while, do-while, and C series languages. The syntax is basically the same as 1 to 10, and the sum is obtained.

//// 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 Loop int number = 10; int sum = 0; // int n = 0; you can also define the int Variable n for (int n = 1; n <= number; n ++) {sum + = n ;} NSLog (@ "The sum is % I", sum); // while LOOP int n2 = 1; int sum2 = 0; while (n2 <= 10) {sum2 + = n2; n2 ++;} NSLog (@ "The sum2 is % I", sum2); // do-while loop int n3 = 1; int sum3 = 0; do {sum3 + = n3; n3 ++;} while (n3 <= 10); NSLog (@ "The sum3 is % I", sum3 );} return 0 ;}
 

Program output

14:25:08. 975 for-while [2983: 192052] The sum is 55

14:25:08. 977 for-while [2983: 192052] The sum2 is 55

14:25:08. 978 for-while [2983: 192052] The sum3 is 55

Program ended with exit code: 0

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.