Sei sulla pagina 1di 26

1

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

Multiple statements within if

General form:
if (condition)
{
statement 1;
statement 2;
-----------;
statement n;
}
6

EXAMPLE

Do the same program with multiple statements

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

if-else if-else if-else


if (condition) if (condition)
{ {
statement 1; statement 1;
statement 2; statement 2;
} }

else else if (condition)


{ {
statement 1; statement 1;
statement 2; statement 2;
} }

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

Example of if-else if-else statement

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

Nested if-else statements


General form Another form

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.

Conditions: 1. 500 Taka for a working hour


2. 1000 Taka bonus for more than 20
working hours
int hrs, salary;
salary = 0;
printf(How many hours have you worked: );
scanf(%d,&hrs);
salary = hrs*500;
if (hrs>=20)
{ salary = salary + 1000;
printf(\nYou got Tk.1000 as bonus.);
}
printf(\n\nYour salary is %d, salary);
13

TEST
Write a program that will find out whether
an input number is odd or even
14

Example on if - else statement

#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.

No. of Units consumed Amount of bill


Less than or equal to 100 200
Between 101 and 130 250
Between 131 and 150 275
Over 150 300

Take the number of units consumed as input and


print the amount of bill as output.
16
#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)
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;

case value#n: statements;


break;
default: statements
}
19

Example on switch statement -1

#include <stdio.h>
#include <conio.h>

void main(void)
{
int num;
clrscr();
printf("Enter any integer between 1 to 4:");
scanf("%d",&num);

continued to the next page..


20

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

printf("Enter the expression: ");


scanf("%d%c%d",&a,&op,&b);
continued to the next page..
23
..continued from the previous page

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

Write a C program to prepare an electricity bill.

No. of Units Tk./unit

Less than or
equal to 100 2.00
Between 101 and 130 2.50
Between 131 and 150 2.75
Over 150 3.00

Your program should take the number of units consumed


as input and print the total amount of bill (in Taka) as
output.
25

ASSIGNMENT-2

Write a program that will take a


character as input and determine
whether the character is a numeric or
alphabetic. If it is an alphabetic
character also check that whether it is
vowel or consonant.
26

Thank you
Dr.Maglub-Al-Nur
Dr. Md. Mamun
A. k. M. monjur morshed
Md.Al-Amin Khan Chowdhuri

Potrebbero piacerti anche