SPL 4 Chapter

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

WHAT IS BRANCHING

Branching: C language executes program statements in a


sequence. Sometimes we need to alter the flow of sequence of
statements.  Programmer can jump from one part of the
program to other with the help of such statements. C language
Branching Statements are given below:

1. if statement
2. if…else statement
3. nested if statement
4. switch statement
WHAT IS DECISION MAKING

Decision making: Decision making is about deciding the order


of execution of statements based on certain conditions or repeat
a group of statements until certain specified conditions are met.

Some statement are popularly known as decision making


statements. They are:

1. if statement
2. Switch statement
3. Conditional operator statement
4. goto statement.
WRITE DOWN GENARAL FORM & FLOW CHART OF IF STATEMENT

If statement: The if statement is a powerful decision making statement and


used to control the flow of execution of statement. It is basically a two way
decision statement .

Genaral form 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:

int a=10, b=5;


if( a>b){
printf(“%d”,a);
}
printf(“%d”,b);

Output: 10
WRITE DOWN GENARAL FORM & FLOW CHART OF IF ELSE
STATEMENT

The if….else statement is an extension of the simple if


statement.

The general form is:


If(condition){
True statement-block;
}
else{
False statement-block;
}
Statement-x;
Flow chart of if……else:
entry

condition

false true

False statement True statement

Statement-x
Example of f statement:

int a=10, b=5;


if( a>b){
printf(“%d”,a);
}
else{
printf(“%d”,b);
}

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.

The general form is:


If(condition 1){
if(condition 2){
True statement-block 1;
}
else{
False statement-block 2;
}
else{
statement-block 3;
}
Statement-x;
Flow chart of if……else: Statement-x
entry

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

The genaral form of else if ladder is:

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.

The genaral from of switch statement:

switch(expression){
case value-1:
block-1
break;
Case value-2:
block-2
break;
----
default:
Default-block
break;;
}
Statement-x;

Flow chart of switch statement:

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;

printf("Enter the First number: ");


scanf("%d",&a);
printf("\nEnter the First number: ");
scanf("%d",&b);
printf("\nEnter the First number: ");
scanf("%d",&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

It can terminate the exeution It doesn’t terminate the loop


CODE TO CHECK ODD OR EVEN

#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;
}

You might also like