Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Review of Chapter 3--- Flow of Control  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound.

Similar presentations


Presentation on theme: "1 Review of Chapter 3--- Flow of Control  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound."— Presentation transcript:

1 1 Review of Chapter 3--- Flow of Control  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement and empty statement  Select among alternative actions The if and if-else statement The switch statements The conditional Operator  Achieve iterative actions The while statement The for statement The do statement The break and continue statements  Nested Flow of Control

2 2 Review of Chapter 3  Representation of true and false in C  false: represented by any zero value  true: represented by any nonzero value  Three types of operators  Relational operators:, =  Equality Operators: == and !=  Logical operators:!, &&, and || int int  These operators yield either the int value 0 (false) or the int value 1 (true).

3 3 Review of Chapter 3  statement  Expression statement An expression followed by a semicolon  Compound Statement A series of declarations and statements surrounded by braces.  Empty Statement A single semicolon.

4 4 Review of Chapter 3  if and if-else statement  exp is enclosed by parentheses  Where appropriate, compound statements should be used to group a series of statements under the control of a single if expression  An if or if-else statement can be used as the statement part of another if or if-else statement. an else attaches to the nearest if. if (expr) statement1 else statement2 if (expr) statement1

5 5 Review of Chapter 3  switch statement  Evaluate the switch_cond.  Go to the case label having a constant value that matches the value of the switch_cond. If a match is not found, go to the default label. If there is no default label, terminate the switch.  Terminate the switch when a break statement is encountered, or by “falling off the end”. switch ( switch_cond ) { case case_cond1: statements; break; // optional... case case_condn: statements; break; // optional default: statements; break; } Next statement //optional

6 6 The switch Statement switch ( switch_cond ) { case case_cond1: statements; break; // optional case case_cond2 : statements; case case_cond3 : statements; …../* no break */ case case_condi : statements; break; …… case case_condn: statements; break; // optional …… } Next statement;

7 7 The switch Statement  Example #include int main(void) { int x; x= 3; switch ( x ) { case 1: printf("case 1\n"); case 2: printf("case 2\n"); break; case 3: printf("case 3\n"); case 4: printf("case 4\n"); case 5: printf("case 5\n"); break; case 6: printf("case 6\n"); break; default: printf(“default\n”);break; } return 0; } % gcc switch.c % a.out case 3 case 4 case 5

8 8 The switch Statement switch ( switch_cond ) { case case_cond1: statements; break; // optional …… case case_condi : statements; break; …… case case_condn: statements; break; // optional default: statements; break; } Next statement;

9 9 The switch Statement  Example #include int main(void) { int x; x= 11; switch ( x ) { case 1: printf("case 1\n"); case 2: printf("case 2\n"); break; case 3: printf("case 3\n"); case 4: printf("case 4\n"); case 5: printf("case 5\n"); break; case 6: printf("case 6\n"); break; default: printf(“default\n”); break; } return 0; } % gcc switch.c % a.out default

10 10 The switch Statement switch ( switch_cond ) { case case_cond1: statements; break; // optional …… case case_condi : statements; break; …… case case_condn: statements; break; // optional } Next statement;

11 11 The switch Statement  Example #include int main(void) { int x; x= 11; switch ( x ) { case 1: printf("case 1\n"); case 2: printf("case 2\n"); break; case 3: printf("case 3\n"); case 4: printf("case 4\n"); case 5: printf("case 5\n"); break; case 6: printf("case 6\n"); break; } return 0; } % gcc switch.c % a.out %

12 12 Review of Chapter 3  switch Statement  switch_cond must be of integer type  case conditions must be of integer type and must all be unique

13 13 Review of Chapter 3  Conditional Operator ?:  General form: expr1? expr2: expr3  Semantics: First, expr1 is evaluated. If it is nonzero (true), then expr2 is evaluated, and this is the value of the conditional expression as a whole. If expr1 is zero (false), then expr3 is evaluated, and this is the value of the conditional expression as a whole.  Precedence: Just above the assignment operators  Associativity: Right to left

14 14 Review of Chapter 3  The while statement  First expr is evaluated.  If expr is nonzero (true), then statement is executed and control is passed back to the beginning of the while loop.  Statement is repeatedly until expr is zero (false)  Then control passes to next statement. while (expr) statement; next statement

15 15 Review of Chapter 3  The for Statement  First expr1 is evaluated.  Then expr2 is evaluated. If expr2 is nonzero (true), othen statement is executed, oexpr3 is evaluated ocontrol passes back to the beginning of the for loop again, except that evaluation of expr1 is skipped. The process continues until expr2 is zero (false), at which point control passes to next statement. for(expr1; expr2; expr3){ statement } next statement

16 16 Review of Chapter 3  The do Statement  First statement is executed, and expr is evaluated.  If the value of expr is nonzero (true), then control passes back to the beginning of the do statement, and process repeats itself.  When expr is zero (false), then control passes to next statement do statement while (expr); Next statement

17 17 Review of Chapter 3  break statement  An exit from the innermost enclosing loop or switch statement  continue statement  Causes the current iteration of a loop to stop and the next iteration to begin immediately.

18 18 Review of Chapter 3  Statements  Expression statement  Compound Statement  Empty Statement  Flow-of-control statement if, if-else switch for while do break, continue

19 19 Nested Flow of Control switch ( switch_exp ) { case constant_exp1: statements; break; // optional case constant_exp2 : statements; break; // optional... case constant_expn: statements; break; // optional default: statements; break; } //optional if (expr) statement1 else statement2 if (expr) statement1  Expression statement  Compound Statement  Empty Statement  Flow-of-control statement  if, if-else  switch  for  while  do  break, continue

20 20 Nested Flow of Control while (expr) statement; next statement for(expr1; expr2; expr3){ statement } next statement do statement while (expr); Next statement  Expression statement  Compound Statement  Empty Statement  Flow-of-control statement  if, if-else  switch  for  while  do  break, continue

21 21 End of Review of Chapter 3 Read 3.1 – 3.22

22 22 Chapter 4: Functions and Structured Programming

23 23 Introduction  An important feature of C:  Structured Programming Language  What is “Structured Programming”? The construction of a program embodies top- down design. oDecomposing a problem into smaller problems. o  a collection of small problems or task.

24 24 Introduction  How C supports structured programming?  The function construct is used to write code that solves the small problems that result from the decomposition.  These functions are combined into other functions and ultimately used in main() to solve the original problem.

25 25 Outline of Chapter 4  How to write a function?  Function Invocation  Function Definition  The return Statement  Function Prototypes  More about function  Function Declarations from the Compiler’s viewpoint  Invocation and Call-by-Value  Program Correctness: The assert() Macro  Top-Down Design

26 26 How to write a function?  An Example: prints a message  In the main function, function prn_message is called to print the following message A message for you: Have a nice day!  How? How to give the compiler information about the function? How to pass control to the function? How to specify the behavior of the function? How to get control back from the function?

27 27 How to write a function? #include void prn_message(void); int main(void) { prn_message(); printf(“Back to main function\n”); return 0; } void prn_message(void) { printf(“A message for you: “); printf(“Have a nice day!\n”); return; }

28 28  How to give the compiler information about the function?  How to pass control to the function?  How to specify the function?  How to get the control back? How to write a function? #include void prn_message(void); int main(void) { prn_message(); printf(“Back to main function\n”); return 0; } void prn_message(void) { printf(“A message for you: “); printf(“Have a nice day!\n”); return; } Function prototype:  returns no values to the calling environment and  tells the compiler this function takes no arguments

29 29 How to write a function? #include void prn_message(void); int main(void) { prn_message(); printf(“Back to main function\n”); return 0; } void prn_message(void) { printf(“A message for you: “); printf(“Have a nice day!\n”); return; } Function Invocation: The function is called  How to give the compiler information about the function?  How to pass control to the function?  How to specify the function?  How to get the control back?

30 30 How to write a function? #include void prn_message(void); int main(void) { prn_message(); printf(“Back to main function\n”); return 0; } void prn_message(void) { printf(“A message for you: “); printf(“Have a nice day!\n”); return; } Function Definition: The function is defined  How to give the compiler information about the function?  How to pass control to the function?  How to specify the function?  How to get the control back?

31 31 How to write a function? #include void prn_message(void); int main(void) { prn_message(); printf(“Back to main function\n”); return 0; } void prn_message(void) { printf(“A message for you: “); printf(“Have a nice day!\n”); return; } return statement: Control is passed back to the environment which calls the function  How to give the compiler information about the function?  How to pass control to the function?  How to specify the function?  How to get the control back?

32 32 How to write a function? #include void prn_message(void); int main(void) { prn_message(); prn_message(); printf(“Back to main function\n”); return 0; } void prn_message(void) { printf(“A message for you: “); printf(“A message for you: “); printf(“Have a nice day!\n”); printf(“Have a nice day!\n”); return; return;}  How to give the compiler information about the function? Function prototype:  How to pass control to the function? Function Invocation  How to specify the function? Function Definition  How to get the control back? return statement

33 33 Function Invocation #include void prn_message(void); int main(void) { prn_message(); printf(“Back to main function\n”); return 0; } void prn_message(void) { printf(“A message for you: Have a nice day!\n”); return; }  A program  is made up of one or more functions.  One of them is function main() How control is passed among functions?

34 34 Function Invocation  How control is passed among functions?  execution begins with main()  When program control encounters a function name followed by parentheses, the function is called, or invoked, which means Program control passes to the function  After the function does its work, program control is passed back to the calling environment, where program execution continues.

35 35 Function Invocation #include void prn_message(void); int main(void) { prn_message(); printf(“Back to main function\n”); return 0; } void prn_message(void) { printf(“A message for you: “); printf(“Have a nice day!\n”); return; }  execution begins with main()  When program control encounters a function name followed by parentheses, the function is called, or invoked, which means control passes to the function  After the function does its work, control is passed back to the calling environment execution continues.

36 36 Function Invocation  Function Invocation:  A function is called in the following format: a function name followed by parentheses  Examples: fun_name(); fun_name(argument1, argument2);  A function can be called in another function.

37 37 Function Invocation #include int min2(int a, int b); int min3(int a, int b, int c); int main(void) { printf("min of 11,23,24 is %d\n", min3(11,23,24)); } int min2(int a, int b) { if (a<b) return a; else return b; } int min3(int a, int b, int c) { int mofab = min2(a,b); return min2(mofab, c); }

38 38 How to write a function? #include void prn_message(void); int main(void) { prn_message(); prn_message(); printf(“Back to main function\n”); return 0; } void prn_message(void) { printf(“A message for you: “); printf(“A message for you: “); printf(“Have a nice day!\n”); printf(“Have a nice day!\n”); return; return;}  How to give the compiler information about the function? Function prototype:  How to pass control to the function? Function Invocation  How to specify the function? Function Definition  How to get the control back? return statement

39 39 Function Definition  Function Definition  The C code that describes what a function does #include void prn_message(void); int main(void) { prn_message(); return 0; } void prn_message(void) { printf(“A message for you: Have a nice day!\n”); return; }

40 40 Function Definition — Example #include void prn_message(int no_of_messages); int main(void) { int how_many; printf(“How many times do you want to see the message?”); scanf(“%d”, &how_many); prn_message(how_many); } void prn_message(int no_of_messages) { int i; for (i = 0; i < no_of_messages; ++i) printf(“ Have a nice day!\n); }

41 41 Function Definition  General form of function definition type function_name ( parameter type list ) { declarations statements } header body void prn_message(int no_of_messages) { int i; for (i = 0; i < no_of_messages; ++i) printf(“ Have a nice day!\n); } header body declarations statements Example:

42 42 Function Definition  General form of function definition (cont’d)  Head: type of the function: othe type of the value returned by the function. ovoid: nothing is returned. type function_name ( parameter type list ) { declarations statements } header void prn_message(int no_of_messages) { int i; for (i = 0; i < no_of_messages; ++i) printf(“ Have a nice day!\n); }

43 43 Function Definition  General form of function definition (cont’d)  Head: Parameter type list: othe number and types of the arguments that are passed into the function when it is invoked ovoid: no arguments type function_name ( parameter type list ) { declarations statements } header void prn_message(int no_of_messages) { int i; for (i = 0; i < no_of_messages; ++i) printf(“ Have a nice day!\n); }

44 44 Function Definition  General form of function definition (cont’d)  Head: Parameters in parameter type list (called as formal parameters) oIdentifiers, can be used within the function body oUpon invocation, the value of the argument corresponding to a formal parameter is used within the function body. type function_name ( parameter type list ) { declarations statements } header void prn_message(int no_of_messages) { int i; for (i = 0; i < no_of_messages; ++i) printf(“ Have a nice day!\n); }

45 45 Function Definition — Example #include void prn_message(int no_of_messages); int main(void) { int how_many; printf(“How many times do you want to see the message?”); scanf(“%d”, &how_many); prn_message(how_many); return 0; } void prn_message(int no_of_messages) { int i; for (i = 0; i < no_of_messages; ++i) printf(“ Have a nice day!\n); }

46 46 Function Definition  General form of function definition  Body: specifies the behavior of a function. Upon invocation, the value of the argument corresponding to a formal parameter is used within the function body. type function_name ( parameter type list ) { declarations statements } header body

47 47 Function Definition  Summary  Header: Type: the type of the value returned by the function. parameter type list: othe number and types of the arguments that are passed into the function when it is called. oparameters, called formal parameter :  Identifiers, can be used within the function body Upon invocation, the value of the argument corresponding to a formal parameter is used within the function body. void: no arguments or no return value  Body:specifies the behavior of a function. type function_name ( parameter type list ) { declarations statements }

48 48 How to write a function? #include void prn_message(void); int main(void) { prn_message(); prn_message(); printf(“Back to main function\n”); return 0; } void prn_message(void) { printf(“A message for you: “); printf(“A message for you: “); printf(“Have a nice day!\n”); printf(“Have a nice day!\n”); return; return;}  How to give the compiler information about the function? Function prototype:  How to pass control to the function? Function Invocation  How to specify the function? Function Definition  How to get the control back? return statement

49 49 The return Statement  When a return statement is executed,  Program control is passed back to the calling environment;  If an expression follows the keyword return, the value of the expression is returned. This value is converted, if necessary, to the type of the function, as specified in the header to the function definition.

50 50 The return Statement  Two forms of a return statement:  return;  return expression; Examples: oreturn 7; oreturn (a+b+c);

51 51 The return Statement #include void prn_message(int no_of_messages); int main(void) { int how_many; printf(“How many times do you want to see the message?”); scanf(“%d”, &how_many); prn_message(how_many); return 0; } void prn_message(int no_of_messages) { int i; for (i = 0; i < no_of_messages; ++i) printf(“ Have a nice day!\n); } No return statement

52 52 The return Statement  There can be zero or more than one return statements in a function.  Zero return statement: “falling off the end” control is passed back when the closing brace is encountered.  More than one return statement Once a return statement is executed, control is passed back.

53 53 The return Statement #include int min(int a, int b); int main(void) { int j, k, minimum; printf(“Input two integers: “); scanf(“%d%d”, &j, &k); minimum = min(j,k); printf(“\n The minimum is %d.\n\n”, minimum); return 0; } int min(int a, int b) { if (a<b) return a; else return b; }

54 54 How to write a function? #include void prn_message(void); int main(void) { prn_message(); prn_message(); printf(“Back to main function\n”); return 0; } void prn_message(void) { printf(“A message for you: “); printf(“A message for you: “); printf(“Have a nice day!\n”); printf(“Have a nice day!\n”); return; return;}  How to give the compiler information about the function? Function prototype:  How to pass control to the function? Function Invocation  How to specify the function? Function Definition  How to get the control back? return statement

55 55 Function Prototype  Functions should be declared before they are used.  Function prototype: tells the compiler The number and type of arguments that are to be passed to the function The type of the value that is to be returned by the function  main function is special Usually, the programmer does not supply a function prototype for main().

56 56 Function Prototypes  The general form of a function prototype  type function_name(parameter type list);  Examples:  int max(int a, int b); type: int parameter type list: int a, int b

57 57 Function Prototypes #include int max(int a, int b); int main(void) { int j, k, m; printf(“Input two integers: “); scanf(“%d%d”, &j, &k); m = max(j,k); printf(“\n The maximum is %d.”, m); return 0; } int max(int a, int b) { if (a>b) return a; else return b; }

58 58 Function Prototypes  The general form of a function prototype  type function_name(parameter type list);  Parameter type list A comma-separated list of types Identifiers are optional oIdentifiers are not used by the compiler; oPurpose: documentation Example: ovoid f(char c, int i); is equivalent to ovoid f(char, int);

59 59 Function Prototypes #include int max(int, int); int main(void) { int j, k, m; printf(“Input two integers: “); scanf(“%d%d”, &j, &k); m = max(j,k); printf(“\n The maximum is %d.”, m); return 0; } int max(int a, int b) { if (a>b) return a; else return b; }

60 60 Function Prototypes  Why function prototypes:  All the compilers need to check the types of values passed to a function.  Values are coerced, where necessary.  Example: double f(double) Function call: f(4) o4 is promoted to a double

61 61 Function Prototypes — Why function prototypes #include int fun(int a); int main(void) { double a = 17.2; printf("fun(a)=%d\n", fun(a)); } int fun(int a) { return (a/3*3); } fun(a)=15

62 62 Function Prototypes — Why function prototypes #include int fun(double a); int main(void) { double a = 17.2; printf("fun(a)=%d\n", fun(a)); } int fun(double a) { return (a/3*3); } fun(a)=17

63 63 How to write a function? #include void prn_message(void); int main(void) { prn_message(); prn_message(); printf(“Back to main function\n”); return 0; } void prn_message(void) { printf(“A message for you: “); printf(“A message for you: “); printf(“Have a nice day!\n”); printf(“Have a nice day!\n”); return; return;}  How to give the compiler information about the function? Function prototype:  How to pass control to the function? Function Invocation  How to specify the function? Function Definition  How to get the control back? return statement

64 64 How to write a function? Preprocessing directives function prototype of fucntion 1 int main(void) { Body of function def } Header of function1 def { Body of function def } Header of function1 def { Body of function def } function invocations

65 65 How to write a function?  Question: write a code to computer the maximum value by using a function max()?  Put its function prototype at the top of the file  Call the function as appropriate in main()  Write its function definition at the bottom of the file

66 66 The return Statement #include Preprocessing directives Function prototype int main(void) { ……. …… Call function …… } Header of function1 def { Body of function def } int max(int a, int b); int main(void) { int j, k, m; printf(“Input two integers: “); scanf(“%d%d”, &j, &k); m = max(j,k); printf(“\n The maximum is %d.”, m); return 0; } int max(int a, int b) { if (a>b) return a; else return b; } C code for calculating the maximum value

67 67  End of Class on Oct 12  Read 4.1 to 4.5

68 68


Download ppt "1 Review of Chapter 3--- Flow of Control  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound."

Similar presentations


Ads by Google