Professional Documents
Culture Documents
Lecture-15&16
Corse Title: Programming Language
Course Code: EL-255
Course Teachers: Dr. Sadia Muniza Faraz
Semester: Fall-2022
Offered to: S.E. (Electronic Engineering)
Chapter-4
Conditions
1
12/19/2022
Switch Case
How switch case works
main()
{
int id ;
printf(“enter course id of a students from 1 – 3”);
scanf(“%d”,&id);
switch(id)
{
case 1:
printf("c programming language");
break;
case 2:
printf("c++ programming language");
break;
case 3:
printf("web technology");
break;
default :
printf("no student found");
break;
}
} // end of main 3
main()
{
char ch;
program: take int ca=0,ce=0,ci=0,co=0,cu=0; // initialize all counters
some text as printf(“ type some text and press enter”);
input and do
{
counts each ch=getche();
vowel switch(ch)
separately and {
case ‘a’: ca++; break;
prints their case ‘e’: ce++; break;
count. ……….
……….
case ‘u’: cu++; break;
default :
} // end of switch
} // end of do-while
while (ch !=‘\r’);
printf(“ \n number of a=%d, \t e=%d, …., \t u=%d”, ca, ce, ci,co,cu);
getch();
} // end of main
4
2
12/19/2022
Practice program
Write a program to make a simple calculator to
perform following basic functions
• Addition
• Subtraction
• Multiplication
• division
Chapter-5
Functions
3
12/19/2022
Functions in C
• A function in is a block of code that performs a
specific task. It has a name and it is reusable.
Types of functions
1. Built-in functions/ Library Functions/ Pre-Defined functions.
2. User defined functions.
User-Defined Functions
• we write our own functions to perform
specific tasks.
• For this, we break code into discrete sections
(functions) that each perform a single,
understandable task.
• we can call our function throughout our
program in the same way that we call C library
functions.
4
12/19/2022
10
5
12/19/2022
11
Example
#include <stdio.h>
int add(int,int); Function Declaration
main()
{
int a=4,b=6,c; Function Call
c = add(a,b);
printf(“value of c = %d”,c);
}
int add( int x, int y)
{ Function Definition
int sum;
sum=x+y;
return(sum);
}
12
6
12/19/2022
Function Definition:
• Function definition contains the actual code of
execution task. In function definition we write
code whatever we want to perform by using
function.
return-type function-name (parameters)
{
declarations
statements
return value;
}
13
THE END
14