Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #3 Control.

Similar presentations


Presentation on theme: "Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #3 Control."— Presentation transcript:

1 Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #3 Control Flows and Functions

2 Programming III Remember to Check – http://moodle.cis.fiu.edu http://moodle.cis.fiu.edu – http://users.cis.fiu.edu/~forte007/prog3/ http://users.cis.fiu.edu/~forte007/prog3/ Any problems with basic compilation? Have you check the resource folders? – Do you know where they are?

3 Programming III This lecture covers Chapter 3 and 4 from K&R – You can find Chapter 5,6,9,10 (5 + 6, 9 + 10) in Modern Approach in C with much more detail

4 Statement and Blocks X = 5; //statement { } // inside of a block or compound statement if (exp) s1; else s2; if (exp) { s1; s2; } else s3; if (exp) s1; else if (exp) s2; else if (exp) s3; else s4;

5 Wrong! Why?

6 Example (Binary Search)

7

8 For and While Loops while (expression) s1; while (true) ; while (expr) { s1; s2; } for (e1; e2; e3) s1; for ( ; ; ) ; for (e1; e2; e3) { s1; s2; }

9 Example

10 Shellsort

11 Reverse String Comma Operator

12 DO While

13 Break and Continue

14 Goto and Labels

15 Note about Goto It will help to exit nested loops But you can always write code without goto – I have never needed in C, C++, C#, or Java. I assume that I will not needed in most other languages – My only code that has goto in C++, is a C library for the WiiMote controller that came with it. However, the issue with goto is that makes code look messy. – But there is nothing wrong with having a jump – We do it in Assembly

16 Another example of Goto

17 But we can do it without GOTO

18 Functions and Program Structure Break large computation task into smaller task Enable you (or other people) to reuse it Ideally, you always want to have small functions You may want to break the C files into many, depending on your project – This is true for any other language Compile multiple files : gcc –Wall main.c getline.c strindex.c -o pattern.out

19 Example -- ould

20

21 main

22 int getline(char s[], int lim)

23 int strindex(char s[], char t[])

24 Functions return-type function-name (argument declarations) { declarations; statements; return expression; // if void, just return }

25 Functions returning non-integer

26 main calling atof

27 atof (note) double atof(char []); – says that atof returns a double and takes a character. It is better to used either function declaration – Use extern if needed.

28 Function declaration #include //already has a declaration extern void myGetLine(); float pow(float x, float y); int main() { … pow(5,2); } float pow(float x, float y) { … }

29 atoi

30 extern External variables are global variables Functions are always external If in separate file – use extern keyword

31 Push and Pop

32 Use of Extern

33 Header Files Allows to keep ideas functions together It is easier to reuse.

34

35 Static variables It has two uses – In private variables, it allows to keep its value – In external (global) variables It allows to hide variables from other source files – static int value; A note about register value – It is unlikely that modern compilers will use this. But if they do, it is unclear that the developer will gain an advantage – register int x;

36 Block Structure (scope) if (n > 0) { int x = 10; for (int x = 0; x < n; x++) … }

37 Block Structure (scope) int x; int y; f (double x) { double y; … }

38 Initialization external and static variables are initialized to zero – However, I like to be safe, so always initialized. Automatic (function vars) and Register variables – Undefined (garbage) External and Static variables – Initialization value must be constant Automatic and Register variables – Initialization does not have to be constant

39 Initialization Arrays can be initialized with braces or by individual members of the arrays Characters arrays can use string or {}.

40 We can also do recursion in C

41 The C Pre-Processor #include “filename” #include Macro Substitution – #define name replacementText – #define foreverloop for ( ; ; ) – #define max(A,B) ( (A) > (B) ? (A) : (B)) x = max(p+q,r+s) Which it will be replaced by x = (( p+ q) > ….. ) It has pitfalls max(i++,j++) // wrong – #define square(x) x * x square (z + 1)

42 Avoid macro Functions Avoid macro functions as much as possible It can brings lots of problems – Undesired side effects – Makes it harder for debugging

43 #define #define has a #undef If you use #param in a definition it will replace it with quotes If use ##, then you can get concatenation

44 “ “ #param If you use #param in a definition it will replace it with quotes

45 Conditional Inclusions if !defined(HDR) #define HDR #if #elif #else #endif #ifdef #ifndef

46 Conditional Inclusion (2)

47


Download ppt "Programming III SPRING 2015 School of Computer and Information Sciences Francisco R. Ortega, Ph.D. McKnight Fellow and GAANN Fellow LECTURE #3 Control."

Similar presentations


Ads by Google