SlideShare a Scribd company logo
Object-Oriented Programming Language
                                                      Chapter 5 : Program Looping


                                                Atit Patumvan
                                Faculty of Management and Information Sciences
                                              Naresuan University




วันจันทร์ท่ี 27 กุมภาพันธ์ 12
2



                                                                                      Contents



              •        The for statement

              •        The while statement

              •        The do statement

              •        The break and continue Statements




 Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University              Object-Oriented Programming Language

วันจันทร์ท่ี 27 กุมภาพันธ์ 12
3



                                                                        Triangular Number




                                                                                      n=4


                                                                                      sum = 1 + 2 + ... + n




 Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                       Object-Oriented Programming Language

วันจันทร์ท่ี 27 กุมภาพันธ์ 12
4



                                                        Triangular Number Example

Program 5.1
01: #import <Foundation/Foundation.h>
02:
03:// Program to calculate the eighth triangular number
04:
05: int main(int argc, const char * argv[])
06:{
07:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
08:! int triangularNumber;
09:
10:! triangularNumber = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8;
11:
12:! NSLog(@"The eighth triangular number is %i", triangularNumber);
13:
14:! [pool drain];
15:! return 0;
16:}


      The eighth triangular number is 36




 Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University   Object-Oriented Programming Language

วันจันทร์ท่ี 27 กุมภาพันธ์ 12
5



                                                                          The for Statement

Program 5.2

01: #import <Foundation/Foundation.h>
02:
03:
04: int main(int argc, const char * argv[])
05: {
06:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
07:! int n, triangularNumber;
08:
09:! triangularNumber = 0;
10:
11:! for(n = 1; n <= 200; n = n + 1)
12:! ! triangularNumber +=n;
13:
14:! NSLog(@"The 200th triangular number is %i", triangularNumber);
15:
16:! [pool drain];
17:! return 0;
18: }


   The 200th triangular number is 20100



 Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University           Object-Oriented Programming Language

วันจันทร์ท่ี 27 กุมภาพันธ์ 12
6



                                                                          The for Statement


      for( init_expression; loop_condition; loop_expression)
         program_statement;
         11:!for(n = 1; n <= 200; n = n + 1)
         12:!! triangularNumber +=n;



                    1. The initial expression is evaluate first.
                    2. The looping condition is evaluated.
                    3. The program statement is executed.
                    4.The looping expression is evaluated.
                    5. Return to step 2.
 Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University           Object-Oriented Programming Language

วันจันทร์ท่ี 27 กุมภาพันธ์ 12
7



                                                         Table ot Triangular Number

Program 5.3
01: #import <Foundation/Foundation.h>
02:
03:
04: int main(int argc, const char * argv[])
05: {
06:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
07:! int n, triangularNumber;
08:
09:! NSLog(@"TABLE OF TRIANGULAR NUMBERS");
10:! NSLog(@" n Sum from 1 to n");
11:! NSLog(@"-- ----------------");         for( init_expression; loop_condition; loop_expression)
12:
13:! triangularNumber = 0;
                                                  program_statement;
14:
                                                                      TABLE OF TRIANGULAR NUMBERS
15:! for(n = 1; n <= 10; ++n){                                          n Sum from 1 to n
16:! ! triangularNumber +=n;                                           -- ----------------
17:! ! NSLog(@" %i    t%i", n, triangularNumber);                      1   1
18:! }                                                                  2   3
                                                                        3   6
19:                                                                     4   10
20:! NSLog(@"The 200th triangular number is %i", triangularNumber); 5 15
21:                                                                     6   21
                                                                        7   28
22:! [pool drain];                                                      8   36
23:! return 0;                                                          9   45
24: }                                                                   10    55
                                                                                      The 200th triangular number is 55

 Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University               Object-Oriented Programming Language

วันจันทร์ท่ี 27 กุมภาพันธ์ 12
8



                                                                               Keyboard Input

Program 5.4
01: #import <Foundation/Foundation.h>
02:
03: int main(int argc, const char * argv[])
04: {
05:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
06:! int n, number, triangularNumber;
07:
08:! NSLog(@"What triangular number do you want?");
09:! scanf("%i", &number);
10:
11:! triangularNumber = 0;
12:
13:! for(n = 1; n <= number; ++n){
14:! ! triangularNumber +=n;
15:! }
16:
17:! NSLog(@"Triangular number %i is %in", number, triangularNumber);
18:
19:! [pool drain];
20:! return 0;
21: }                                What triangular number do you want?
22:                                  100
                                     Triangular number 100 is 5050


 Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University             Object-Oriented Programming Language

วันจันทร์ท่ี 27 กุมภาพันธ์ 12
9



                                                                            Nested for Loops

Program 5.5
05: #import <Foundation/Foundation.h>
05:
05: int main(int argc, const char * argv[])
05: {
05:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
05:! int n, number, triangularNumber, counter;
05:
05:! for(counter = 1; counter <= 5; ++counter){
05:! ! NSLog(@"What triangular number do you want?");
05:! ! scanf("%i", &number);
05:
05:! ! triangularNumber = 0;
05:
05:! ! for(n = 1; n <= number; ++n){
05:! ! ! triangularNumber +=n;
05:! ! }
05:
05:! ! NSLog(@"Triangular number %i is %i", number, triangularNumber);
05: ! }
05:! [pool drain];
05:! return 0;
05: }



 Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University            Object-Oriented Programming Language

วันจันทร์ท่ี 27 กุมภาพันธ์ 12
10



                                                                     The while Statement


                 while( expression )
                   program_statement


                 init_expression;
                 while( loop_condition )
                 {
                     program_statement;
                     loop_expression;
                 }

 Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University        Object-Oriented Programming Language

วันจันทร์ท่ี 27 กุมภาพันธ์ 12
11



                                                                      The while Statement

Program 5.6
01: #import <Foundation/Foundation.h>
02:
03: int main(int argc, const char * argv[])
04: {
05:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
06:! int count = 1;
07:
08:! while( count <= 5){                  init_expression;
09:! ! NSLog(@"%i", count);               while( loop_condition )
10:! ! ++count;
11:! }                                    {
12:                                           program_statement;
13:! [pool drain];                            loop_expression;
14:! return 0;                            }
15: }
16:!

    1
    2
    3
    4
    5


 Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University         Object-Oriented Programming Language

วันจันทร์ท่ี 27 กุมภาพันธ์ 12
12



                                             Find the greatest common divisor

Program 5.7
01: #import <Foundation/Foundation.h>
02:
03: int main(int argc, const char * argv[])
04: {
05:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
06:! unsigned int u, v, temp;
                                                                                      30 18
07:
08:! NSLog(@"Please type in two nonnegative integers.");
09:! scanf("%u%u", &u, &v);
10:                                                                                   30 /18 = 1R12
11:! while( v != 0){
12:! ! temp = u % v;                                                                  18 /12 = 1R6
                                                                                      12 / 6 = 2R0
13:! ! u = v;
14:! ! v = temp;
15:! }
16:
17:! NSLog(@"Their greatest common advisor is %u", u);
18:
19:! [pool drain];
20:! return 0;            Please type in two nonnegative integers.
21: }                     30 18
22:                       Their greatest common advisor is 6
!


 Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University           Object-Oriented Programming Language

วันจันทร์ท่ี 27 กุมภาพันธ์ 12
13



                                  Program to reverse the digits of number

Program 5.8
01: #import <Foundation/Foundation.h>
01:
01: int main(int argc, const char * argv[])
01:{
01:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
01:! int number, right_digit;
01:
01:! NSLog(@"Enter your number.");

                                                                                      1234 % 10 = 4
01:! scanf("%i", &number);
01:
01:! while( number != 0){
01:! ! right_digit = number % 10;
01:! ! NSLog(@"%i", right_digit);
                                                                                      1234 / 10 = 123
01:! ! number /= 10;
01:! }
01:
01:! [pool drain];                                                                    123 % 10 = 3
01:! return 0;
01:}    Enter your number.
                                                                                      123 /10 = 12
                                                                                              :
01:     1234
!       4
                  3
                  2
                  1

 Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                  Object-Oriented Programming Language

วันจันทร์ท่ี 27 กุมภาพันธ์ 12
14



                                                                          The do Statement


                 do
                   program_statement
                 while( expression )


                 init_expression;
                 do {
                     program_statement;
                     loop_expression;
                 } while( loop_condition )

 Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University          Object-Oriented Programming Language

วันจันทร์ท่ี 27 กุมภาพันธ์ 12
15



                                  Program to reverse the digits of number

Program 5.9
01: #import <Foundation/Foundation.h>
02:
03: int main(int argc, const char * argv[])
04: {
05:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
06:! int number, right_digit;
07:
08:! NSLog(@"Enter your number.");
09:! scanf("%i", &number);
10:
11:! do{                                               init_expression;
12:! ! right_digit = number % 10;                      do{
13:! ! NSLog(@"%i", right_digit);
14:! ! number /= 10;                                       program_statement;
15:! }while( number != 0);                                 loop_expression;
16:                                                    } while( loop_condition        );
17:! [pool drain];
18:! return 0;
19: }
01:!




 Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University   Object-Oriented Programming Language

วันจันทร์ท่ี 27 กุมภาพันธ์ 12
16



                                         The break and continue Statements


              •        Labeled break statement

                     •          Exit from nested control structures

                     •          Proceeds to end of specified labeled block

              •        Labeled continue statement

                     •          Skips remaining statements in nested-loop body

                     •          Proceeds to beginning of specified labeled block




 Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University   Object-Oriented Programming Language

วันจันทร์ท่ี 27 กุมภาพันธ์ 12

More Related Content

Similar to OOP Chapter 5 : Program Looping

Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
Nisha Soms
 
Ada lab manual
Ada lab manualAda lab manual
Ada lab manual
aman713418
 
Computer Programming Chapter 6 : Array and ArrayList
Computer Programming Chapter 6 : Array and ArrayListComputer Programming Chapter 6 : Array and ArrayList
Computer Programming Chapter 6 : Array and ArrayList
Atit Patumvan
 
Operating system labs
Operating system labsOperating system labs
Operating system labs
bhaktisagar4
 
OOP Chapter 4: Data Type and Expressions
OOP Chapter 4: Data Type and ExpressionsOOP Chapter 4: Data Type and Expressions
OOP Chapter 4: Data Type and Expressions
Atit Patumvan
 
Complete Lab 123456789123456789123456789
Complete Lab 123456789123456789123456789Complete Lab 123456789123456789123456789
Complete Lab 123456789123456789123456789
vickyvikas51556
 
Lab program 1234567891234567891234567891
Lab program 1234567891234567891234567891Lab program 1234567891234567891234567891
Lab program 1234567891234567891234567891
akashpunarvi2005
 
lab.123456789123456789123456789123456789
lab.123456789123456789123456789123456789lab.123456789123456789123456789123456789
lab.123456789123456789123456789123456789
Ghh
 
Data Structures and Algorithm - Week 11 - Algorithm Analysis
Data Structures and Algorithm - Week 11 - Algorithm AnalysisData Structures and Algorithm - Week 11 - Algorithm Analysis
Data Structures and Algorithm - Week 11 - Algorithm Analysis
Ferdin Joe John Joseph PhD
 
Algorithms overview
Algorithms overviewAlgorithms overview
Algorithms overview
Deborah Akuoko
 
Arrays in c
Arrays in cArrays in c
SIGDOC 2011 - Necessary and Neglected? An Empirical Study of Internal Documen...
SIGDOC 2011 - Necessary and Neglected? An Empirical Study of Internal Documen...SIGDOC 2011 - Necessary and Neglected? An Empirical Study of Internal Documen...
SIGDOC 2011 - Necessary and Neglected? An Empirical Study of Internal Documen...
Christoph Johann Stettina
 

Similar to OOP Chapter 5 : Program Looping (12)

Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
 
Ada lab manual
Ada lab manualAda lab manual
Ada lab manual
 
Computer Programming Chapter 6 : Array and ArrayList
Computer Programming Chapter 6 : Array and ArrayListComputer Programming Chapter 6 : Array and ArrayList
Computer Programming Chapter 6 : Array and ArrayList
 
Operating system labs
Operating system labsOperating system labs
Operating system labs
 
OOP Chapter 4: Data Type and Expressions
OOP Chapter 4: Data Type and ExpressionsOOP Chapter 4: Data Type and Expressions
OOP Chapter 4: Data Type and Expressions
 
Complete Lab 123456789123456789123456789
Complete Lab 123456789123456789123456789Complete Lab 123456789123456789123456789
Complete Lab 123456789123456789123456789
 
Lab program 1234567891234567891234567891
Lab program 1234567891234567891234567891Lab program 1234567891234567891234567891
Lab program 1234567891234567891234567891
 
lab.123456789123456789123456789123456789
lab.123456789123456789123456789123456789lab.123456789123456789123456789123456789
lab.123456789123456789123456789123456789
 
Data Structures and Algorithm - Week 11 - Algorithm Analysis
Data Structures and Algorithm - Week 11 - Algorithm AnalysisData Structures and Algorithm - Week 11 - Algorithm Analysis
Data Structures and Algorithm - Week 11 - Algorithm Analysis
 
Algorithms overview
Algorithms overviewAlgorithms overview
Algorithms overview
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
SIGDOC 2011 - Necessary and Neglected? An Empirical Study of Internal Documen...
SIGDOC 2011 - Necessary and Neglected? An Empirical Study of Internal Documen...SIGDOC 2011 - Necessary and Neglected? An Empirical Study of Internal Documen...
SIGDOC 2011 - Necessary and Neglected? An Empirical Study of Internal Documen...
 

More from Atit Patumvan

Iot for smart agriculture
Iot for smart agricultureIot for smart agriculture
Iot for smart agriculture
Atit Patumvan
 
An Overview of eZee Burrp! (Philus Limited)
An Overview of eZee Burrp! (Philus Limited)An Overview of eZee Burrp! (Philus Limited)
An Overview of eZee Burrp! (Philus Limited)
Atit Patumvan
 
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ตแบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
Atit Patumvan
 
Chapter 1 mathmatics tools
Chapter 1 mathmatics toolsChapter 1 mathmatics tools
Chapter 1 mathmatics tools
Atit Patumvan
 
Chapter 1 mathmatics tools
Chapter 1 mathmatics toolsChapter 1 mathmatics tools
Chapter 1 mathmatics toolsAtit Patumvan
 
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
Atit Patumvan
 
Chapter 0 introduction to theory of computation
Chapter 0 introduction to theory of computationChapter 0 introduction to theory of computation
Chapter 0 introduction to theory of computation
Atit Patumvan
 
Media literacy
Media literacyMedia literacy
Media literacy
Atit Patumvan
 
Chapter 01 mathmatics tools (slide)
Chapter 01 mathmatics tools (slide)Chapter 01 mathmatics tools (slide)
Chapter 01 mathmatics tools (slide)
Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 8
การบริหารเชิงคุณภาพ ชุดที่ 8การบริหารเชิงคุณภาพ ชุดที่ 8
การบริหารเชิงคุณภาพ ชุดที่ 8
Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 7
การบริหารเชิงคุณภาพ ชุดที่ 7การบริหารเชิงคุณภาพ ชุดที่ 7
การบริหารเชิงคุณภาพ ชุดที่ 7
Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 6
การบริหารเชิงคุณภาพ ชุดที่ 6การบริหารเชิงคุณภาพ ชุดที่ 6
การบริหารเชิงคุณภาพ ชุดที่ 6
Atit Patumvan
 
Computer Programming Chapter 5 : Methods
Computer Programming Chapter 5 : MethodsComputer Programming Chapter 5 : Methods
Computer Programming Chapter 5 : MethodsAtit Patumvan
 
Computer Programming Chapter 4 : Loops
Computer Programming Chapter 4 : Loops Computer Programming Chapter 4 : Loops
Computer Programming Chapter 4 : Loops Atit Patumvan
 
Introduction to Java EE (J2EE)
Introduction to Java EE (J2EE)Introduction to Java EE (J2EE)
Introduction to Java EE (J2EE)
Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 5
การบริหารเชิงคุณภาพ ชุดที่ 5การบริหารเชิงคุณภาพ ชุดที่ 5
การบริหารเชิงคุณภาพ ชุดที่ 5
Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 4
การบริหารเชิงคุณภาพ ชุดที่ 4การบริหารเชิงคุณภาพ ชุดที่ 4
การบริหารเชิงคุณภาพ ชุดที่ 4
Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 3
การบริหารเชิงคุณภาพ ชุดที่ 3การบริหารเชิงคุณภาพ ชุดที่ 3
การบริหารเชิงคุณภาพ ชุดที่ 3
Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 2
การบริหารเชิงคุณภาพ ชุดที่ 2การบริหารเชิงคุณภาพ ชุดที่ 2
การบริหารเชิงคุณภาพ ชุดที่ 2
Atit Patumvan
 
Computer Programming: Chapter 1
Computer Programming: Chapter 1Computer Programming: Chapter 1
Computer Programming: Chapter 1
Atit Patumvan
 

More from Atit Patumvan (20)

Iot for smart agriculture
Iot for smart agricultureIot for smart agriculture
Iot for smart agriculture
 
An Overview of eZee Burrp! (Philus Limited)
An Overview of eZee Burrp! (Philus Limited)An Overview of eZee Burrp! (Philus Limited)
An Overview of eZee Burrp! (Philus Limited)
 
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ตแบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
 
Chapter 1 mathmatics tools
Chapter 1 mathmatics toolsChapter 1 mathmatics tools
Chapter 1 mathmatics tools
 
Chapter 1 mathmatics tools
Chapter 1 mathmatics toolsChapter 1 mathmatics tools
Chapter 1 mathmatics tools
 
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
 
Chapter 0 introduction to theory of computation
Chapter 0 introduction to theory of computationChapter 0 introduction to theory of computation
Chapter 0 introduction to theory of computation
 
Media literacy
Media literacyMedia literacy
Media literacy
 
Chapter 01 mathmatics tools (slide)
Chapter 01 mathmatics tools (slide)Chapter 01 mathmatics tools (slide)
Chapter 01 mathmatics tools (slide)
 
การบริหารเชิงคุณภาพ ชุดที่ 8
การบริหารเชิงคุณภาพ ชุดที่ 8การบริหารเชิงคุณภาพ ชุดที่ 8
การบริหารเชิงคุณภาพ ชุดที่ 8
 
การบริหารเชิงคุณภาพ ชุดที่ 7
การบริหารเชิงคุณภาพ ชุดที่ 7การบริหารเชิงคุณภาพ ชุดที่ 7
การบริหารเชิงคุณภาพ ชุดที่ 7
 
การบริหารเชิงคุณภาพ ชุดที่ 6
การบริหารเชิงคุณภาพ ชุดที่ 6การบริหารเชิงคุณภาพ ชุดที่ 6
การบริหารเชิงคุณภาพ ชุดที่ 6
 
Computer Programming Chapter 5 : Methods
Computer Programming Chapter 5 : MethodsComputer Programming Chapter 5 : Methods
Computer Programming Chapter 5 : Methods
 
Computer Programming Chapter 4 : Loops
Computer Programming Chapter 4 : Loops Computer Programming Chapter 4 : Loops
Computer Programming Chapter 4 : Loops
 
Introduction to Java EE (J2EE)
Introduction to Java EE (J2EE)Introduction to Java EE (J2EE)
Introduction to Java EE (J2EE)
 
การบริหารเชิงคุณภาพ ชุดที่ 5
การบริหารเชิงคุณภาพ ชุดที่ 5การบริหารเชิงคุณภาพ ชุดที่ 5
การบริหารเชิงคุณภาพ ชุดที่ 5
 
การบริหารเชิงคุณภาพ ชุดที่ 4
การบริหารเชิงคุณภาพ ชุดที่ 4การบริหารเชิงคุณภาพ ชุดที่ 4
การบริหารเชิงคุณภาพ ชุดที่ 4
 
การบริหารเชิงคุณภาพ ชุดที่ 3
การบริหารเชิงคุณภาพ ชุดที่ 3การบริหารเชิงคุณภาพ ชุดที่ 3
การบริหารเชิงคุณภาพ ชุดที่ 3
 
การบริหารเชิงคุณภาพ ชุดที่ 2
การบริหารเชิงคุณภาพ ชุดที่ 2การบริหารเชิงคุณภาพ ชุดที่ 2
การบริหารเชิงคุณภาพ ชุดที่ 2
 
Computer Programming: Chapter 1
Computer Programming: Chapter 1Computer Programming: Chapter 1
Computer Programming: Chapter 1
 

Recently uploaded

Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
ImMuslim
 
Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47
MysoreMuleSoftMeetup
 
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
EduSkills OECD
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
iammrhaywood
 
Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
Iris Thiele Isip-Tan
 
SWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptxSWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptx
zuzanka
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
JomonJoseph58
 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Henry Hollis
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
Himanshu Rai
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)
nitinpv4ai
 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
MJDuyan
 
HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.
deepaannamalai16
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025
khuleseema60
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
Nguyen Thanh Tu Collection
 
Nutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour TrainingNutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour Training
melliereed
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
 
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
National Information Standards Organization (NISO)
 
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptxRESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
zuzanka
 

Recently uploaded (20)

Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
Geography as a Discipline Chapter 1 __ Class 11 Geography NCERT _ Class Notes...
 
Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47
 
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
Andreas Schleicher presents PISA 2022 Volume III - Creative Thinking - 18 Jun...
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
 
Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
 
SWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptxSWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptx
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
 
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.pptLevel 3 NCEA - NZ: A  Nation In the Making 1872 - 1900 SML.ppt
Level 3 NCEA - NZ: A Nation In the Making 1872 - 1900 SML.ppt
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)
 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
 
HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025MDP on air pollution of class 8 year 2024-2025
MDP on air pollution of class 8 year 2024-2025
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
 
Nutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour TrainingNutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour Training
 
Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
 
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
Jemison, MacLaughlin, and Majumder "Broadening Pathways for Editors and Authors"
 
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptxRESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
 

OOP Chapter 5 : Program Looping

  • 1. Object-Oriented Programming Language Chapter 5 : Program Looping Atit Patumvan Faculty of Management and Information Sciences Naresuan University วันจันทร์ท่ี 27 กุมภาพันธ์ 12
  • 2. 2 Contents • The for statement • The while statement • The do statement • The break and continue Statements Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language วันจันทร์ท่ี 27 กุมภาพันธ์ 12
  • 3. 3 Triangular Number n=4 sum = 1 + 2 + ... + n Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language วันจันทร์ท่ี 27 กุมภาพันธ์ 12
  • 4. 4 Triangular Number Example Program 5.1 01: #import <Foundation/Foundation.h> 02: 03:// Program to calculate the eighth triangular number 04: 05: int main(int argc, const char * argv[]) 06:{ 07:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 08:! int triangularNumber; 09: 10:! triangularNumber = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8; 11: 12:! NSLog(@"The eighth triangular number is %i", triangularNumber); 13: 14:! [pool drain]; 15:! return 0; 16:} The eighth triangular number is 36 Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language วันจันทร์ท่ี 27 กุมภาพันธ์ 12
  • 5. 5 The for Statement Program 5.2 01: #import <Foundation/Foundation.h> 02: 03: 04: int main(int argc, const char * argv[]) 05: { 06:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 07:! int n, triangularNumber; 08: 09:! triangularNumber = 0; 10: 11:! for(n = 1; n <= 200; n = n + 1) 12:! ! triangularNumber +=n; 13: 14:! NSLog(@"The 200th triangular number is %i", triangularNumber); 15: 16:! [pool drain]; 17:! return 0; 18: } The 200th triangular number is 20100 Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language วันจันทร์ท่ี 27 กุมภาพันธ์ 12
  • 6. 6 The for Statement for( init_expression; loop_condition; loop_expression) program_statement; 11:!for(n = 1; n <= 200; n = n + 1) 12:!! triangularNumber +=n; 1. The initial expression is evaluate first. 2. The looping condition is evaluated. 3. The program statement is executed. 4.The looping expression is evaluated. 5. Return to step 2. Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language วันจันทร์ท่ี 27 กุมภาพันธ์ 12
  • 7. 7 Table ot Triangular Number Program 5.3 01: #import <Foundation/Foundation.h> 02: 03: 04: int main(int argc, const char * argv[]) 05: { 06:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 07:! int n, triangularNumber; 08: 09:! NSLog(@"TABLE OF TRIANGULAR NUMBERS"); 10:! NSLog(@" n Sum from 1 to n"); 11:! NSLog(@"-- ----------------"); for( init_expression; loop_condition; loop_expression) 12: 13:! triangularNumber = 0; program_statement; 14: TABLE OF TRIANGULAR NUMBERS 15:! for(n = 1; n <= 10; ++n){ n Sum from 1 to n 16:! ! triangularNumber +=n; -- ---------------- 17:! ! NSLog(@" %i t%i", n, triangularNumber); 1 1 18:! } 2 3 3 6 19: 4 10 20:! NSLog(@"The 200th triangular number is %i", triangularNumber); 5 15 21: 6 21 7 28 22:! [pool drain]; 8 36 23:! return 0; 9 45 24: } 10 55 The 200th triangular number is 55 Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language วันจันทร์ท่ี 27 กุมภาพันธ์ 12
  • 8. 8 Keyboard Input Program 5.4 01: #import <Foundation/Foundation.h> 02: 03: int main(int argc, const char * argv[]) 04: { 05:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 06:! int n, number, triangularNumber; 07: 08:! NSLog(@"What triangular number do you want?"); 09:! scanf("%i", &number); 10: 11:! triangularNumber = 0; 12: 13:! for(n = 1; n <= number; ++n){ 14:! ! triangularNumber +=n; 15:! } 16: 17:! NSLog(@"Triangular number %i is %in", number, triangularNumber); 18: 19:! [pool drain]; 20:! return 0; 21: } What triangular number do you want? 22: 100 Triangular number 100 is 5050 Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language วันจันทร์ท่ี 27 กุมภาพันธ์ 12
  • 9. 9 Nested for Loops Program 5.5 05: #import <Foundation/Foundation.h> 05: 05: int main(int argc, const char * argv[]) 05: { 05:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 05:! int n, number, triangularNumber, counter; 05: 05:! for(counter = 1; counter <= 5; ++counter){ 05:! ! NSLog(@"What triangular number do you want?"); 05:! ! scanf("%i", &number); 05: 05:! ! triangularNumber = 0; 05: 05:! ! for(n = 1; n <= number; ++n){ 05:! ! ! triangularNumber +=n; 05:! ! } 05: 05:! ! NSLog(@"Triangular number %i is %i", number, triangularNumber); 05: ! } 05:! [pool drain]; 05:! return 0; 05: } Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language วันจันทร์ท่ี 27 กุมภาพันธ์ 12
  • 10. 10 The while Statement while( expression ) program_statement init_expression; while( loop_condition ) { program_statement; loop_expression; } Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language วันจันทร์ท่ี 27 กุมภาพันธ์ 12
  • 11. 11 The while Statement Program 5.6 01: #import <Foundation/Foundation.h> 02: 03: int main(int argc, const char * argv[]) 04: { 05:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 06:! int count = 1; 07: 08:! while( count <= 5){ init_expression; 09:! ! NSLog(@"%i", count); while( loop_condition ) 10:! ! ++count; 11:! } { 12: program_statement; 13:! [pool drain]; loop_expression; 14:! return 0; } 15: } 16:! 1 2 3 4 5 Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language วันจันทร์ท่ี 27 กุมภาพันธ์ 12
  • 12. 12 Find the greatest common divisor Program 5.7 01: #import <Foundation/Foundation.h> 02: 03: int main(int argc, const char * argv[]) 04: { 05:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 06:! unsigned int u, v, temp; 30 18 07: 08:! NSLog(@"Please type in two nonnegative integers."); 09:! scanf("%u%u", &u, &v); 10: 30 /18 = 1R12 11:! while( v != 0){ 12:! ! temp = u % v; 18 /12 = 1R6 12 / 6 = 2R0 13:! ! u = v; 14:! ! v = temp; 15:! } 16: 17:! NSLog(@"Their greatest common advisor is %u", u); 18: 19:! [pool drain]; 20:! return 0; Please type in two nonnegative integers. 21: } 30 18 22: Their greatest common advisor is 6 ! Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language วันจันทร์ท่ี 27 กุมภาพันธ์ 12
  • 13. 13 Program to reverse the digits of number Program 5.8 01: #import <Foundation/Foundation.h> 01: 01: int main(int argc, const char * argv[]) 01:{ 01:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 01:! int number, right_digit; 01: 01:! NSLog(@"Enter your number."); 1234 % 10 = 4 01:! scanf("%i", &number); 01: 01:! while( number != 0){ 01:! ! right_digit = number % 10; 01:! ! NSLog(@"%i", right_digit); 1234 / 10 = 123 01:! ! number /= 10; 01:! } 01: 01:! [pool drain]; 123 % 10 = 3 01:! return 0; 01:} Enter your number. 123 /10 = 12 : 01: 1234 ! 4 3 2 1 Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language วันจันทร์ท่ี 27 กุมภาพันธ์ 12
  • 14. 14 The do Statement do program_statement while( expression ) init_expression; do { program_statement; loop_expression; } while( loop_condition ) Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language วันจันทร์ท่ี 27 กุมภาพันธ์ 12
  • 15. 15 Program to reverse the digits of number Program 5.9 01: #import <Foundation/Foundation.h> 02: 03: int main(int argc, const char * argv[]) 04: { 05:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 06:! int number, right_digit; 07: 08:! NSLog(@"Enter your number."); 09:! scanf("%i", &number); 10: 11:! do{ init_expression; 12:! ! right_digit = number % 10; do{ 13:! ! NSLog(@"%i", right_digit); 14:! ! number /= 10; program_statement; 15:! }while( number != 0); loop_expression; 16: } while( loop_condition ); 17:! [pool drain]; 18:! return 0; 19: } 01:! Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language วันจันทร์ท่ี 27 กุมภาพันธ์ 12
  • 16. 16 The break and continue Statements • Labeled break statement • Exit from nested control structures • Proceeds to end of specified labeled block • Labeled continue statement • Skips remaining statements in nested-loop body • Proceeds to beginning of specified labeled block Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language วันจันทร์ท่ี 27 กุมภาพันธ์ 12