Professional Documents
Culture Documents
SPL 4 Chapter
SPL 4 Chapter
SPL 4 Chapter
1. if statement
2. if…else statement
3. nested if statement
4. switch statement
WHAT IS DECISION MAKING
1. if statement
2. Switch statement
3. Conditional operator statement
4. goto statement.
WRITE DOWN GENARAL FORM & FLOW CHART OF IF STATEMENT
if(condition){
Statement-block;
}
Statement-x;
Flow chart of if statement:
entry
con true
diti
on
Statement
block
false
Statement -x
Next
statement
Example of f statement:
Output: 10
WRITE DOWN GENARAL FORM & FLOW CHART OF IF ELSE
STATEMENT
condition
false true
Statement-x
Example of f statement:
Output: 10
WRITE DOWN GENARAL FORM & FLOW CHART OF NESTING OF IF…
ELSE STATEMENT
When a series of decision are involved ,we may have to use more than
one if……else statement in nested form.
condition
1 true
false Conditio
n2
False statement False statement true statement
Statement 3
Statement x
Example of f statement:
int a=10, b=5,c=8;
if( a>b){
If(a>c){
printf(“%d”,a);
}
else{
printf(“%d”,c);
}
}
else{
if(c>b){
printf(“%d”,c);
}
else{
printf(“%d”,b);
}
Output: 10
WRITE DOWN GENARAL FORM & FLOW CHART OF ELSE IF LADDER
STATEMENT
If(condition 1)
Statement-1;
Else if(condition 2)
Statement-2;
Else if(condition 3)
Statement-3;
Else if(condition n)
Statement-n;
Else
Default statement;
Statement-x;
Flow chart of else…..if ladder:
entry
conditi
on1
true conditi false
statement on2
false
1 true conditi
statement true on3
conditi false
2 statement
3 true on4
statement Default
4 statement
Statement
-x
next
Example of else…if ladder:
float units=520.00;
If(units<=200)
charges=0.5*units;
else if(units<=400)
charges=100+0.65*(units-200);
else if(units<=600)
charges=230+0.8*(units-400);
else
charges=390+(units-600);
printf(“%f”,charges);
Output:326.00
WRITE DOWN GENARAL FORM & FLOW CHART OF SWITCH
STATEMENT
We have seen that whe one of the many alternatives is to be selected, we can use an if
statement to control the selection.the compliexity of such a program increases
dramatically when the number of alternative increases.To simplify this complexity ,c has
a built in multiway decision statement known as a switch statement.
switch(expression){
case value-1:
block-1
break;
Case value-2:
block-2
break;
----
default:
Default-block
break;;
}
Statement-x;
expession
expression=value1
Block-1
expression=value2
Block-2
default
Default block
Statement-x
Example of switch statement:
Index=marks/10
switch(index)
{
case 6:
grade=“First division”
break;
case 5:
grade=“Second division”
break;
case 4:
grade=“Third division”
break;
}
printf(“%s”,grade);
C CODE FOR LARGEST VALUE FROM THREE VALUES
#include<stdio.h>
int main()
{
int a,b,c;
if(a>b)
if(a>c)
printf("\n\nThe largest number is: %d\n",a);
else
printf("\n\nThe largest number is: %d\n",c);
else if(b>c)
if(b>a)
printf("\n\nThe largest number is: %d\n",b);
else
printf("\n\nThe largest number is: %d\n",a);
else
if(c>a)
printf("\n\nThe largest number is: %d\n",c);
else
printf("\n\nThe largest number is: %d\n",a);
return 0;
}
DIFFERENCE BETWEEN BREAK & CONTINUE
break continue
The break can be used in loop & swtch The continue can be used in loop
satetment. statement only
#include<stdio.h>
int main()
{
int k;
level:
printf("\nEnter the a number: ");
scanf("%d",&k);
if(k<=100){
if(k%2==0)
printf("\n\nThe number is EVEN\n");
else
printf("\n\nThe number is ODD\n");
}
else{
printf("\n\nPlease enter a number less than 100\n");
goto level;
}
return 0;
}