introduction to c programming ce00312-1 lecture 3 control structures in c
TRANSCRIPT
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
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
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
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;}
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
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
If Statements
if (condition) statement;
if (condition) statement1;else statement 2; where a statement can be a compound statement.
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
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;
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”);
Compound statements
e.g. 5
if (a<b)
{
temp=a; /* compound */
a=b; /* statement */
b=temp;
}
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;
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;}
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”);
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
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}
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)
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;}
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”);
}
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