1 for – while – switch ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

Fundamental of C programming
1 Flowchart If – Then – Else อนันต์ ผลเพิ่ม Anan Phonphoem
CHAPTER 5: LOOP STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.
1 CIS Jan Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.
Branches and Loops Selim Aksoy Bilkent University Department of Computer Engineering
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
Week 7 - Programming II Today – more features: – Loop control – Extending if/else – Nesting of loops Debugging tools Textbook chapter 7, pages
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Programming in Java Unit 4. Learning outcome:  LO2: Be able to design Java solutions  LO3: Be able to implement Java solutions Assessment criteria:
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Engineering Computation with MATLAB Second Edition by David M. Smith.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
Chapter 05 (Part III) Control Statements: Part II.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
1 CSCI N201 Programming Concepts and Database 9 – Loops Lingma Acheson Department of Computer and Information Science, IUPUI.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Review if imag(x) > 0 fprintf('Sorry, but I don''t understand complex numbers.\n'); return end if x < 10 % leave this out, type the next line first, and.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
1 Conditional Statements + Loops ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem
CSI 3125, Preliminaries, page 1 Control Statements.
Program Structures Chapter 5. 5 Branching Allows different code to execute based on a conditional test. if, if-else, and switch statements.
EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 1 Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
CSE123 - Lecture 4 Structured Programming- Loops.
Computer C programming Chapter 3. CHAPTER 3 Program Looping –The for Statement –Nested for Loops –for Loop Variants –The while Statement –The do Statement.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
PH2150 Scientific Computing Skills Control Structures in Python In general, statements are executed sequentially, top to bottom. There are many instances.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
ITEC 2600 Introduction to Analytical Programming
REPETITION CONTROL STRUCTURE
Chapter 4 MATLAB Programming
JavaScript: Control Statements I
Chapter 5: Control Structures II
JavaScript: Control Statements.
ITM 352 Flow-Control: Loops
Logical Operators and While Loops
Week 8 - Programming II Today – more features: Loop control
Chapter 6 Decision Making and Looping
Types of Flow of Control
3 Control Statements:.
Lab5 PROGRAMMING 1 Loop chapter4.
Conditional Statements
Logical Operators and While Loops
Decisions, decisions, decisions
Computer Programming Basics
REPETITION Why Repetition?
Announcements Exercise Sets 2 and 3 Due Thursday.
Presentation transcript:

1 for – while – switch ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem

2 Loops Repeating a calculation for a number of times Each repetition of the loop is a pass Two types of loops (In MATLAB) for loop while loop

3 for loops for loop var = m:s:n statements end Variable > n True False Increment variable by s Set loop variable = m Statements

4 for loops (Example) for a = 1:2:10 total = total + a end a > 10 True False a = a + 2 a = 1 total = total +a

5 break command To abnormally jump out of the loop before its end

6 Find the answer of the program for num = 10:-2:0 disp(num); temp = 2*num - 10; solution = temp + 5; end solution = solution - 10 >> cmd solution = -15 Results cmd1.m

7 break example % showing ‘break’ command for num = 10:-2:0 disp(num); temp = 2*num - 10; if (temp <= 0) break end solution = temp + 5; end solution = solution - 10 >> cmd solution = -3 Results cmd2.m

8 continue command To skip for some cases and continue execute the rest of the loop For example, skip the error that might happen

9 continue example a = [ ]; for k = 1:length(a) solution = 1000/a(k) end >> cmd3 solution = 10 Warning: Divide by zero. > In cmd3 at 3 solution = Inf solution = 100 solution = -100 Results cmd3.m

10 continue example % showing ‘continue’ command a = [ ]; for k = 1:length(a) if (a(k)== 0) continue end solution = 1000/a(k) end >> cmd4 solution = 10 solution = 100 solution = -100 Results cmd4.m

11 while loop Looping process is terminated based on the specified condition

12 condition False True Statements while loops while condition statements end Set loop variable = m Increment variable Without the, no way to get out of the loop. It ’ s called “ infinite loop ”

13 while example (I) while k > 0 disp(k); end >> cmd5 ??? Undefined function or variable "k". Error in ==> cmd5 at 1 while k > 0 Results cmd5.m

14 while example (II) k = 5; while k > 0 disp(k); end >> cmd6 5 … Results cmd6.m To break the program Press “ Ctrl-C ”

15 while example (III) k = 5; while k > 0 disp(k); k = k-1; end >> cmd Results cmd7.m

16 while and for loop % showing ‘break’ command for num = 10:-2:0 disp(num); temp = 2*num - 10; if (temp <= 0) break end solution = temp + 5; end solution = solution - 10 cmd2.m Rewrite the program By using “ while ”

17 while and for loop % using while num = 10; temp = 2*num - 10; while temp > 0 disp(num); solution = temp+5; temp = 2*num -10; num = num-2; end solution = solution - 10 cmd8.m

18 Nested Logic condition1 False True Statement1 condition2 FalseTrue Statement3Statement2 if logical exp1 statement g1 elseif logical exp2 statement g2 else statement g3 end

19 if-else and switch comparison switch input expression case value1 statement g1 case value2 statement g2 otherwise statement g3 end if logical exp1 statement g1 elseif logical exp2 statement g2 else statement g3 end

20 Nested Logic – (Example) a == 20 False True c = a * 2 c == 30 FalseTrue c = 0c = a + 2 if a == 20 c = a * 2 elseif a == 30 c = a + 2 else c = 0 end switch a case 20 c = a * 2 case 30 c = a + 2 otherwise c = 0 end

21 switch switch input expression case value1 statement group 1 case value2 statement group 2 case value1 statement group 3... otherwise statement group n end

22 switch example (I) response = input(‘Type like, hate, or ok: ’,’s’); switch response case ‘like’ disp(‘I really like it’); case ‘hate’ disp(‘I do not like it’); case ‘ok’ disp(‘It is ok’); otherwise disp(‘Your enter is wrong’); end cmd9.m

23 switch example (I) >> cmd9 Type like, hate, or ok: like I really like it >> cmd9 Type like, hate, or ok: hate I do not like it >> cmd9 Type like, hate, or ok: abc Your enter is wrong >> cmd9 Type like, hate, or ok: Like Your enter is wrong

24 Can you make it better ?

25 while true response = input(‘Type like,hate,ok, or quit:’,’s’); response = lower(response); switch response case ‘like’ disp(‘I really like it’); case ‘hate’ disp(‘I do not like it’); case ‘ok’ disp(‘It is ok’); case ‘quit’ break otherwise disp(‘Your enter is wrong’); end %end switch end %end while switch example (II) cmd10.m

26 switch example (II) >> cmd10 Type like, hate, ok, or quit: like I really like it Type like, hate, ok, or quit: Hate I do not like it Type like, hate, ok, or quit: abc Your enter is wrong Type like, hate, ok, or quit: quit >>