SlideShare ist ein Scribd-Unternehmen logo
1 von 41
Downloaden Sie, um offline zu lesen
Chapter 3
C Language Control
                              Mr.Warawut Khangkhan
                 Twitter: http://twitter.com/awarawut
      Facebook: http://www.facebook.com/AjWarawut
                       E-Mail: awarawut@hotmail.com
                                 Mobile: 083-0698-410
Contents
 Decision / Selection
 Loop / Repetition
 Break and Continue Statement




             Mr.Warawut Khangkhan   Chapter 3 C Language Control   2
DECISION / SELECTION




    Mr.Warawut Khangkhan   Chapter 3 C Language Control   3
Decision / Selection
 if then
 if then else
 Nested if
 switch




                Mr.Warawut Khangkhan   Chapter 3 C Language Control   4
if then
Format:
 if (expression)
     statement;




               Mr.Warawut Khangkhan   Chapter 3 C Language Control   5
if then
Format:
 if (expression) {
     statement-1;
     statement-2;
     …
 }




               Mr.Warawut Khangkhan   Chapter 3 C Language Control   6
Example if1.c
#include <stdio.h>
#include <stdlib.h>
int main( ) {
     int i;

     scanf(“%d”, &i);
     if (i > 0)
           printf(“a positive number was enteredn”);
     system(“PAUSE”);
     return 0;
}
                      Mr.Warawut Khangkhan   Chapter 3 C Language Control   7
Example if2.c
#include <stdio.h>
#include <stdlib.h>
int main( ) {
     int i;

     scanf(“%d”, &i);
     if (i < 0) {
           printf(“a negative number was enteredn”);
           i = -i;
     }
     system(“PAUSE”);
     return 0;
}                     Mr.Warawut Khangkhan   Chapter 3 C Language Control   8
if then else




               Mr.Warawut Khangkhan   Chapter 3 C Language Control   9
if then else
Format:
 if (expression)
     statement-true;
 else
     statement-false;




                Mr.Warawut Khangkhan   Chapter 3 C Language Control   10
if then else




               Mr.Warawut Khangkhan   Chapter 3 C Language Control   11
if then else
Format:
 if (expression) {
     statement-1;
     statement-2;
     …
 } else {
     statement-3;
     statement-4;
     …
 }

               Mr.Warawut Khangkhan   Chapter 3 C Language Control   12
Example ifthen1.c
#include <stdio.h>
#include <stdlib.h>
int main( ) {
     int i;
     scanf(“%d”, &i);
     if (i > 0)
           printf(“a negative number was enteredn”);
     else
           printf(“a negative number was enteredn”);
     system(“PAUSE”);
     return 0;
}                     Mr.Warawut Khangkhan   Chapter 3 C Language Control   13
Nested if




            Mr.Warawut Khangkhan   Chapter 3 C Language Control   14
Nested if
Format:
 if (expression-1)
     statement-1;
 else if (expression-2)
     statement-2;
 else if (expression-3)
     statement-3;
 else
     statement-4;

               Mr.Warawut Khangkhan   Chapter 3 C Language Control   15
Example nestedif.c
#include <stdio.h>
#include <stdlib.h>
int main( ) {
     int score;
     printf(“Enter score : “);
     scanf(“%d”, &score);
     if (score > 79)
           printf(“Very Goodn”);
     else if (score > 49)
           printf(“Goodn”);
     else
           printf(“Badn”);
}
                      Mr.Warawut Khangkhan   Chapter 3 C Language Control   16
switch




         Mr.Warawut Khangkhan   Chapter 3 C Language Control   17
switch
Format:
 switch (expression) {
   case const-expr.1: statement-1; break;
   case const-expr.2: statement-2; break;
   case const-expr.3: statement-3; break;
   …
   [default: statement-default;]
 }


              Mr.Warawut Khangkhan   Chapter 3 C Language Control   18
Example switch1.c
#include <stdio.h>
#include <stdlib.h>
int main( ) {
     char ch;
     printf(“Enter char : “);
     ch = getchar( );
     switch (ch) {
         case 'a': case 'A': printf(“Additionn”); break;
         case 's': case 'S': printf(“Subtractn”); break;
         case 'q': case 'Q': printf(“Quitn”); break;
         default: printf(“Other Charactern”);
     }
     system(“PAUSE”);
     return 0;
}                        Mr.Warawut Khangkhan   Chapter 3 C Language Control   19
Example switch2.c
#include <stdio.h>
#include <stdlib.h>
int main( ) {
     int n = ?;
     swicth (n) {
         case 2: n++;
         case 4: n++; break;
         case 6: n++; break;
         case 8: n++;
         default: n++;
     }
     printf(“N = %dn”, n);

     system(“PAUSE”);
     return 0;
}                       Mr.Warawut Khangkhan   Chapter 3 C Language Control   20
LOOP / REPETITION




    Mr.Warawut Khangkhan   Chapter 3 C Language Control   21
Loop / Repetition
 while loop
 do ... while loop
 for loop




              Mr.Warawut Khangkhan   Chapter 3 C Language Control   22
while loop
Format:
 while (expression)
   statement;




              Mr.Warawut Khangkhan   Chapter 3 C Language Control   23
while loop
Format:
 while (expression) {
   statement-1;
   statement-2;
   …
 }




              Mr.Warawut Khangkhan   Chapter 3 C Language Control   24
Example while1.c
#include <stdio.h>         while (num <= 10) {
#include <stdlib.h>          printf(“%dn”, num);
                             num++;
int main( ) {              }
    int num = 1;
    while (num <= 10)
      printf(“%dn”, num++);
    system(“PAUSE”);
    return 0;
}

               Mr.Warawut Khangkhan   Chapter 3 C Language Control   25
Example while2.c
#include <stdio.h>
#include <stdlib.h>

int main( ) {
     int num = 1;
     int sum = 0;

     while (num <= 10) {
         sum += num; // sum = sum + num;
         num++; // num = num + 1;
     }

     printf(“Summary of 1-10 = %dn”, sum);

     system(“PAUSE”);
     return 0;
}                       Mr.Warawut Khangkhan   Chapter 3 C Language Control   26
do ... while loop
Format:
 do
   statement;
 while (expression);




              Mr.Warawut Khangkhan   Chapter 3 C Language Control   27
do ... while loop
Format:
 do {
   statement-1;
   statement-2;
   …
 } while (expression);




              Mr.Warawut Khangkhan   Chapter 3 C Language Control   28
Example dowhile1.c
#include <stdio.h>             do {
#include <stdlib.h>              printf(“%dn”, num);
int main( ) {                    num++;
    int num = 1;               } while (num <= 10);
    do
      printf(“%dn”, num++);
    while (num <= 10);
    system(“PAUSE”);
    return 0;
}

                   Mr.Warawut Khangkhan   Chapter 3 C Language Control   29
Example dowhile2.c
#include <stdio.h>
#include <stdlib.h>

int main( ) {
     int num = 1;
     int sum = 0;

     do {
         sum += num; // sum = sum + num;
         num++; // num = num + 1;
     } while (num <= 10);

     printf(“Summary of 1-10 = %dn”, sum);

     system(“PAUSE”);
     return 0;
}                       Mr.Warawut Khangkhan   Chapter 3 C Language Control   30
for loop
Format:
 for (expr.1; expr.2; expr.3)
   statement;

or

 for (expr.1; expr.2; expr.3) {
   statement-1;
   statement-2;
   …
 }
                Mr.Warawut Khangkhan   Chapter 3 C Language Control   31
Example for1.c
#include <stdio.h> for (num = 1; num <= 10; ) {
#include <stdlib.h> printf(“%dn”, num);
                             num++;
int main( ) {           }
    int num;
    for (num = 1; num <= 10; num++)
       printf(“%dn”, num);
    system(“PAUSE”);
    return 0;
}

                Mr.Warawut Khangkhan   Chapter 3 C Language Control   32
Example for2.c
#include <stdio.h>
#include <stdlib.h>

int main( ) {
    int num = 1;

    for ( ; num <= 10; ) {
        printf(“%dn”, num;
        num++;
    }

    system(“PAUSE”);
    return 0;
}
                      Mr.Warawut Khangkhan   Chapter 3 C Language Control   33
Example for3.c
#include <stdio.h>
#include <stdlib.h>

int main( ) {
     int num;
     int sum = 0;

     for (num = 1; num <= 10; num++) {
          sum += num; // sum = sum + num;
     }

     printf(“Summary of 1-10 = %dn”, sum);

     system(“PAUSE”);
     return 0;
}
                        Mr.Warawut Khangkhan   Chapter 3 C Language Control   34
Change with for loop to while loop
for (expr.1; expr.2; expr.3)
     statement;
   1                             2

             expr.1;                                                4
        3
             while (expr.2) {
                  statement;
                  expr.3;
             }

              Mr.Warawut Khangkhan   Chapter 3 C Language Control       35
Change with for loop to do .. while
loop
for (expr.1; expr.2; expr.3)
     statement;
   1                             2

             expr.1;                                                4
        3
             do {
                   statement;
                   expr.3;
             } while (expr.2);

              Mr.Warawut Khangkhan   Chapter 3 C Language Control       36
Change with while loop to
do .. while loop
expr.1;
while (expr.2) { 1
    statement;                  2
    expr.3;
 }
             3 expr.1;
                do {
    4
                      statement;
                      expr.3;
                } while (expr.2);
             Mr.Warawut Khangkhan   Chapter 3 C Language Control   37
BREAK AND CONTINUE
STATEMENT




    Mr.Warawut Khangkhan   Chapter 3 C Language Control   38
Break and Continue Statement
break statement
     FF                                                              F

           F
continue statement
  F F        while, do..while                             for            F
                             F
           F


               Mr.Warawut Khangkhan   Chapter 3 C Language Control       39
Example break.c
#include <stdio.h>
#include <stdlib.h>

int main( ) {
    int num;
    for (num = 1; num <= 10; num++)
        if (num == 5)
             break;
        else
             printf(“%d”, num);
    system(“PAUSE”);
    return 0;
}
                      Mr.Warawut Khangkhan   Chapter 3 C Language Control   40
Example continue.c
#include <stdio.h>
#include <stdlib.h>

int main( ) {
    int num;
    for (num = 1; num <= 10; num++)
        if (num == 5)
             continue;
        else
             printf(“%d”, num);
    system(“PAUSE”);
    return 0;
}
                      Mr.Warawut Khangkhan   Chapter 3 C Language Control   41

Weitere ähnliche Inhalte

Was ist angesagt? (10)

Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder Nesting of if else statement & Else If Ladder
Nesting of if else statement & Else If Ladder
 
Aosd2009 adams
Aosd2009 adamsAosd2009 adams
Aosd2009 adams
 
Lecture04(control structure part i)
Lecture04(control structure part i)Lecture04(control structure part i)
Lecture04(control structure part i)
 
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KRLoops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
 
Lecture 10 - Control Structures 2
Lecture 10 - Control Structures 2Lecture 10 - Control Structures 2
Lecture 10 - Control Structures 2
 
C Constructs (C Statements & Loop)
C Constructs (C Statements & Loop)C Constructs (C Statements & Loop)
C Constructs (C Statements & Loop)
 
Matlab dsp examples
Matlab dsp examplesMatlab dsp examples
Matlab dsp examples
 
Lecture 12 - Recursion
Lecture 12 - Recursion Lecture 12 - Recursion
Lecture 12 - Recursion
 
Ch2
Ch2Ch2
Ch2
 
basic of desicion control statement in python
basic of  desicion control statement in pythonbasic of  desicion control statement in python
basic of desicion control statement in python
 

Andere mochten auch

เอกสารประกอบการบรรยาย
เอกสารประกอบการบรรยายเอกสารประกอบการบรรยาย
เอกสารประกอบการบรรยายWarawut
 
Database Design
Database DesignDatabase Design
Database DesignWarawut
 
Structure Statement VB.NET 2005
Structure Statement VB.NET 2005Structure Statement VB.NET 2005
Structure Statement VB.NET 2005Warawut
 
Connect MySQL
Connect MySQLConnect MySQL
Connect MySQLWarawut
 
ตัวอย่างการเขียนโปรแกรม โดยใช้ฟังก์ชัน
ตัวอย่างการเขียนโปรแกรม โดยใช้ฟังก์ชันตัวอย่างการเขียนโปรแกรม โดยใช้ฟังก์ชัน
ตัวอย่างการเขียนโปรแกรม โดยใช้ฟังก์ชันWarawut
 
04 connect-db-tools
 04 connect-db-tools 04 connect-db-tools
04 connect-db-toolsWarawut
 
Additional Information
Additional InformationAdditional Information
Additional InformationWarawut
 
Search Data
Search DataSearch Data
Search DataWarawut
 
Chapter 2 Strategy & Information System
Chapter 2 Strategy & Information SystemChapter 2 Strategy & Information System
Chapter 2 Strategy & Information SystemWarawut
 
HTC - aplikacije, brez katerih ne gre
HTC - aplikacije, brez katerih ne greHTC - aplikacije, brez katerih ne gre
HTC - aplikacije, brez katerih ne greAljaž Maher
 
Work conditions in Spain school
Work conditions in Spain schoolWork conditions in Spain school
Work conditions in Spain schoolOLEtark
 
2006:SADC Macroeconomic Convergence Programme
2006:SADC Macroeconomic Convergence Programme2006:SADC Macroeconomic Convergence Programme
2006:SADC Macroeconomic Convergence Programmeeconsultbw
 
327- Argentina-July 9th
327- Argentina-July 9th327- Argentina-July 9th
327- Argentina-July 9thmireille 30100
 
Проведення вуличних заходів
Проведення вуличних заходівПроведення вуличних заходів
Проведення вуличних заходівMykhailo Kameniev
 
Lenguaje digital y audiovisual
Lenguaje digital y audiovisualLenguaje digital y audiovisual
Lenguaje digital y audiovisualvirginiascandura
 
programma parastaseon iounios -ioulios 2011 athina
programma parastaseon iounios -ioulios 2011 athinaprogramma parastaseon iounios -ioulios 2011 athina
programma parastaseon iounios -ioulios 2011 athinagiullari2011
 

Andere mochten auch (17)

เอกสารประกอบการบรรยาย
เอกสารประกอบการบรรยายเอกสารประกอบการบรรยาย
เอกสารประกอบการบรรยาย
 
Database Design
Database DesignDatabase Design
Database Design
 
Structure Statement VB.NET 2005
Structure Statement VB.NET 2005Structure Statement VB.NET 2005
Structure Statement VB.NET 2005
 
Connect MySQL
Connect MySQLConnect MySQL
Connect MySQL
 
ตัวอย่างการเขียนโปรแกรม โดยใช้ฟังก์ชัน
ตัวอย่างการเขียนโปรแกรม โดยใช้ฟังก์ชันตัวอย่างการเขียนโปรแกรม โดยใช้ฟังก์ชัน
ตัวอย่างการเขียนโปรแกรม โดยใช้ฟังก์ชัน
 
04 connect-db-tools
 04 connect-db-tools 04 connect-db-tools
04 connect-db-tools
 
Additional Information
Additional InformationAdditional Information
Additional Information
 
Search Data
Search DataSearch Data
Search Data
 
Chapter 2 Strategy & Information System
Chapter 2 Strategy & Information SystemChapter 2 Strategy & Information System
Chapter 2 Strategy & Information System
 
HTC - aplikacije, brez katerih ne gre
HTC - aplikacije, brez katerih ne greHTC - aplikacije, brez katerih ne gre
HTC - aplikacije, brez katerih ne gre
 
Work conditions in Spain school
Work conditions in Spain schoolWork conditions in Spain school
Work conditions in Spain school
 
2006:SADC Macroeconomic Convergence Programme
2006:SADC Macroeconomic Convergence Programme2006:SADC Macroeconomic Convergence Programme
2006:SADC Macroeconomic Convergence Programme
 
Rolf barlindhaug
Rolf barlindhaugRolf barlindhaug
Rolf barlindhaug
 
327- Argentina-July 9th
327- Argentina-July 9th327- Argentina-July 9th
327- Argentina-July 9th
 
Проведення вуличних заходів
Проведення вуличних заходівПроведення вуличних заходів
Проведення вуличних заходів
 
Lenguaje digital y audiovisual
Lenguaje digital y audiovisualLenguaje digital y audiovisual
Lenguaje digital y audiovisual
 
programma parastaseon iounios -ioulios 2011 athina
programma parastaseon iounios -ioulios 2011 athinaprogramma parastaseon iounios -ioulios 2011 athina
programma parastaseon iounios -ioulios 2011 athina
 

Ähnlich wie การควบคุมภาษา C

Ähnlich wie การควบคุมภาษา C (20)

CONTROL FLOW in C.pptx
CONTROL FLOW in C.pptxCONTROL FLOW in C.pptx
CONTROL FLOW in C.pptx
 
Control structuresin c
Control structuresin cControl structuresin c
Control structuresin c
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
 
C lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareC lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshare
 
Loop's definition and practical code in C programming
Loop's definition and  practical code in C programming Loop's definition and  practical code in C programming
Loop's definition and practical code in C programming
 
COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops
 
while loop in C.pptx
while loop in C.pptxwhile loop in C.pptx
while loop in C.pptx
 
if,loop,switch
if,loop,switchif,loop,switch
if,loop,switch
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
Session05 iteration structure
Session05 iteration structureSession05 iteration structure
Session05 iteration structure
 
Control structures in c
Control structures in cControl structures in c
Control structures in c
 
Elements of programming
Elements of programmingElements of programming
Elements of programming
 
ภาษาซี
ภาษาซีภาษาซี
ภาษาซี
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
C operators
C operatorsC operators
C operators
 
ภาษาซี
ภาษาซีภาษาซี
ภาษาซี
 
Looping
LoopingLooping
Looping
 

Mehr von Warawut

Database design
Database designDatabase design
Database designWarawut
 
Business Computer Project 4
Business Computer Project 4Business Computer Project 4
Business Computer Project 4Warawut
 
Object-Oriented Programming 10
Object-Oriented Programming 10Object-Oriented Programming 10
Object-Oriented Programming 10Warawut
 
Object-Oriented Programming 9
Object-Oriented Programming 9Object-Oriented Programming 9
Object-Oriented Programming 9Warawut
 
Object-Oriented Programming 8
Object-Oriented Programming 8Object-Oriented Programming 8
Object-Oriented Programming 8Warawut
 
Object-Oriented Programming 7
Object-Oriented Programming 7Object-Oriented Programming 7
Object-Oriented Programming 7Warawut
 
Object-Oriented Programming 6
Object-Oriented Programming 6Object-Oriented Programming 6
Object-Oriented Programming 6Warawut
 
Management Information System 6
Management Information System 6Management Information System 6
Management Information System 6Warawut
 
Management Information System 5
Management Information System 5Management Information System 5
Management Information System 5Warawut
 
Management Information System 4
Management Information System 4Management Information System 4
Management Information System 4Warawut
 
Object-Oriented Programming 5
Object-Oriented Programming 5Object-Oriented Programming 5
Object-Oriented Programming 5Warawut
 
Business Computer Project 3
Business Computer Project 3Business Computer Project 3
Business Computer Project 3Warawut
 
Management Information System 3
Management Information System 3Management Information System 3
Management Information System 3Warawut
 
Business Computer Project 2
Business Computer Project 2Business Computer Project 2
Business Computer Project 2Warawut
 
Object-Oriented Programming 4
Object-Oriented Programming 4Object-Oriented Programming 4
Object-Oriented Programming 4Warawut
 
Business Computer Project 1
Business Computer Project 1Business Computer Project 1
Business Computer Project 1Warawut
 
Chapter 1 Organization & MIS
Chapter 1 Organization & MISChapter 1 Organization & MIS
Chapter 1 Organization & MISWarawut
 
Object-Oriented Programming 3
Object-Oriented Programming 3Object-Oriented Programming 3
Object-Oriented Programming 3Warawut
 
Object-Oriented Programming 2
Object-Oriented Programming 2Object-Oriented Programming 2
Object-Oriented Programming 2Warawut
 
Object-Oriented Programming 1
Object-Oriented Programming 1Object-Oriented Programming 1
Object-Oriented Programming 1Warawut
 

Mehr von Warawut (20)

Database design
Database designDatabase design
Database design
 
Business Computer Project 4
Business Computer Project 4Business Computer Project 4
Business Computer Project 4
 
Object-Oriented Programming 10
Object-Oriented Programming 10Object-Oriented Programming 10
Object-Oriented Programming 10
 
Object-Oriented Programming 9
Object-Oriented Programming 9Object-Oriented Programming 9
Object-Oriented Programming 9
 
Object-Oriented Programming 8
Object-Oriented Programming 8Object-Oriented Programming 8
Object-Oriented Programming 8
 
Object-Oriented Programming 7
Object-Oriented Programming 7Object-Oriented Programming 7
Object-Oriented Programming 7
 
Object-Oriented Programming 6
Object-Oriented Programming 6Object-Oriented Programming 6
Object-Oriented Programming 6
 
Management Information System 6
Management Information System 6Management Information System 6
Management Information System 6
 
Management Information System 5
Management Information System 5Management Information System 5
Management Information System 5
 
Management Information System 4
Management Information System 4Management Information System 4
Management Information System 4
 
Object-Oriented Programming 5
Object-Oriented Programming 5Object-Oriented Programming 5
Object-Oriented Programming 5
 
Business Computer Project 3
Business Computer Project 3Business Computer Project 3
Business Computer Project 3
 
Management Information System 3
Management Information System 3Management Information System 3
Management Information System 3
 
Business Computer Project 2
Business Computer Project 2Business Computer Project 2
Business Computer Project 2
 
Object-Oriented Programming 4
Object-Oriented Programming 4Object-Oriented Programming 4
Object-Oriented Programming 4
 
Business Computer Project 1
Business Computer Project 1Business Computer Project 1
Business Computer Project 1
 
Chapter 1 Organization & MIS
Chapter 1 Organization & MISChapter 1 Organization & MIS
Chapter 1 Organization & MIS
 
Object-Oriented Programming 3
Object-Oriented Programming 3Object-Oriented Programming 3
Object-Oriented Programming 3
 
Object-Oriented Programming 2
Object-Oriented Programming 2Object-Oriented Programming 2
Object-Oriented Programming 2
 
Object-Oriented Programming 1
Object-Oriented Programming 1Object-Oriented Programming 1
Object-Oriented Programming 1
 

Kürzlich hochgeladen

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 

Kürzlich hochgeladen (20)

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 

การควบคุมภาษา C

  • 1. Chapter 3 C Language Control Mr.Warawut Khangkhan Twitter: http://twitter.com/awarawut Facebook: http://www.facebook.com/AjWarawut E-Mail: awarawut@hotmail.com Mobile: 083-0698-410
  • 2. Contents Decision / Selection Loop / Repetition Break and Continue Statement Mr.Warawut Khangkhan Chapter 3 C Language Control 2
  • 3. DECISION / SELECTION Mr.Warawut Khangkhan Chapter 3 C Language Control 3
  • 4. Decision / Selection if then if then else Nested if switch Mr.Warawut Khangkhan Chapter 3 C Language Control 4
  • 5. if then Format: if (expression) statement; Mr.Warawut Khangkhan Chapter 3 C Language Control 5
  • 6. if then Format: if (expression) { statement-1; statement-2; … } Mr.Warawut Khangkhan Chapter 3 C Language Control 6
  • 7. Example if1.c #include <stdio.h> #include <stdlib.h> int main( ) { int i; scanf(“%d”, &i); if (i > 0) printf(“a positive number was enteredn”); system(“PAUSE”); return 0; } Mr.Warawut Khangkhan Chapter 3 C Language Control 7
  • 8. Example if2.c #include <stdio.h> #include <stdlib.h> int main( ) { int i; scanf(“%d”, &i); if (i < 0) { printf(“a negative number was enteredn”); i = -i; } system(“PAUSE”); return 0; } Mr.Warawut Khangkhan Chapter 3 C Language Control 8
  • 9. if then else Mr.Warawut Khangkhan Chapter 3 C Language Control 9
  • 10. if then else Format: if (expression) statement-true; else statement-false; Mr.Warawut Khangkhan Chapter 3 C Language Control 10
  • 11. if then else Mr.Warawut Khangkhan Chapter 3 C Language Control 11
  • 12. if then else Format: if (expression) { statement-1; statement-2; … } else { statement-3; statement-4; … } Mr.Warawut Khangkhan Chapter 3 C Language Control 12
  • 13. Example ifthen1.c #include <stdio.h> #include <stdlib.h> int main( ) { int i; scanf(“%d”, &i); if (i > 0) printf(“a negative number was enteredn”); else printf(“a negative number was enteredn”); system(“PAUSE”); return 0; } Mr.Warawut Khangkhan Chapter 3 C Language Control 13
  • 14. Nested if Mr.Warawut Khangkhan Chapter 3 C Language Control 14
  • 15. Nested if Format: if (expression-1) statement-1; else if (expression-2) statement-2; else if (expression-3) statement-3; else statement-4; Mr.Warawut Khangkhan Chapter 3 C Language Control 15
  • 16. Example nestedif.c #include <stdio.h> #include <stdlib.h> int main( ) { int score; printf(“Enter score : “); scanf(“%d”, &score); if (score > 79) printf(“Very Goodn”); else if (score > 49) printf(“Goodn”); else printf(“Badn”); } Mr.Warawut Khangkhan Chapter 3 C Language Control 16
  • 17. switch Mr.Warawut Khangkhan Chapter 3 C Language Control 17
  • 18. switch Format: switch (expression) { case const-expr.1: statement-1; break; case const-expr.2: statement-2; break; case const-expr.3: statement-3; break; … [default: statement-default;] } Mr.Warawut Khangkhan Chapter 3 C Language Control 18
  • 19. Example switch1.c #include <stdio.h> #include <stdlib.h> int main( ) { char ch; printf(“Enter char : “); ch = getchar( ); switch (ch) { case 'a': case 'A': printf(“Additionn”); break; case 's': case 'S': printf(“Subtractn”); break; case 'q': case 'Q': printf(“Quitn”); break; default: printf(“Other Charactern”); } system(“PAUSE”); return 0; } Mr.Warawut Khangkhan Chapter 3 C Language Control 19
  • 20. Example switch2.c #include <stdio.h> #include <stdlib.h> int main( ) { int n = ?; swicth (n) { case 2: n++; case 4: n++; break; case 6: n++; break; case 8: n++; default: n++; } printf(“N = %dn”, n); system(“PAUSE”); return 0; } Mr.Warawut Khangkhan Chapter 3 C Language Control 20
  • 21. LOOP / REPETITION Mr.Warawut Khangkhan Chapter 3 C Language Control 21
  • 22. Loop / Repetition while loop do ... while loop for loop Mr.Warawut Khangkhan Chapter 3 C Language Control 22
  • 23. while loop Format: while (expression) statement; Mr.Warawut Khangkhan Chapter 3 C Language Control 23
  • 24. while loop Format: while (expression) { statement-1; statement-2; … } Mr.Warawut Khangkhan Chapter 3 C Language Control 24
  • 25. Example while1.c #include <stdio.h> while (num <= 10) { #include <stdlib.h> printf(“%dn”, num); num++; int main( ) { } int num = 1; while (num <= 10) printf(“%dn”, num++); system(“PAUSE”); return 0; } Mr.Warawut Khangkhan Chapter 3 C Language Control 25
  • 26. Example while2.c #include <stdio.h> #include <stdlib.h> int main( ) { int num = 1; int sum = 0; while (num <= 10) { sum += num; // sum = sum + num; num++; // num = num + 1; } printf(“Summary of 1-10 = %dn”, sum); system(“PAUSE”); return 0; } Mr.Warawut Khangkhan Chapter 3 C Language Control 26
  • 27. do ... while loop Format: do statement; while (expression); Mr.Warawut Khangkhan Chapter 3 C Language Control 27
  • 28. do ... while loop Format: do { statement-1; statement-2; … } while (expression); Mr.Warawut Khangkhan Chapter 3 C Language Control 28
  • 29. Example dowhile1.c #include <stdio.h> do { #include <stdlib.h> printf(“%dn”, num); int main( ) { num++; int num = 1; } while (num <= 10); do printf(“%dn”, num++); while (num <= 10); system(“PAUSE”); return 0; } Mr.Warawut Khangkhan Chapter 3 C Language Control 29
  • 30. Example dowhile2.c #include <stdio.h> #include <stdlib.h> int main( ) { int num = 1; int sum = 0; do { sum += num; // sum = sum + num; num++; // num = num + 1; } while (num <= 10); printf(“Summary of 1-10 = %dn”, sum); system(“PAUSE”); return 0; } Mr.Warawut Khangkhan Chapter 3 C Language Control 30
  • 31. for loop Format: for (expr.1; expr.2; expr.3) statement; or for (expr.1; expr.2; expr.3) { statement-1; statement-2; … } Mr.Warawut Khangkhan Chapter 3 C Language Control 31
  • 32. Example for1.c #include <stdio.h> for (num = 1; num <= 10; ) { #include <stdlib.h> printf(“%dn”, num); num++; int main( ) { } int num; for (num = 1; num <= 10; num++) printf(“%dn”, num); system(“PAUSE”); return 0; } Mr.Warawut Khangkhan Chapter 3 C Language Control 32
  • 33. Example for2.c #include <stdio.h> #include <stdlib.h> int main( ) { int num = 1; for ( ; num <= 10; ) { printf(“%dn”, num; num++; } system(“PAUSE”); return 0; } Mr.Warawut Khangkhan Chapter 3 C Language Control 33
  • 34. Example for3.c #include <stdio.h> #include <stdlib.h> int main( ) { int num; int sum = 0; for (num = 1; num <= 10; num++) { sum += num; // sum = sum + num; } printf(“Summary of 1-10 = %dn”, sum); system(“PAUSE”); return 0; } Mr.Warawut Khangkhan Chapter 3 C Language Control 34
  • 35. Change with for loop to while loop for (expr.1; expr.2; expr.3) statement; 1 2 expr.1; 4 3 while (expr.2) { statement; expr.3; } Mr.Warawut Khangkhan Chapter 3 C Language Control 35
  • 36. Change with for loop to do .. while loop for (expr.1; expr.2; expr.3) statement; 1 2 expr.1; 4 3 do { statement; expr.3; } while (expr.2); Mr.Warawut Khangkhan Chapter 3 C Language Control 36
  • 37. Change with while loop to do .. while loop expr.1; while (expr.2) { 1 statement; 2 expr.3; } 3 expr.1; do { 4 statement; expr.3; } while (expr.2); Mr.Warawut Khangkhan Chapter 3 C Language Control 37
  • 38. BREAK AND CONTINUE STATEMENT Mr.Warawut Khangkhan Chapter 3 C Language Control 38
  • 39. Break and Continue Statement break statement FF F F continue statement F F while, do..while for F F F Mr.Warawut Khangkhan Chapter 3 C Language Control 39
  • 40. Example break.c #include <stdio.h> #include <stdlib.h> int main( ) { int num; for (num = 1; num <= 10; num++) if (num == 5) break; else printf(“%d”, num); system(“PAUSE”); return 0; } Mr.Warawut Khangkhan Chapter 3 C Language Control 40
  • 41. Example continue.c #include <stdio.h> #include <stdlib.h> int main( ) { int num; for (num = 1; num <= 10; num++) if (num == 5) continue; else printf(“%d”, num); system(“PAUSE”); return 0; } Mr.Warawut Khangkhan Chapter 3 C Language Control 41