Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to C Programming CE00312-1 Lecture 3 Control Structures in C.

Similar presentations


Presentation on theme: "Introduction to C Programming CE00312-1 Lecture 3 Control Structures in C."— Presentation transcript:

1 Introduction to C Programming CE00312-1 Lecture 3 Control Structures in C

2 Program Structure Modularity Atomisation of complex problem into manageable units – possibly reusable in other complex problems Functions Control structures Basic mechanics of process deconstruction Most problems can be expressed as combinations of Sequence Selection Repetition

3 Modularity in a C Program The Function main In C, all code is packaged in functions main is the function that the system calls when you run your program Every program must have one and only one main All functions may return a value Main statement Call function1 statement Call function 2 return Function 1 Function 2

4 Function Components A function definition consists of two parts; A Specification ( or header or interface); Specifies the function name, the return value and argument list A Body ( or block or implementation); Code to be executed when the function is called. A sequence of statements enclosed in braces

5 Declaring and using a function #include int sum(int,int); /*Function prototype */ int main(void) { int num1=4, num2=7; int total; total=sum(num1,num2); printf(“The sum of the numbers is %d\n”, total); return 0; } int sum(int n1, int n2) { /* Function to find the sum of two numbers */ int sumresult; sumresult=n1+n2; return sumresult; }

6 Program Control - Sequence Default control structure Program execution occurs sequentially unless otherwise directed Each step should be the logical consequence of the preceding one Statements should be arranged such that they occur in the right order

7 Program control - Selection Selection Implemented using If..  Single branch If..else..  Multiple branching  Ordinal relationship in branch determination Switch..case...  Multiple branching  Category branch determination

8 If Statements if (condition) statement; if (condition) statement1; else statement 2; where a statement can be a compound statement.

9 Conditions and Operators Conditions No boolean type; integer 0 == false, 1 == true e.g. 5>3 has the value 1 2>3 has the value 0 Relational Operators =, > if (x=1) printf(“OK”); if (x==1) printf(“OK”); LogicalOperators !NOT &&AND ||OR

10 Some Examples If the conditional value is non-zero then statement1 is executed, otherwise execution passes to next statement after if construct. e.g. 1 if (age 75) printf(“Error”); e.g. 2 if (gender !=’m’ && gender !=’f’) printf(“Error”); e.g. 3 if (x<0) x=-x;

11 IF.. cf IF.. ELSE If the conditional value is non-zero then statement1 is executed, otherwise statement2 is executed. e.g. 4 if (x=3) printf(“Yes”); else printf(“No”);

12 Compound statements e.g. 5 if (a<b) { temp=a; /* compound */ a=b; /* statement */ b=temp; }

13 Compound Statements e.g. 7 if (a>b) { max=a; flag=1; } else { max=b; flag=2; } e.g. 6 if (a>b) max=a; else max=b;

14 Validation Example #include int main(void) { int valid,age; char gender; scanf(“%d%c”,&age, &gender); valid=1; if (age =65) { valid=0; printf(“\nAGE ERROR\n”); } if (gender !=’m’ && gender !=’f’) { valid=0; printf(“\nGENDER ERROR\n”); } if (valid) printf(“\nData OK\n”); else printf(“\nData Error\n”); return 0; }

15 If.. Else Chains e.g. if (month==1) printf(”January”); else if (month==2) printf(“February”); else if (month==3) printf(“March”); else printf(“Error”);

16 Dangling else problem 1. if (x>0) if (y==’A’) printf(“Positive A”); else printf(“Negative”); Which condition determines the outcome? 2. if (x>0) { if (y==’A’) printf(“Positive A”); } else printf(“Negative”); A is only printed if x > and y is A

17 Switch Statement Provides a multi-way decision by testing whether an expression matches one of a number of constant values. General Form: switch (expression) { case c1:statements case c2:statements …. default:statements }

18 switch Statement expression is any integer valued expression c1, c2 are integer valued constants (remember integer include chars!) statements are any C statements default is optional c1, c2 must be unique (i.e same constant cannot appear twice) each case can have only one value (not range)

19 Example Switch Statement Classify an input character c as digit, white space or other. switch (c) { case ‘ ‘: case ‘\n’: case ‘\t’: nwhite=nwhite+1; break; case ‘0’: case ‘1’: case ‘2’: case ‘3’: case ‘4’: case ‘5’: case ‘6’: case ‘7’: case ‘8’: case ‘9’: ndigit=ndigit+1; break; default:nother=nother+1; }

20 Integer example switch switch (gradePoint) { case 1: printf (“You have failed badly”); break; case 3: printf(“A narrow fail\n”); case 2: printf(“Better luck at the resit”); break; case 4: case 5: case 6: case 7: case 8: printf(“Well done, you passed”); break; default: printf(“Not a valid grade point”); }

21 Switch after switch is evaluated control falls to matching case if no match, go to default if present terminate with break or by reaching end of switch block can group cases together by omitting break - careful! No need for {... } within case


Download ppt "Introduction to C Programming CE00312-1 Lecture 3 Control Structures in C."

Similar presentations


Ads by Google