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

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

Embed Size (px)

Citation preview

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

Introduction to C Programming

CE00312-1

Lecture 3

Control Structures in C

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

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

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

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

MainstatementCall function1statementstatementCall function 2return

Function 1

Function 2

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

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

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

Declaring and using a function #include <stdio.h>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;}

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

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

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

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

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

If Statements

if (condition) statement;

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

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

Conditions and Operators

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

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

Some Examples

If the conditional value is non-zero then statement1 is executed, otherwise execution passes to next statement after if construct.

e.g. 1if (age<18||age >75)

printf(“Error”);e.g. 2  if (gender !=’m’ && gender !=’f’)

printf(“Error”);e.g. 3

if (x<0) x=-x;

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

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”);

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

Compound statements

e.g. 5

if (a<b)

{

temp=a; /* compound */

a=b; /* statement */

b=temp;

}

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

Compound Statements

e.g. 7if (a>b)

{max=a;flag=1;}

else{max=b;flag=2;}

e.g. 6

if (a>b)

max=a;

else

max=b;

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

Validation Example

#include <stdio.h>int main(void){int valid,age;char gender;scanf(“%d%c”,&age, &gender);

valid=1;if (age<18||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;}

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

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”);

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

Dangling else problem1. 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

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

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}

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

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)

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

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;}

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

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”);

}

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

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