Lecture CSP07
Lecture CSP07
• Selection Structure
• Conditional
If statement provide one option to choose
if (condition) F
condition
{
True-statement-block; T
}
True-Statement-block
next-statement;
int X,Y;
if(X%Y)
printf(“X is not divisible Next Statement
by Y”);
if (x <= 10) {
y = x * x + 5; compound statement;
z = (2 * y) / 3; both executed if x <= 10
}
if (x <= 10)
only first statement is conditional
y = x * x + 5;
second statement is
z = (2 * y) / 3; always executed
More if Examples
if (x = 2)
y = 5; always true,
so y = 5 is always executed!
if(i>j)
k = i++ + --j;
else
k = ++i + j--;
if(test-1)
statement-1;
else if(test-2)
statement-2;
else if(test-3)
statement-3;
else if(test-4)
statement-4;
else
default-statement;
next_statement;
Example:
if(test-1)
{ if(test-2)
{
statement(s)-1;
}
else
{
statement(s)-2;
}
}
else
{
statement(s)-3;
}
next_statement;
Nested if examples
if (x == 3)
if (y != 6) {
z = z + 1;
w = w + 2;
}
is same as...
if ((x == 3) && (y != 6))
{
z = z + 1;
w = w + 2;
}
Matching else with If
15
Exercise: on if-else
1. Write a program to determine whether a number
is odd or even and print the appropriate
message.
16
Switch
evaluate
expression
switch (expression)
{
case const1:
block-1; break;
= const1? block-1
T
F
case const2:
block-2; break;
= const2? block-2
default: T
block-3; F
}
block-3
next_statement
17
Example using switch
#include <stdio.h>
int main()
{ int num = 8;
switch (num) {
case 7:
printf("Value is 7");
break;
case 8:
printf("Value is 8");
break;
case 9:
printf("Value is 9");
break;
default:
printf("Out of range");
break;
}
return 0;
}
Example: Month’s have how many days
21
switch points to remember
22
goto Statement
To branch unconditionally from one location to
another location in the program.
23
Use of goto Statement
Consider the program segment.
label:
printf(“Enter a number:”);
scanf(“%d”,&num);
if(num==0)
goto label;
num = num/2;
24
Program using switch
25
switch(choice){
case ‘A’:
res = num1+num2;
printf(“\n Addition of %f and %f” is %f”,num1,num2,res);
break;
case ‘B’:
res = num1-num2;
printf(“\n Sub of %f and %f” is %f”,num1,num2,res);
break;
case ‘C’: res = num1/num2;
printf(“\n Division of %f and %f” is %f”,num1,num2,res);
break;
case ‘D’:
res = num1*num2;
printf(“\n Mul of %f and %f” is %f”,num1,num2,res);
break;
default:
printf(“Not a valid Choice”);
}
return 0;
26
}
Note:
Remove all break statements from the program
and observe the output if
Choice = ‘A’ ??
Choice = ‘B’ ??
Choice = ‘C’ ??
Choice = ‘D’ ??
Assume num1 = 125.0 and num2 = 10.0
Programming Exercise
Write the previous program using if–else statement.
And observe which is convenient.
27
#include <stdio.h> Example
int main() {
int number=5;
switch (number)
{ case 1:
case 2:
case 3:
printf("One, Two, or Three.\n");
break;
case 4:
case 5:
case 6:
printf("Four, Five, or Six.\n");
break;
default:
printf("Greater than Six.\n");
}
}
Practice Home Task:
Write the program implementation for the decision diagrammed in the accompanying flow
diagram shown below using appropriate selection structure in C language.
Practice Home Task:
2. Write a program that read principal amount, rate of interest and no of days
for a loan and Computes the simple interest for the loan using following
formula.
Simple_Interest = Pincipal_Amount * Rate * Days / 365;
31