Introduction To C Programming ปรีดา เลิศพงศ์วิภูษณะ ภาควิชาวิศวกรรมคอมพิวเตอร์ คณะวิศวกรรมศาสตร์ มหาวิทยาลัยเกษตรศาสตร์

Slides:



Advertisements
Similar presentations
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Advertisements

C Language.
Programming Languages and Paradigms The C Programming Language.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Kernighan/Ritchie: Kelley/Pohl:
Lecture 2 Introduction to C Programming
Structure of a C program
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Chapter 2: Introduction to C++.
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
C Programming. Chapter – 1 Introduction Study Book for one month – 25% Learning rate Use Compiler for one month – 60%
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
C Tokens Identifiers Keywords Constants Operators Special symbols.
C-Language Keywords(C99)
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return.
C++ Programming: Basic Elements of C++.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
C Programming language Basic Concepts Prepared By The Smartpath Information systems
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
C LANGUAGE Characteristics of C · Small size
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
The Machine Model Memory
Computer Science 210 Computer Organization
Chapter 2: Introduction to C++
C Characters and Strings
BASIC ELEMENTS OF A COMPUTER PROGRAM
Programming Languages and Paradigms
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
C Short Overview Lembit Jürimägi.
Programming Paradigms
C Basics.
Introduction to C Programming Language
Chapter 2: Introduction to C++
Introduction to C Programming
Computer Science 210 Computer Organization
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
2.1 Parts of a C++ Program.
Basics of ‘C’.
Govt. Polytechnic,Dhangar
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
C Programming Getting started Variables Basic C operators Conditionals
Chapter 2: Introduction to C++.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Programming Languages and Paradigms
C Language B. DHIVYA 17PCA140 II MCA.
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

Introduction To C Programming ปรีดา เลิศพงศ์วิภูษณะ ภาควิชาวิศวกรรมคอมพิวเตอร์ คณะวิศวกรรมศาสตร์ มหาวิทยาลัยเกษตรศาสตร์

C Programming Language Abstract Data Type and Problem Solver 2 Why use C General-purpose programming language Closely associated with the UNIX and Linux system –C was developed on UNIX –UNIX, Linux and its software are written in C. 13,000 lines of code 800 lines at the very lowest level in assembly System programming language Low-Level language with High-Level structure High-Level language with Low-Level efficiency

C Programming Language Abstract Data Type and Problem Solver 3 The C Programming Language A block structured language The language is weakly typed (explicit type conversion not required). For example, –arithmetic expressions may involve operand of type character and integer; –no type checking between actual and format parameters in a procedure call

C Programming Language Abstract Data Type and Problem Solver 4 Operation systems System programming Compilers Commercial applications Database management systems C has many application areas

C Programming Language Abstract Data Type and Problem Solver 5 Characteristics of C Structured Modular (compiled separately) Free format (read ability) Single thread (one input, one output) Relatively low level

C Programming Language Abstract Data Type and Problem Solver 6 Flow control for structured program –decision making (if) –looping with the termination test at the top (while, for) test at the bottom (do) selecting one of a set of possible case (switch) Characteristics of C

C Programming Language Abstract Data Type and Problem Solver 7 Characteristics of C Call by value (array names are passed as a location of the array of the array origin [call by reference]) Any function may be called recursively Function definitions may not be nested Compact object code Portable

C Programming Language Abstract Data Type and Problem Solver 8 Getting Start The only way to learn a new programming Language is by writing program in it. I want to print this Hello, world using any editor (vi) to edit hello.c main() { /* This is my first C program */ printf(“Hello, world\n”); printf(“Hello, world\n”);}

C Programming Language Abstract Data Type and Problem Solver 9 Compiling gcc -c hello.c gcc -c -o hello hello.c gcc -c -o {O-FileName} {C-FileName} Linking gcc hello.o gcc -o hello hello.o gcc -o {E-FileName} {O-FileName} Compiling & Linking gcc Compiler Hello.c Hello.o, a.out gcc Compiler Hello.o Hello.o, a.out

C Programming Language Abstract Data Type and Problem Solver 10 Runninghello{E-FileName} $./hello Hello, world $./a.out Hello, world $ _ Running

C Programming Language Abstract Data Type and Problem Solver 11 Component of C program main() function name { start of function { start of function.. function body. function body.. } end of function } end of function

C Programming Language Abstract Data Type and Problem Solver 12 main main() function name main is special function name. Every program must have a main() function to show the compiler where the program start, this function can be used only once. –C dose make a distinction between upper and lowercase letter. MAIN, Main, main are not the same. –a-z, A-Z, 0-9, _ –first character cannot be a digit, may be more than 8 char long, but on some system not all characters are significant Component of C program

C Programming Language Abstract Data Type and Problem Solver 13 Lexical Elements Whitespace and Line Termination In C source programs the blank (space), end-of-line, vertical-tab, form feed, and horizontal-tab are know collectively as whitespace character. They are used to separate when they appear in character or string constants.

C Programming Language Abstract Data Type and Problem Solver 14 Character Encodings Each character in a computer’s character set will have some conventional encoding. ‘A’ = 65 ‘Z’ = 90 ‘Z’- ‘A’ + 1 = ? (26) Lexical Elements

C Programming Language Abstract Data Type and Problem Solver 15 Comments Comments may contain any number of character and are always treated as whitespace Begin with /*, end with */ Legal anywhere a space is legal /* in-line comment */ /* in-line comment */ /* /* * * Header Comment * Header Comment * */ */

C Programming Language Abstract Data Type and Problem Solver 16 Comments แบบครั้งละบรรทัดใช้ //...EOL เช่น printf(“Test Program”) // ปิดหน้าต่าง แบบหลายบรรทัดใช้ /*...*/ เช่น /*The purpose of this script is to calculate the total from three difference variable.*/

C Programming Language Abstract Data Type and Problem Solver 17 void Squares() /* no arguments */ { int i; int i; /* /* loop from 1 to 10 loop from 1 to 10 printing out the squares printing out the squares */ */ for (i = 1; i <= 10; i++) for (i = 1; i <= 10; i++) printf(“%d squared is %d\n”, i, i*i); printf(“%d squared is %d\n”, i, i*i);} Example of Comments

C Programming Language Abstract Data Type and Problem Solver 18 Identifiers identifier must not begin with a digitit must not have the same as a reserved word. An identifier, also called name in C is a sequence of letters, digits, and underscores. An identifiers must not begin with a digit, and it must not have the same as a reserved word. averylongidentifier avery_long_identifier avery_long_identifier AveryLongIdentifier AveryLongIdentifier

C Programming Language Abstract Data Type and Problem Solver 19 Identifier Name ต้องเริ่มต้นด้วยตัวอักษร ยาวสุดไม่เกิน 40 ตัวอักษร จะต้องไม่มีช่องว่าง สามารถใช้ตัวเลขได้ ใช้อักษรพิเศษต่อไปนี้ได้ _ มีความแตกต่างระหว่างอักษรตัวใหญ่และตัวเล็ก

C Programming Language Abstract Data Type and Problem Solver 20 Reserved word The following identifier have been reserved and must not be used as program identifiers asm default float register asm default float register auto do for return auto do for return break double fortran short break double fortran short case else goto signed case else goto signed const enum int static const enum int static continue extern long struct continue extern long struct

C Programming Language Abstract Data Type and Problem Solver 21 Integer Constants decimal-constants L L L

C Programming Language Abstract Data Type and Problem Solver 22 octal-constants L L L hexadecimal-constants 0xFF 0xFFL 0x1A 0x1AL 0x2A 0x2AL Integer Constants

C Programming Language Abstract Data Type and Problem Solver 23 Floating-point Constants Fix-point Constants F 0.0L F 1.0L F 1.5l Floating-point Constants 3E1 3E1L 1.0E-3 1.0E-3L 1.0E67 1.0E67L

C Programming Language Abstract Data Type and Problem Solver 24 Character Constants CHARACTER VALUE CHARACTER VALUE ‘a’ 97 ‘a’ 97 ‘ ’ 32 ‘ ’ 32 ‘/r’ 13 ‘/r’ 13 ‘A’ 65 ‘A’ 65 ‘?’ 63 ‘?’ 63 ‘\O’ O ‘\O’ O ‘\377’ 255 ‘\377’ 255 ‘%’ 37 ‘%’ 37 ‘8’ 56 ‘8’ 56 ‘\23’ 19 ‘\23’ 19 ‘\\’ 92 ‘\\’ 92

C Programming Language Abstract Data Type and Problem Solver 25 String Constants CHARACTER VALUE “” ‘\0’ “” ‘\0’ “\\” ‘\’,‘\0’ “\\” ‘\’,‘\0’ “Total” ‘T’,‘o’,‘t’,‘a’,‘l’,‘\0’ “Total” ‘T’,‘o’,‘t’,‘a’,‘l’,‘\0’ “Copyright 1982” ‘C’,‘o’,‘p’,‘y’,‘r’,‘i’,‘g’,‘h’,‘t’, “Copyright 1982” ‘C’,‘o’,‘p’,‘y’,‘r’,‘i’,‘g’,‘h’,‘t’, ‘ ’,’1’,’9’,’8’,’2’,’\0’ ‘ ’,’1’,’9’,’8’,’2’,’\0’ ‘Total’ = ‘T’ ‘Total’ = ‘T’

C Programming Language Abstract Data Type and Problem Solver 26 Escape Characters ‘\Escape-Code’ f Form Feed n r t v \ ‘ “ ? b a Escape code Translation Alert,Bell Backspace Question mask New Line Carriage Return Horizontal tab Vertical tab Backslash Single Quote Double Quote

C Programming Language Abstract Data Type and Problem Solver 27 The C preprocessor The C preprocessor is a simple macro processor conceptually processes the source text of a C program before the compiler reads the source program.

C Programming Language Abstract Data Type and Problem Solver 28 Definition and Replacement #define {First-String} {Second-String} Example #define sum(x,y) x+y result = sum(5,a*b); the preprocessor replaces the source program line with result = 5+a*b;

C Programming Language Abstract Data Type and Problem Solver 29 Simple macro Definitions #define name Sequence-of-Token When the name is encountered in the source program text or other appropriate context, the name is replaced by the body #define BLOCK_SIZE 0*100 #define EOF ‘\004’ #define ERRMSG “***Error***”

C Programming Language Abstract Data Type and Problem Solver 30 #define NUMBER_OF_TAPE_DRIVERS 5 count = NUMBER_OF_TAPE_DRIVES count = 5; Simple macro Definitions

C Programming Language Abstract Data Type and Problem Solver 31 Define Macros with Parameters #define name(n1, n2,..., nn) Sequence-of-Token #define product(x,y) ((x)+(y)) produtct(a+3, b); ((a+3)+(b)); ((a+3)+(b));

C Programming Language Abstract Data Type and Problem Solver 32 Undefining and Redefining Macro #undef name #define NULL 0 #define FUNC(x) x+4 #define NULL 0 #define FUNC(x) x+4 #undef FUNC(x) #define FUNC(y) y+4

C Programming Language Abstract Data Type and Problem Solver 33 #define ON 1 #undef ON #define ON 1 if (ON == 1) printf (); printf (); #undef ON Undefining and Redefining Macro

C Programming Language Abstract Data Type and Problem Solver 34 File Inclusion #include #include Search for the file in certain standard places #include “name” Search for the file in current directory #include “third.h” #include #include

C Programming Language Abstract Data Type and Problem Solver 35 Condition Compilation #if#else#endif#elif #if Constant-Expression-1 Group-of-Lines-1 Group-of-Lines-1 #elif Constant-Expression-2 Group-of-Lines-2 Group-of-Lines-2#else Group-of-Lines-3 Group-of-Lines-3#endif #ifdef name #if defined name

C Programming Language Abstract Data Type and Problem Solver 36 Declaration C Type C Type Void Scalar Function Union Aggregate Type Type Type Type Type Type Type Type pointer Arithmetic array structure type type type type type type type type integral floating-point integral floating-point type type type type enumerated character type type type type

C Programming Language Abstract Data Type and Problem Solver 37 Signed Integer Types short 16 bit int bit long 32 bit MicroSoft C short 16 bit -32,768 --> 32,767 int 16 bit -32,768 --> 32,767 long 32 bit -2,147,483,648 --> 2,147,483,647 Data Type

C Programming Language Abstract Data Type and Problem Solver 38 Unsigned Integer Type unsigned short 16 bit unsigned int bit unsigned long 32 bit MicroSoft C unsigned short 16 bit 0 --> 65,535 unsigned int 16 bit 0 --> 65,535 unsigned long 32 bit 0 --> 4,294,967,296 Data Type

C Programming Language Abstract Data Type and Problem Solver 39 Character Type unsigned char 8 bit signed char 8 bit MicroSoft C unsigned char 8 bit 0 --> 255 signed char 8 bit > 127 Data Type

C Programming Language Abstract Data Type and Problem Solver 40 Pointer Types type_name *variable; Generic Pointers void *G_Ptr; void *G_Ptr; int *I_Ptr; int *I_Ptr; char *C_Ptr; char *C_Ptr; G_Ptr = I_Ptr; G_Ptr = I_Ptr; C_Ptr = G_Ptr; C_Ptr = G_Ptr; C_Ptr = I_Ptr; C_Ptr = I_Ptr; Null Pointer (void *) 0 (void *) 0 Address & int Data; int Data; int *DataPtr = &Data; int *DataPtr = &Data;

C Programming Language Abstract Data Type and Problem Solver 41 Array Types type_name variable[N] Array and Pointer int a[10], *ip; ip = a; ip = &a[0]; a[i] a[i]*((a)+(i))

C Programming Language Abstract Data Type and Problem Solver 42 Multidimensional Arrays type_name variable[n1], [n2]...[nn] int matrix [10][10]; int t[2][3]; t[1][2] t[1][2] *(*(t+1)+2) *(*(t+1)+2)

C Programming Language Abstract Data Type and Problem Solver 43

C Programming Language Abstract Data Type and Problem Solver 44 Structure types struct struct_name {Declaration_1; Declaration_2; Declaration_2;.;.; Declaration_N;}; Declaration_N;};Example struct complex { double real; double real; }; double real; };

C Programming Language Abstract Data Type and Problem Solver 45 Example struct A {int X, Y, Z;}; struct A Data, *DataPtr; Data.x = 0; Data.y = 1; Data.z = 2; DataPtr -> x = 0; DataPtr -> y = 1; DataPtr -> z = 2; Structure types

C Programming Language Abstract Data Type and Problem Solver 46 Structure types 2 struct u { double d; char c[2]; char c[2]; int i; int i;}; d {double} (8 bytes) c {char} (2 bytes) i {int} (4 bytes)

C Programming Language Abstract Data Type and Problem Solver 47 Union types union union_name {Declaration_1; Declaration_2; Declaration_2;.;.; Declaration_N;}; Declaration_N;};Example union single_data { char cData; int iData; int iData; long lData; long lData; float fData; float fData; double dData; }; double dData; };

C Programming Language Abstract Data Type and Problem Solver 48 Union types 2 union u { double d; char c[2]; char c[2]; int i; int i;}; d {double} (8 bytes) c {char} (2 bytes) i {int} (4 bytes)

C Programming Language Abstract Data Type and Problem Solver 49 Type defined typedef oldtype newtype Example typedef long bigint; typedef long bigint; typedef struct s { int a; typedef struct s { int a; int b; int b; } Stype; } Stype;

C Programming Language Abstract Data Type and Problem Solver 50 Enumerted Types enum enum_identifier {n1,n2,..., nn} enum colour {red, blue, green}; enum colour c; The first enumeration constant receives the value 0 if no explicit value is specifiedExample enum boy {Bill = 10, enum boy {Bill = 10, John = Bill + 2, John = Bill + 2, Fred = John + 2}; Fred = John + 2};

C Programming Language Abstract Data Type and Problem Solver 51 Function Types Return_Type function_name( Parameter_1, Parameter_2, Parameter_2,.,., Parameter_n) { Parameter_n) {...}

C Programming Language Abstract Data Type and Problem Solver 52 Example int square(int x) { return x*x; return x*x;} Function Types

C Programming Language Abstract Data Type and Problem Solver 53 Pointer to Function void (*fptr)(); int *ptr[10]; int (*fptr[])(); double (*fptr[])(double) = {sin, cos, tan}; (*fptr)(-1.0);(*(double(*)())fptr)(-1.0);(*(double(*)(double))fptr)(-1.0);

C Programming Language Abstract Data Type and Problem Solver 54 qsort Function Void qsort(void *base, int num, int width, int (*compare)(void *elem1, void *elem2)); <0 elem1 < elem2 =0 elem1 = elem2 >0 elem1 > elem2

C Programming Language Abstract Data Type and Problem Solver 55 Memory Function #include #include Buffer Compare int memcmp(void *buf1, void *buf2, int size); int memcmp(void *buf1, void *buf2, int size); void *memchr(void *buf, int c, int size); void *memchr(void *buf, int c, int size); Buffer copy void *memcpy(void *dest, void *src, int size); void *memcpy(void *dest, void *src, int size); void *memmove(void *dest, void *src, int size); void *memmove(void *dest, void *src, int size); Buffer Initialization void *memset(void *dest, int c, int size); void *memset(void *dest, int c, int size);

C Programming Language Abstract Data Type and Problem Solver 56 Memory Function Allocate memory initialized void *malloc(int size); void *malloc(int size); Allocate memory uninitialized void *calloc(int num, int size); void *calloc(int num, int size); Free memory void free(void *); void free(void *); Reallocate memory void *realloc(void *buf,int newsize); void *realloc(void *buf,int newsize); Calculate size sizeof(); sizeof();

C Programming Language Abstract Data Type and Problem Solver 57 Pointer int Data[3]; int *DataPtr; int Data[3][4]; int *DataPtr[4]; int (*DataPtr)[4]; int Data[3][4][5]; int *DataPtr[4][5]; int (*DataPtr)[4][5];

C Programming Language Abstract Data Type and Problem Solver 58 Arithmetic Operators Simple assignment = assignment = assignment Binary Arithmetic + addition + addition - subtraction - subtraction * multiplication * multiplication / division / division % modulus % modulus

C Programming Language Abstract Data Type and Problem Solver 59 Binary Comparative == equal == equal != not equal != not equal > greater than > greater than >= greater than or equal to >= greater than or equal to < less than < less than <= less than or equal to <= less than or equal to Comparative Operators

C Programming Language Abstract Data Type and Problem Solver 60 Logical Operators Binary Logical && Boolean AND && Boolean AND || Boolean OR || Boolean OR Unary Logical ! NOT ! NOT

C Programming Language Abstract Data Type and Problem Solver 61 Example x = a + 1; if ((i >= MAX) || (C[i] = ‘ ’)) {...} Logical Operators

C Programming Language Abstract Data Type and Problem Solver 62 Operator Perference ( ) [ ] ->. ! ~ * & sizeof(type) * / % + - > > => => == != &^|&&||?

C Programming Language Abstract Data Type and Problem Solver 63 Assignment Operators and Expressions i = i + 2; i += 2; Most binary operators have a corresponding assignment operator (op=)

C Programming Language Abstract Data Type and Problem Solver 64 Assignment Operators and Expressions x = y++; y = y+1; x = y; x += y+5; x = x+y+5; Operator Example Equivalent ++ i++ i=i+1 -- i-- i=i-1 += i+=5 i=i+5 -= i-=5 i=i-5 *= i*=2 i=i*2 /= i/=2 i=i/2 ^= i^=2 i=i^2

C Programming Language Abstract Data Type and Problem Solver 65 x += 2; --> x = x + 2; x += 2; --> x = x + 2; x -= 2; --> x = x - 2; x -= 2; --> x = x - 2; x *= 2; --> x = x * 2; x *= 2; --> x = x * 2; x /= 2; --> x = x / 2; x /= 2; --> x = x / 2; x %= 2; --> x = x % 2; x %= 2; --> x = x % 2; x x = x x = x << 2; x >>= 2; --> x = x >> 2; x >>= 2; --> x = x >> 2; x &= 2; --> x = x $ 2; x &= 2; --> x = x $ 2; x |= 2; --> x = x | 2; x |= 2; --> x = x | 2; Assignment Operators and Expressions

C Programming Language Abstract Data Type and Problem Solver 66 Statements x = 0i++ An expression such as x = 0 or i++ becomes a statement when it is followed by a semicolon, as in x = 0; x = 0; i++; i++; printf(...); printf(...);

C Programming Language Abstract Data Type and Problem Solver 67 { } The braces { and } are used to group declarations and statement together into a compound statement or block. there is never a semicolon after the right brace that ends a block. { statement statement... } Blocks Statements

Flow Control Statement ปรีดา เลิศพงศ์วิภูษณะ ภาควิชาวิศวกรรมคอมพิวเตอร์ คณะวิศวกรรมศาสตร์ มหาวิทยาลัยเกษตรศาสตร์

C Programming Language Abstract Data Type and Problem Solver 69 Flow Control Statement if then Statement if then else Statement else if Statement switch Statement for Statement do while Statement do util Statement

C Programming Language Abstract Data Type and Problem Solver 70 IF THEN Statement Single Line Format Multi Line Format IF งานที่ต้องการทำ จริง เท็จ

C Programming Language Abstract Data Type and Problem Solver 71 Single Line Format if (LogicalExpression) Statement; Example if (Score > 95) Grade = ‘A’;

C Programming Language Abstract Data Type and Problem Solver 72 Multiline Format if (LogicalExpression) { StatementBlock } Example if (G == h) { a = a + 1; b = b + 1; }

C Programming Language Abstract Data Type and Problem Solver 73 IF THEN ELSE Statement Single Line Format Multi Line Format IF จริงเท็จ งานที่ 1 งานที่ 2

C Programming Language Abstract Data Type and Problem Solver 74 Single Line Format if (LogicalExpression) Statement1; else Statement2; Example if (Score > 94) Grade = ‘A’; else Grade = ‘F’;

C Programming Language Abstract Data Type and Problem Solver 75 Multiline Format if (LogicalExpression) { StatementBlock } else { StatementBlock } Example if (Score > 94) { Grade = ‘ A ’ ; a = a + 1; } else { Grade = ‘ F ’ ; a = a - 1; }

C Programming Language Abstract Data Type and Problem Solver 76 ELSE IF Statement IF จริงเท็จ งานที่ 1 งานที่ 4 Multi Line Format ELSE IF เท็จ งานที่ 2 ELSE IF เท็จ งานที่ 3 จริง

C Programming Language Abstract Data Type and Problem Solver 77 Multiline Format if (LogicalExpression) { StatementBlock } else if (LogicalExpression) { StatementBlock } else { StatementBlock } Example if (Mark > 90) { Grade = ‘ A ’ ; } else if (Mark > 80) { Grade = ‘ B ’ ; } else { Grade = ‘ C ’ ; }

C Programming Language Abstract Data Type and Problem Solver 78 SWITCH CASE Statement switch (Expression) { case constant_1: StatementBlock_1; break; case constant_2: StatementBlock_2; break; default: StatementBlock_n; break; } CASE งานที่ 1 งานที่ 4 งานที่ 2 งานที่ 3

C Programming Language Abstract Data Type and Problem Solver 79 BREAK Statement break; ใช้คำสั่งนี้เมื่อต้องการหยุดการทำงาน และออก จากคำสั่งนั้นไปทำคำสั่งถัดไป

C Programming Language Abstract Data Type and Problem Solver 80 Looping with the termination test at the top Single Line Format Multi Line Format WHILE DO Statement WHILE เท็จ งานที่ต้องการทำ จริง

C Programming Language Abstract Data Type and Problem Solver 81 Single Line Format while (LogicalExpression) Statement; Example X = 0; while (X < 5) X = X + 1; while ((C = gechar()) ! = EOF) putchar(C);

C Programming Language Abstract Data Type and Problem Solver 82 MultiLine Format while (LogicalExpression) { StatementBlock;. } Example X = 0; while (X < 5) { X = X + 1; } C = gechar(); while (C != EOF) { putchar(C); C = getchar(); } while ((C = gechar()) != EOF) putchar(C); putchar(C);

C Programming Language Abstract Data Type and Problem Solver 83 Looping with the termination test at the bottom. do StatementBlock;. while (LogicalExpression); The statement is executed, then expression is evaluated. If true, statement is evaluated again, and so on. If theexpression becomes false, the loop terminates. The statement is always executed at least once. DO WHILE Statement WHILE เท็จ งานที่ต้องการทำ จริง

C Programming Language Abstract Data Type and Problem Solver 84 DO WHILE Format do StatementBlock; while (LogicalExpression); Example X = 0; do X = X + 1 while (X < 5); X = 5; do X = X - 1 while (x > 0);

C Programming Language Abstract Data Type and Problem Solver 85 Create Loop c = 1; while (c <= 10) { // // คำสั่งอื่นๆ ที่ต้องการให้ ทำงาน // c = c + 1; } C <= 10 C = 1 งานที่ต้องการทำ C = C + 1 Statement ที่ 3 Statement ที่ 2 Statement ที่ 1 ตรวจสอบเงื่อนไข กำหนดค่าเริ่มต้นให้กับตัวนับ เพิ่มค่าให้กับตัวนับ

C Programming Language Abstract Data Type and Problem Solver 86 FOR Statement for (C = N1;C <= N2;C = C + N3) { // // คำสั่งอื่นๆ ที่ต้องการให้ทำงาน // } C <= N2 C = N1 เท็จ งานที่ต้องการทำ C = C + N3 จริง

C Programming Language Abstract Data Type and Problem Solver 87 FOR NEXT Statement for (FirstState; SecondState; ThirdState) { StatementBlock } Example for (i = 1;i <= 1;i++) { k = k + i } for (i = 2;i <= 20;i++) { k = k + i } for (i = -2;i >= -20; i--) { k = k + i }

C Programming Language Abstract Data Type and Problem Solver 88 Break Exiting a loop Control loop exists other than by testing at the top or bottom. The break statement provides an early exit from for, while, and do, just as from switch. A break statement cause the innermost enclosing loop (or switch) to be exited immediately.

C Programming Language Abstract Data Type and Problem Solver 89 Example while ((ch = getchar()) != EOF) { if (ch == ‘\n’) if (ch == ‘\n’) break ; break ; putchar(ch); putchar(ch);} Break Exiting a loop

C Programming Language Abstract Data Type and Problem Solver 90 Continue a loop Continue ignoring Code within a loop causes the next iteration of the enclosing loop (for, while, do) to begin:Example while ((ch = getchar()) != EOF) { if (ch == ‘\n’) if (ch == ‘\n’) continue; continue; putchar(); putchar();};

C Programming Language Abstract Data Type and Problem Solver 91 CONTINUE and BREAK Statement 1 continue กลับไปตรวจสอบเงื่อนไขแล้วทำ LOOP ถัดไป break ออกจาก LOOP หนึ่งลำดับชั้น

C Programming Language Abstract Data Type and Problem Solver 92 Example for (i = 1;i <= 10;i++) if (i > 3) continue; do X = X + 1; if (X = 3) break; while (x <= 10); for (i = 1;i <= 10;i++) { do while (j < 5) { k = i + j; if (K > 25) break; } CONTINUE and BREAK Statement 2

C Programming Language Abstract Data Type and Problem Solver 93 GOTO and Label Statement การประกาศ Label Identifier: Identifier: การใช้คำสั่ง goto goto LabelName; Example if (Flag = 1) goto JumpStep; Total = Total + 1; JumpStep : printf(“ ทดสอบคำนวณถูกต้อง ”);

C Programming Language Abstract Data Type and Problem Solver 94 RETURN Statement เมื่อต้องการหยุดการทำคำสั่งภายใน Function และส่งค่าของข้อมูลกลับใช้คำสั่ง return; return; return VariableName; return VariableName;

C Programming Language Abstract Data Type and Problem Solver 95 Declaration {AccessControl} DataType Identifier{=Value, Identifier{=Value,...}}... Single Variable Example long ll_Temp; char lc_Temp; Multiple Variable Example long ll_t1, ll_t2, ll_t3;

C Programming Language Abstract Data Type and Problem Solver 96 Scope Local Variable Global Variable

C Programming Language Abstract Data Type and Problem Solver 97 Local Variable Local Variable คือการประกาศตัวแปรต่างๆที่ ต้องการใช้งาน ในช่วงของการเขียนโปรแกรม

C Programming Language Abstract Data Type and Problem Solver 98 Global Variable Global Variable คือการประกาศตัวแปรต่างๆที่ ต้องการใช้งาน ติดต่อภายในแอพพลิเคชั่นที่ทำ การประกาศ

C Programming Language Abstract Data Type and Problem Solver 99 Assignment Operator VariableName = Values; VariableName = VariableName; VariableName = FunctionName(); Example ll_t1 = 1; ls_t2 = “test”, s_test.il_t3 = 5; s_test.CompCode[1] = 100;

C Programming Language Abstract Data Type and Problem Solver 100 Question & Answer