Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
ME-172
C
Programming language
CLASS-4
DATE: 06/01/2007
2
TOPICS
if-else statement
switch statement
3
The if statement
General form:
if (condition)
{
statement;
}
Conditions:
1. Using relational or conditional operators
2. Using logical operators
4
EXAMPLE
Write the following simple program.
[If the condition is fulfilled then it will print a line
otherwise it will escape]
int i;
printf(Enter an Integer: );
scanf(%d,&i);
if (i==1)
{
printf(\nYou typed 1);
}
printf(\n This is the end of the program);
5
General form:
if (condition)
{
statement 1;
statement 2;
-----------;
statement n;
}
6
EXAMPLE
int i;
printf(Enter an Integer: );
scanf(%d,&i);
if (i==1)
{
printf(\n You typed 1);
printf(\n This is the end of the if statement);
}
printf(\n This is the end of the program);
7
General form
else
{
statement 1;
statement 2;
}
8
Example
Do a similar program
int num;
printf(Enter an Integer: );
scanf(%d,&num);
if (num < 0)
{
printf(\n the number is less than zero );
}
else
{
printf(\n the number is greater than zero);
}
9
int num;
printf(Enter an Integer: );
scanf(%d,&num);
if (num < 0)
{
printf(\n the number is less than zero);
}
else if(num == 0)
{
printf(\n the number is equal to zero);
}
else
{
printf(\n the number is greater than zero);
}
10
if (condition) if (condition)
{ {
statement; if (condition)
} {
statement;
else }
{ else
if (condition) {
{ statement;
statement; }
} }
else else
{ {
statement; statement;
}
} }
11
Example
Write a program to find out whether your input
number is Positive, negative or zero.
scanf(%d,&num);
if (num == 0)
{
printf(\n the number is equal to zero);
}
else
{ if (num < 0)
{
printf(\n the number is negative);
}
else
{
printf(\n the number is positive);
}
}
12
MORE EXAMPLE
Write a program that will calculate the salary of a worker.
TEST
Write a program that will find out whether
an input number is odd or even
14
#include <stdio.h>
#include <conio.h>
void main(void)
{
int num;
printf(Enter an Integer: );
scanf(%d,&num);
if ((num%2)==0)
printf(\n%d is an even number, num);
else
printf(\n%d is an odd number , num);
getch();
}
15
TEST
Write a C program to prepare an electricity bill.
void main()
{
int unit, bill;
clrscr ();
printf ("\nInput the number of units consumed\n");
scanf ("%d", &unit);
if (unit <= 100)
printf("\nThe bill is 200");
else if (unit>100 && unit <= 130)
printf("\nThe bill is 250");
else if (unit>130 && unit <= 150)
printf("\nThe bill is 275");
else
printf("\nThe bill is 300");
getch();
}
17
#include <stdio.h>
#include <conio.h>
void main()
{
int unit, bill;
clrscr ();
printf ("\nInput the number of units consumed\n");
scanf ("%d", &unit);
if (unit <= 100)
bill = 200;
else if (unit <= 130)
bill = 250;
else if (unit <= 150)
bill = 275;
else
bill = 300;
printf("\nThe bill = %d\n",bill);
getch();
}
18
switch statement
General form:
switch (variable name)
{
case value#1: statements;
break;
#include <stdio.h>
#include <conio.h>
void main(void)
{
int num;
clrscr();
printf("Enter any integer between 1 to 4:");
scanf("%d",&num);
Case value #
switch(num)
{ Variable name
case 1: printf("ONE");
break;
case 2: printf("TWO");
break;
case 3: printf("THREE");
break;
case 4: printf("FOUR");
break;
default: printf(OUT OF BOUND");
}
getch();
}
21
TEST
Write a program that will take two
numbers and an option for arithmetic
operation from keyboard and will print out
the result. (Use switch)
22
SOLUTION
#include<stdio.h>
#include<conio.h>
void main(void)
{
int a,b;
char op;
clrscr();
switch(op)
{
case '+': printf(" = %d",a+b); break;
case '-': printf(" = %d ",a-b); break;
case 'x':
case '*': printf(" = %d",a*b); break;
case '/':
if(b!=0) printf(" = %d",a/b);
else printf("The value of diviser can't be zero");
break;
default : printf("Unknown Operator");
}
getch();
}
24
ASSIGNMENT-1
Less than or
equal to 100 2.00
Between 101 and 130 2.50
Between 131 and 150 2.75
Over 150 3.00
ASSIGNMENT-2
Thank you
Dr.Maglub-Al-Nur
Dr. Md. Mamun
A. k. M. monjur morshed
Md.Al-Amin Khan Chowdhuri