Control Structure គោលបំណងនៃមេរៀន អ្វីជា Control structure ?

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

Recursive Descent Technique CMSC 331. UMBC 2 The Header /* This program matches the following A -> B { '|' B } B -> C { '&' C } C -> D { '^' D } D ->
Fundamental of C programming
Programming In C++ Spring Semester 2013 Lecture 4 Programming In C++, Lecture 3 By Umer Rana.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
Understanding Loops Using C Language Week 15 Mr.Omer Salih.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
Switch Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 2: Control Flow.
If () else statement, switch statement, while () loop, do…while() loop and for( ; ; ) loop 1.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
CECS 121 EXAM 1. /* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
The switch StatementtMyn1 The switch Statement Sometimes there can be a multiple-choice situation, in which you need to execute a particular set of statements.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Tutorial ICS431 – Introduction to C.
Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=
Do-while loop Syntax do statement while (loop repetition condition)
/* C Programming for the Absolute Beginner */ // by Michael Vine #include main() { printf(“\nC you later\n”); system(“pause”); }
Chapter 6: Control Structures Computer Programming Skills Second Term Department of Computer Science Foundation Year Program Umm Alqura.
Control Statements (Decision Making)
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
1. Agenda for loop Short-handed notation How to use break and continue 2.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
1 ELEC 206 Chapter 3 Control Structures 5-Step Problem Solving Methodology 1. State the problem clearly. 2. Describe the input and output. 3. Work a.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
Iterations Very Useful: Ability to repeat a block of code Example:
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
Decision Making It is used to change the order of the program based on condition. Categories: – Sequential structure – Selection structure – Iteration.
Statements
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
CSCI 171 Presentation 3. Operators Instructs C to perform some operation Assignment = Mathematical Relational Logical.
C syntax (simplified) BNF. Program ::= [ ] Directives ::= [ ] ::= | |… ::=#include > ::=#define.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
The following statements are for y = -1; if ( x ) if ( x>0 ) y = 1; else y = 0; A. y= -1 x0 B. y= 0 x0 C. y= 1 x
 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,
Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance.
The Repetition control structure using while loop.
BY ILTAF MEHDI(MCS,MCSE, CCNA) 1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI(MCS,MCSE, CCNA) 2 Programming Fundamentals Chapter.
Dr. Sajib Datta Sep 8,  char type technically is an integer type  Computer uses numeric codes to represent characters, and store characters.
Tutorial #4 Summer 2005.
Control Structures (Repetition structure) Jump Statements
Control Structures Introduction
WHILE, DO-WHILE AND FOR LOOPS
EKT120 COMPUTER PROGRAMMING
Decisions Chapter 4.
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
最新C程式語言 蔡明志 編著.
CSE1320 Loop Dr. Sajib Datta
Engineering Problem Solving with C++, Etter/Ingber
Condition Statements.
Engineering Problem Solving with C Fundamental Concepts
מבוא כללי למדעי המחשב תרגול 2
Control Structures Lecture 7.
INC 161 , CPE 100 Computer Programming
Introduction to Programming
TOPIC 4: REPETITION CONTROL STRUCTURE
Lec 5.
Program Control Structures
Program Control Topics While loop For loop Switch statement
Control Statements_2.
Code Refresher Test #1 Topics:
Control Structures Lecture 6.
CSCE 206 Lab Structured Programming in C
break & continue Statements
Presentation transcript:

Control Structure គោលបំណងនៃមេរៀន អ្វីជា Control structure ? Control structure គឺជាមែកធាងនៃលក្ខខ័ណ្ឌដែលបានផ្ដល់តំលៃជា​លក្ខណៈពិត(True)​ ឬ មិនពិត(False)។ Control Structure ប្រើប្រាស់ដើម្បីសិក្សា និងដោះស្រាយបញ្ហាដែលមានជាប្រចាំនៅក្នុងសង្គមទៅតាមកាលៈទេសៈនិមួយៗ។

Control Structure ទម្រង់នៃ Control Structure (if ) ឩទាហណ៍ៈ #include<stdio.h> void main() { int number=0; printf(“Enter the number ”); scanf(“%d”, &number); if(number >0) printf(“the number is positive number…=%d”, number); return 0; } Syntax: if(expression){ statement1;}

Control Structure 2.1 ទម្រង់នៃ Control Structure (if else ) ឩទាហណ៍ៈ #include<stdio.h> void main() { int number=0; printf(“Enter the number ”); scanf(“%d”, &number); if(number >0) printf(“the number is positive number…=%d”, number); else printf(“the number is negative number…=%d”, number); return 0; } Syntax: if(expression){ statement1;} else{ statement2;}

Control Structure 2.2. ទម្រង់នៃ Control Structure (if , else if, else…) Syntax: if(expression){ statement1;} else if(expression){ statement(s);} else{ statement2;}

Control Structure ឩទាហណ៍ៈ #include<stdio.h> void main() { int number=0; printf(“Enter the number ”); scanf(“%d”, &number); if(number >0){ printf(“the number is positive number…=%d”, number);} else if (<0){ printf(“the number is negative number…=%d”, number); } else{ printf(“the number is zero number…=%d”, number);} getch(); }

Control Structure Ternary Operator Ternary Operator គឺជាexpression ដែលសិក្សាល័ក្ខខ័ណ្ឌប្រហាក់ប្រហែល if else statement ដែរ តែវាមានទម្រង់នៃការសរសេរខ្លី។ ប៉ុន្តែ ternary operator មិនត្រូវបានរាប់បញ្ជូលក្នុង មេរៀននេះទេ គ្រាន់តែលើកយកនូវលក្ខណៈមួយចំនួនដែលមានសមត្ថភាពស្រដៀងនឹង if else ប៉ុន្នោះ។ Syntax: Identifier = (test expression)? value1: value2 ;

Control Structure ឩទាហណ៍ៈ #include<stdio.h> #include<stdbool.h> void main() { int a; scanf("%d", &a); (a>=0)? printf("%d", true): printf("%d", false); getch(); } ឩទាហណ៍ៈ #include<stdio.h> #include<stdbool.h> void main() { int a , show=0; scanf("%d", &a); show=(a>=0)? 1 : 0 ; printf("%d", show) getch(); }

Control Structure Switch ចំណាំៈ គ្រប់ Statement case ត្រូវបញ្ជប់ដោយ keyword “break”។ Variable ត្រូវមានប្រភេទទិន្ន័យជាចំនួនគត់ និងមានតួអក្សរ (char)។

Control Structure Syntax: switch(expression) { case constant1: statements 1; break; case constant2: statements 2; break; ………………….. default: statements n; break; }

Control Structure ឩទាហណ៍ៈ void main() { int input; scanf(“%d”, &input); switch ( input ) { case 1: printf(Play game”); break; case 2: printf(“Load game); break; case 3: printf(“Play multiplayer”); break; case 4: printf( "Thanks for playing!\n" ); break; default: printf( "Bad input, quitting!\n" );break; } getchar(); }

Control Structure Loop នៅក្នុងការសេរសេរកម្មវិធីដើម្បីដោះស្រាយបញ្ហាអ្វីមួយដែលនៅពេលជួបប្រទះលក្ខខណ្ឌតែ ម្ដង ឬប្រើនដងដដែលៗនោះត្រូវបានគេចាត់ទុកថា loops ។ loop គឺជាការធ្វើសកម្មភាពច្រើនដងដដែលៗរហូតជួបលក្ខខណ្ឌណាមួយទើបវាបញ្ចប់ដំណើរ ការរបស់វា។ ក្នុងនោះដែល loop បានចែកចេញជា 3​ ប្រភេទៈ​​​​ Loop , While Loop , Do While loop។ ​ ​loop ត្រូវបានគេយកមកប្រើនៅពេលដែល គេដឹងពីចំនួនដងនៃការអនុ វត្តន៍របស់វាច្បាស់លាស់ក្រោមលក្ខខណ្ឌណាមួយ។

Control Structure Syntax:               for(initialization, condition, ​increment/decrement)               {                         Statement 1;                         Statement 2;                         …………...                         Statement n;              } ឩទាហណ៍ៈ For(int i=0; i<=10; i++) { printf(“the value of loop are =%d”, i); } 1 2 4 3

Control Structure Initialization:​​​ ជាកន្លែងដែលផ្ដល់នូវតំលៃចាប់ផ្ដើម Condition: ជាកន្លែងដែលសិក្សាលក្ខខណ្ឌ Increment/Decrement:​ ជាកន្លែងដែលសំរាប់កំណត់តំលៃកើន ឬ​ ថយ។

Control Structure While Loop while loop ត្រូវបានគេយកមកប្រើប្រាស់នៅពេលមានការត្រួតពិនិត្យរូចទើបយកមកអនុវត្ដន៍ការងារជាក្រោយ។ Syntax:​ while (expression)         {         Statement 1;         Statement 2;         …………...         Statement n;        }

Control Structure

Control Structure បើសិន​ expression មានលក្ខខណ្ឌពិតនោះវានឹងអនុវត្ត Statement ដ៏ទៃទៀតដែលនៅក្រោមវា។ បើសិន​ expression មានលក្ខខណ្ឌមិនពិតនោះវាមិនអនុវត្ត Statement ដ៏ទៃទៀតដែលនៅក្រោមវាទេ។ ឩទាហណ៍ៈ int main() { int i; printf("please enter number "); scanf("%d", &i); while(i <=10) printf("the value of While =%d \n", i); i++; } getch();

Control Structure Do While Loop Syntax do      {      Statement 1;      Statement 2;      …………...      Statement n;     } while(expression);

Control Structure

Control Structure void main() { int i; printf("please enter the value:"); scanf("%d", &i); do printf("%d\n", i); i++; } while (i <=10); getch();

Exercise ចូរគណនាប្រមាណវិធីដូចខាងក្រោមៈ S=1+2+3+…+n S=2+4+6+…+2n S=1*2*3*…n +2+4+6+…+2n S= 1 + 1/2 + 1/3 + ... + 1/n S=1! * 2! * 3!...*n! S=11 + 22 + 33 + ... + nn

Exercise 1 -4 7 -10 … -40 x+x^2/2+x^3/3+.....+x^n/n 1+x+x^2+......+x^n 1 -4 7 -10 … -40 x+x^2/2+x^3/3+.....+x^n/n 1+x+x^2+......+x^n 1/2+4/5+7/8+...... 1+1/2^2+1/3^3+.....+1/n^n

Exercise