1 Introduction to “C” Control Loops and Functions Patt and Patel Ch. 13 & 14.

Slides:



Advertisements
Similar presentations
Chapter 14 Functions. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Function Smaller, simpler, subcomponent.
Advertisements

1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 4 (Conditional Statements) © CPCS
Homework Any Questions?. Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used.
Computer Science 1620 Loops.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Chapter 11-14, Appendix D C Programs Higher Level languages Compilers C programming Converting C to Machine Code C Compiler for LC-3 Please return breadboards.
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
CS 201 Functions Debzani Deb.
C++ for Engineers and Scientists Third Edition
COMP1170 Midterm Preparation (March 17 th 2009) Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 2: Control Flow.
Chapter TwelveModern Programming Languages1 Memory Locations For Variables.
Chapter 14 Functions. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Function Smaller, simpler, subcomponent.
CMPE13Cyrus Bazeghi Chapter 14 Functions in C. CMPE13 2 Functions Smaller, simpler, subcomponents of programs Provide abstraction –hide low-level details.
1 C Programming Language Review and Dissection I: Overview, Static and Automatic Variables, and Program Control Flow These lecture notes created by Dr.
Fundamentals of C and C++ Programming Control Structures and Functions.
Chapter 17 Pointers and Arrays. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Pointers and Arrays.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
Chapter 14 Functions.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
CMPE13Cyrus Bazeghi Chapter 13 C Control Structures.
Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional making.
Flow of Control Part 1: Selection
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
CPS120: Introduction to Computer Science Decision Making in Programs.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
FUNCTIONS. Funtions  The heart of effective problem solving is problem decomposition.  breaking a problem into small, manageable pieces  In C, the.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Functions. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 9-2 JSR Instruction Jumps to a location (like.
Chapter 14 Functions. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Declarations (aka prototype) int.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Decision Making and Branching (cont.)
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
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.
1 Lecture03: Control Flow 9/24/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
CPS120: Introduction to Computer Science Decision Making in Programs.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
A First Book of C++ Chapter 4 Selection.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Chapter 14 Functions.
Chapter 4 – C Program Control
Chapter 12 Variables and Operators
Chapter 4 (Conditional Statements)
Chapter 14 Functions.
JavaScript: Control Statements.
Chapter 13 Control Structures
Chapter 14 Functions.
Chapter 13 Control Structures
More C Stack Frames / Pointer variables
Structured Program
CSC 533: Programming Languages Spring 2015
Chapter 4 - Program Control
Chapter 14 Functions.
3 Control Statements:.
2.6 The if/else Selection Structure
CSC 533: Programming Languages Spring 2019
Chapter 14 Functions.
Chapter 13 Control Structures
Chapter 14 Functions.
Implementing Functions: Overview
Presentation transcript:

1 Introduction to “C” Control Loops and Functions Patt and Patel Ch. 13 & 14

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

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).

4 Example 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; z = (2 * y) / 3; compound statement; both executed if x <= 10 only first statement is conditional; second statement is always executed

5 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!

6 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...

7 Generating 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 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 AND R1, R1, #0 ; store 5 to y ADD R1, R1, #5 STR R1, R5, #-1 NOT_TRUE... ; next statement

8 “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.

9 Generating 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, #-2 ; 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

10 Matching Else with If Else is always associated with 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; is NOT the same as...

11 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”); printf(“Month has 28 or 29 days.\n”);else printf(“Don’t know that month.\n”); printf(“Don’t know that month.\n”);

12 “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 Generating Code for While x = 0; while (x < 10) { printf(“%d ”, x); x = x + 1; } 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

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

15 “For” for (init; end-test; re-init) statement 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 includedin loop statement. Note: Test is evaluated before executing loop body.

16 Generating 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 This is the same as the while example!

17 Example For Loops /* -- what is the output of this loop? -- */ for (i = 0; i <= 10; i ++) printf("%d ", i); /* -- what does this one output? -- */ 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 Nested Loops Loop body can (of course) be another loop. /* print a multiplication table */ for (mp1 = 0; mp1 < 10; mp1++) { for (mp2 = 0; mp2 < 10; mp2++) { printf(“%d\t”, mp1*mp2); } printf(“\n”); } Braces aren’t necessary, but they make the code easier to read.

19 Another Nested Loop The test for the inner loop depends on the counter variable of the outer loop. for (outer = 1; outer <= input; outer++) { for (inner = 0; inner < outer; inner++) { sum += inner; } }

20 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 “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 “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.

23 Switch Example /* 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”); case 2: printf(“Month has 28 or 29 days.\n”); break; default: printf(“Don’t know that month.\n”);}

24 More About Switch Case expressions must be constant. case i: /* illegal if i is a variable */ 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”); } switch (a) { case 1: printf(“A”); case 2: printf(“B”); default: printf(“C”); } If a is 1, prints “ABC”. If a is 2, prints “BC”. Otherwise, prints “C”.

25 Function Smaller, simpler, subcomponent of program Provides abstraction –hide low-level details –give high-level structure to program, easier to understand overall program flow –enables separable, independent development C functions –zero or multiple arguments passed in –single result returned (optional) –return value is always a particular type In other languages, called procedures, subroutines,...

26 Example of High-Level Structure main() { SetupBoard(); /* place pieces on board */ DetermineSides(); /* choose black/white */ /* Play game */ do { WhitesTurn(); BlacksTurn(); } while (NoOutcomeYet()); } /* Play game */ do { WhitesTurn(); BlacksTurn(); } while (NoOutcomeYet()); } Structure of program is evident, even without knowing implementation.

27 Functions in C Declaration (also called prototype) int Factorial(int n); int Factorial(int n); Function call -- used in expression a = x + Factorial(f + g); a = x + Factorial(f + g); type of return value name of function types of all arguments 1. evaluate arguments 2. execute function 3. use return value in expression

28 Function Definition State type, name, types of arguments –must match function declaration –give name to each argument (doesn't have to match declaration) int Factorial(int n) { int i; int result = 1; for (i = 1; i <= n; i++) result *= i; return result; int i; int result = 1; for (i = 1; i <= n; i++) result *= i; return result;} gives control back to calling function and returns value

29 Why Declaration? Since function definition also includes return and argument types, why is declaration needed? Use might be seen before definition. Compiler needs to know return and arg types and number of arguments. Definition might be in a different file, written by a different programmer. –include a "header" file with function declarations only –compile separately, link together to make executable

30 Example double ValueInDollars(double amount, double rate); main() {... dollars = ValueInDollars(francs, DOLLARS_PER_FRANC); printf("%f francs equals %f dollars.\n", francs, dollars);... } double ValueInDollars(double amount, double rate) { return amount * rate; return amount * rate;} declaration function call (invocation) definition

31 Implementing Functions: Overview Activation record –information about each function, including arguments and local variables –stored on run-time stack Calling function push new activation record copy values into arguments call function get result from stack Called function execute code put result in activation record pop activation record from stack return

32 Run-Time Stack Recall that local variables are stored on the run-time stack in an activation record Frame pointer (R5) points to the beginning of a region of activation record that stores local variables for the current function When a new function is called, its activation record is pushed on the stack; when it returns, its activation record is popped off of the stack.

33 Run-Time Stack main Memory R6 Watt Memory R6 main Memory main Before callDuring callAfter call R5 R6 R5

34 Activation Record int NoName(int a, int b) { int w, x, y;... return y; } NameTypeOffsetScope abwxyabwxy int NoName y x w dynamic link return address return value a b bookkeeping locals args R5

35 Activation Record Bookkeeping Return value –space for value returned by function –allocated even if function does not return a value Return address –save pointer to next instruction in calling function –convenient location to store R7 in case another function (JSR) is called Dynamic link –caller’s frame pointer –used to pop this activation record from stack

36 Example Function Call int Volta(int q, int r) { int k; int m;... return k; } int Watt(int a) { int w;... w = Volta(w,10);... return w; }

37 Calling the Function w = Volta(w, 10); ; push second arg AND R0, R0, #0 ADD R0, R0, #10 ADD R6, R6, #-1 STR R0, R6, #0 ; push first argument LDR R0, R5, #0 ADD R6, R6, #-1 STR R0, R6, #0 ; call subroutine JSR Volta q r w dyn link ret addr ret val a xFD00 new R6 Note: Caller needs to know number and type of arguments, doesn't know about local variables. R5 R6

38 ANSI C Summary C is the basis for most modern languages: Java, C#, C++C is the basis for most modern languages: Java, C#, C++ Has a fairly close relationship to the hardware, not too much abstractionHas a fairly close relationship to the hardware, not too much abstraction For the most part it is portable across plateformsFor the most part it is portable across plateforms Use “gcc” or “cc” on UNIX machineUse “gcc” or “cc” on UNIX machine

39 Questions?

40