CMPE13Cyrus Bazeghi Chapter 13 C Control Structures.

Slides:



Advertisements
Similar presentations
Control Structures Corresponds with Chapters 3 and 4.
Advertisements

Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 4 (Conditional Statements) © CPCS
Computer Science 1620 Loops.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
1 Introduction to “C” Control Loops and Functions Patt and Patel Ch. 13 & 14.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
C++ for Engineers and Scientists Third Edition
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 2: Control Flow.
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.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Chapter 4 Program Control Statements
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 5: Structured Programming.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional making.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
Flow of Control Part 1: Selection
1 Flowchart notation and loops Implementation of loops in C –while loops –do-while loops –for loops Auxiliary Statements used inside the loops –break –continue.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
Chapter 5: Structured Programming
Chapter 5: Structured Programming
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
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.
Decision Making and Branching (cont.)
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
Flow Control. Comments u Comments: /* This is a comment */ –Use them! –Comments should explain: v special cases v the use of functions (parameters, return.
A First Book of C++ Chapter 4 Selection.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Chapter 5: Structured Programming
REPETITION CONTROL STRUCTURE
Chapter 12 Variables and Operators
Chapter 4 (Conditional Statements)
Chapter 4: Making Decisions.
Chapter 4 – Control Structures Part 1
Control Structures Lecture 7.
Chapter 13 Control Structures
Looping.
MSIS 655 Advanced Business Applications Programming
CS1100 Computational Engineering
Chapter 13 Control Structures
How to develop a program?
Structured Program
Exam 1 Date: Feb. 2nd, 2015 during class time (50 minutes) Coverage
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Chapter 13 Control Structures
Presentation transcript:

CMPE13Cyrus Bazeghi Chapter 13 C Control Structures

CMPE13 2 Control Structures Conditional –making a decision about which code to execute, based on evaluated expression if if-else switch Iterative –executing code multiple times, ending based on evaluated expression for while do-while

CMPE13 3 if if(condition) action; condition action T F Condition is a C expression, which evaluates to TRUE (non- zero) or FALSE (zero). Action is a C statement, which may be simple or compound (a block).

CMPE13 4 Examples of if statements if (x <= 10) y = x * x + 5; if (x <= 10) { y = x * x + 5; z = (2 * y) / 3; } if (x <= 10) y = x * x + 5; z = (2 * y) / 3; compound statement; both executed if x <= 10 only first statement is conditional; second statement is always executed (indentation doesn’t help)

CMPE13 5 About braces indentation… if(x <= 10) { y = x * x + 5; z = (2 * y) / 3; } if(x <= 10){ y = x * x + 5; z = (2 * y) / 3; } Good style: the braces are aligned (*) “Different “ style: the braces are not aligned (*) (*) Source: My opinion

CMPE13 6 LC-3 code for if statement if (x == 2) y = 5; LDR R0, R5, #0 ; load x into R0 ADD R0, R0, #-2 ; subtract 2 BRnp NOT_TRUE ; if non-zero, x is not 2 AND R1, R1, #0 ; store 5 to y ADD R1, R1, #5 STR R1, R5, #-1 NOT_TRUE... ; next statement

CMPE13 7 More if examples if(0 <= age && age <= 11) kids += 1; if(month == 4 || month == 6 || month == 9 || month == 11) printf(“The month has 30 days.\n”); if(x = 2) y = 5; This is a common programming error (= instead of ==), not caught by compiler because it’s syntactically correct. always true, so action is always executed!

CMPE13 8 if ’s can be nested if (x == 3) if (y != 6) { z = z + 1; w = w + 2; } if ((x == 3) && (y != 6)) { z = z + 1; w = w + 2; } is the same as...

CMPE13 9 if-else if(condition) action_if; else action_else; condition action_ifaction_else TF else allows choice between two mutually exclusive actions without re-testing condition.

CMPE13 10 LC-3 code for if-else if(x) { y++; z--; } else { y--; z++; } LDR R0, R5, #0 BRz ELSE ; x is not zero LDR R1, R5, #-1 ; incr y ADD R1, R1, #1 STR R1, R5, #-1 LDR R1, R5, #02 ; decr z ADD R1, R1, #1 STR R1, R5, #-2 JMP DONE ; skip else code ; x is zero ELSE LDR R1, R5, #-1 ; decr y ADD R1, R1, #-1 STR R1, R5, #-1 LDR R1, R5, #-2 ; incr z ADD R1, R1, #1 STR R1, R5, #-2 DONE... ; next statement

CMPE13 11 Matching else with if Else is always associated with the closest unassociated if. if(x != 10) if(y > 3) z = z/2; else z = z * 2; if(x != 10) { if(y > 3) z = z/2; else z = z*2; } is the same as... if (x != 10) { if (y > 3) z = z/2; } else z = z*2; and is NOT the same as... This code

CMPE13 Chaining If’s and Else’s if (month == 4 || month == 6 || month == 9 || month == 11) printf(“Month has 30 days.\n”); else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) printf(“Month has 31 days.\n”); else if (month == 2) printf(“Month has 28 or 29 days.\n”); else printf(“Don’t know that month.\n”); 12

CMPE13 while while (test) loop_body; test loop_body T F Executes loop body as long as test evaluates to TRUE (non-zero). Note: Test is evaluated before executing loop body. 13

CMPE13 LC-3 code for while AND R0, R0, #0 STR R0, R5, #0 ; x = 0 ; test LOOP LDR R0, R5, #0 ; load x ADD R0, R0, #-10 BRzp DONE ; loop body LDR R0, R5, #0 ; load x ADD R0, R0, #1 ; incr x STR R0, R5, #0 JMP LOOP ; test again DONE ; next statement x = 0; while (x < 10) { printf(“%d ”, x); x = x + 1; } 14

CMPE13 Infinite loops The following loop will never terminate: x = 0; while (x < 10) printf(“%d ”, x); Loop body does not change condition, so the test never fails. This is a common programming error that can be difficult to find. 15

CMPE13 for loops for(init; test; re-init) loop_body init test loop_body re-init F T Executes loop body as long as test evaluates to TRUE (non-zero). Initialization and re-initialization code included in loop statement. Note: Test is evaluated before executing loop body. 16

CMPE13 LC-3 code for for for(i = 0; i < 10; i++) printf(“%d ”, i); ; init AND R0, R0, #0 STR R0, R5, #0 ; i = 0 ; test LOOP LDR R0, R5, #0 ; load i ADD R0, R0, #-10 BRzp DONE ; loop body LDR R0, R5, #0 ; load i ; re-init ADD R0, R0, #1 ; incr i STR R0, R5, #0 JMP LOOP ; test again DONE ; next statement (NOTE: This is the same as the while example!) 17

CMPE13 Examples of for loops /* -- what is the output of this loop? -- */ for(i = 0; i <= 10; i ++) printf("%d ", i); /* -- what is the output of this loop? -- */ letter = 'a'; for(c = 0; c < 26; c++) printf("%c ", letter+c); /* -- what does this loop do? -- */ numberOfOnes = 0; for(bitNum = 0; bitNum < 16; bitNum++) { if(inputValue & (1 << bitNum)) numberOfOnes++; } 18

CMPE13 Nested loops /* print a multiplication table */ for(mp1 = 0; mp1 < 10; mp1++) { for(mp2 = 0; mp2 < 10; mp2++) { printf(“%d\t”, mp1*mp2); } printf(“\n”); } These braces aren’t necessary, but they make the code easier to read. The loop body can (of course) be another loop. 19

CMPE13 Another nested loop for(outer = 1; outer <= input; outer++) { for(inner = 0; inner < outer; inner++) { printf(“%d “, inner); } } The test for the inner loop depends on the counter variable of the outer loop. 20

CMPE13 for vs. while In general: for loop is preferred for counter-based loops. –Explicit counter variable –Easy to see how counter is modified each loop while loop is preferred for sentinel-based loops. –Test checks for sentinel value. Either kind of loop can be expressed as the other, so it’s really a matter of style and readability. 21

CMPE13 do-while do loop_body; while(test); loop_body test T F Executes loop body as long as test evaluates to TRUE (non-zero). Note: Test is evaluated after executing loop body. 22

CMPE13 Chaining if ’s and else ’s if(month == 4 || month == 6 || month == 9 || month == 11) printf(“Month has 30 days.\n”); else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) printf(“Month has 31 days.\n”); else if(month == 2) printf(“Month has 28 or 29 days.\n”); else printf(“Don’t know that month.\n”); 23

CMPE13 switch switch(expression) { case const1: action1; break; case const2: action2; break; default: action3; } evaluate expression = const1? = const2? action1 action2 action3 T T F F Alternative to long if-else chain. If break is not used, then case "falls through" to the next action. 24

CMPE13 Example of switch /* same as month example for if-else */ switch (month) { case 4: case 6: case 9: case 11: printf(“Month has 30 days.\n”); break; case 1: case 3: /* some cases omitted for brevity...*/ printf(“Month has 31 days.\n”); break; case 2: printf(“Month has 28 or 29 days.\n”); break; default: printf(“Don’t know that month.\n”); } 25

CMPE13 More about switch case expressions must be constant. case i: /* illegal if i is a variable */ If no break, then next case is also executed. switch (a) { case 1: printf(“A”); case 2: printf(“B”); default: printf(“C”); } What does it do? 26

CMPE13 break and continue break; –used only in switch statement or iteration statement –passes control out of the innermost loop or switch statement containing it to the statement immediately following –usually used to exit a loop before terminating condition occurs (or to exit switch statement when case is done) continue; –used only in iteration statement –terminates the execution of the loop body for this iteration –loop expression is evaluated to see whether another iteration should be performed –if for loop, also executes the re-initializer 27

CMPE13 Example What does the following loop do? What would be an easier way to write this? What happens if break instead of continue ? for(i = 0; i <= 20; i++) { if(i%2 == 0) continue; printf("%d ", i); } 28

CMPE13 Problem Solving in C Stepwise Refinement –as covered in Chapter 6...but can stop refining at a higher level of abstraction. Same basic constructs –Sequential -- C statements –Conditional -- if-else, switch –Iterative -- while, for, do-while 29

CMPE13 Problem 1: Calculating Pi Calculate  using its series expansion. User inputs number of terms. Start Initialize Get Input Evaluate Series Output Results Stop 30

CMPE13 Pi: 1st refinement Start Initialize Get Input Evaluate Series Output Results Stop Initialize iteration count count<terms Evaluate next term count = count+1 for loop F T 31

CMPE13 Pi: 2nd refinement Initialize iteration count count<terms Evaluate next term count = count+1 F T count is odd subtract termadd term if-else FT 32

CMPE13 Pi: Code for Evaluate Terms for (count=0; count < numOfTerms; count++) { if (count % 2) { /* odd term -- subtract */ pi -= 4.0 / (2 * count + 1); } else { /* even term -- add */ pi += 4.0 / (2 * count + 1); } Note: Code in text is slightly different, but this code corresponds to equation. 33

CMPE13 Pi: Complete Code #include main() { double pi = 0.0; int numOfTerms, count; printf("Number of terms (must be 1 or larger) : "); scanf("%d", &numOfTerms); for (count=0; count < numOfTerms; count++) { if (count % 2) { pi -= 4.0 / (2 * count + 1); /* odd term -- subtract */ } else { pi += 4.0 / (2 * count + 1); /* even term -- add */ } printf("The approximate value of pi is %f\n", pi); } 34

CMPE13 Problem 2: Finding Prime Numbers Print all prime numbers less than 100. –A number is prime if its only divisors are 1 and itself. –All non-prime numbers less than 100 will have a divisor between 2 and 10. Start Stop Initialize Print primes 35

CMPE13 Primes: 1st refinement Start Stop Initialize Print primes Initialize num = 2 num < 100 Print num if prime num = num + 1 F T 36

CMPE13 Primes: 2nd refinement Initialize num = 2 num < 100 Print num if prime num = num + 1 F T Divide num by 2 through 10 no divisors? Print num F T 37

CMPE13 Primes: 3rd refinement Divide num by 2 through 10 no divisors? Print num F T Initialize divisor = 2 divisor <= 10 Clear flag if num%divisor > 0 divisor = divisor + 1 F T 38

CMPE13 Primes: Using a Flag Variable To keep track of whether number was divisible, we use a "flag" variable. –Set prime = TRUE, assuming that this number is prime. –If any divisor divides number evenly, set prime = FALSE. Once it is set to FALSE, it stays FALSE. –After all divisors are checked, number is prime if the flag variable is still TRUE. Use macros to help readability. #define TRUE 1 #define FALSE 0 39

CMPE13 Primes: Complete Code #include #define TRUE 1 #define FALSE 0 main () { int num, divisor, prime; /* start with 2 and go up to 100 */ for (num = 2; num < 100; num ++ ) { prime = TRUE; /* assume num is prime */ /* test whether divisible by 2 through 10 */ for (divisor = 2; divisor <= 10; divisor++) if (((num % divisor) == 0) && (num != divisor)) prime = FALSE; /* not prime */ if (prime) /* if prime, print it */ printf("The number %d is prime\n", num); } } Optimization: Could put a break here to avoid some work. 40

CMPE13 Problem 3: Searching for Substring Have user type in a line of text (ending with linefeed) and print the number of occurrences of "the". Reading characters one at a time –Use the getchar() function -- returns a single character. Don't need to store input string; look for substring as characters are being typed. –Similar to state machine: based on characters seen, move toward success state or move back to start state. –Switch statement is a good match to state machine. 41

CMPE13 Substring: State machine to flow chart matched 't' matched 'th' matched 'the' 't' 'h' 'e' 't' no match other increment count read char match = 0 match = 1 match = 2 if 't', match=1 if 'h', match=2 if 't', match=1 else match=0 if 'e', count++ and match = 0 if 't', match=1 else match=0 T T T F F F 42

CMPE13 Substring: Code (Part 1) #include main() { char key; /* input character from user */ int match = 0; /* keep track of characters matched */ int count = 0; /* number of substring matches */ /* Read character until newline is typed */ while ((key = getchar()) != '\n') { /* Action depends on number of matches so far */ switch (match) { case 0: /* starting - no matches yet */ if (key == 't') match = 1; break; 43

CMPE13 Substring: Code (Part 2) case 1: /* 't' has been matched */ if (key == 'h') match = 2; else if (key == 't') match = 1; else match = 0; break; 44

CMPE13 Substring: Code (Part 3) case 2: /* 'th' has been matched */ if (key == 'e') { count++; /* increment count */ match = 0; /* go to starting point */ } else if (key == 't') { match = 1; else match = 0; break; } } printf("Number of matches = %d\n", count); } 45