Introduction
Instruction
of a programs are executed either
Sequential
manner
Branching
C
language supports the following decision making statements.
if
statement
switch
statement
conditional
operator
goto
statement
if
statement
• It
is used to control flow of execution of statement.
• It
is two-way decision statement and is used in conjuction with an expression
Syntax- if
(condition)
{ statement 1; Entry
……………..
}
False
Ex: if
(age is more than 55) True
Person is retired
Different
forms of if statement
simple
if if-else nested if-else else if
ladder
Simple
if statement
Syntax- if ( Condition)
{
statement block;
true
}
statement x ;
Ex: if ( category == sports)
{
marks=marks+bonus;
}
printf ( “%f”,marks);
Program of simple if statement
main()
{
int a,b,c,d;
float ratio;
printf(“\n enter four integer value”);
scanf(“%d %d %d”,&a,&b,&c);
if(c-d !=0)
{
ratio= (float) (a+b)/(float)(c-d);
printf(“Ratio= %f”, ratio);
}
}
Output- enter four integer values
12 23
34 45
Ratio= -3.181818
The if…else statement entry
Syntax- if( Condition)
{
true block statement;
false
}
else
{
false block statement;
}
statement x;
Ex-: if (code== 1) if (code==1)
boy=boy+ 1; boy=boy+1;
if ( code== 2) else
girl=girl+1; girl=girl+1;
Program
for if…else statement
main()
{
int
a;
printf(“Enter
an integer”);
scanf(“%d”,&a);
if(a%2==0)
printf(“
%d is even number”,a);
else
printf(“%d
is an odd number”,a);
}
output-
Enter an integer
46
46 is an even number
Nesting
of if…else statement
Syntax-
if(
test condition 1)
{
if (test condition 2)
{
statement
1;
}
else
{
statement
2;
}
}
else
{
statement 3;
statement 3;
}
statement x ;
Program
for nested if…else
statement
The
else if ladder
syntax-:
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;
Flowchart
of else….if ladder
Program for else…if ladder
The
switch statement
The
complexity of a program increases by increasing no. of if statement.
To overcome this C has a built in multiway decision statement known as switch.
Syntax
switch
(expression)
{
case value-1: ----> case labels (257 case labels)
block1;
break;
case value-2:
block2;
break;
.
.
.
default:
default block;
break;
}
statement x;
Flowchart
for switch statement
Program
for switch statement
main()
{
int grade,mark,index;
printf(“Enter ur marks \n”);
scanf(“%d”,&mark);
index=mark/10;
switch(index)
{
case 10:
case 9:
case 8:
grade=“Honours”;
break;
case 7:
case
6:
grade=“First Division”;
break;
case
5:
grade=“Second Division”;
break;
case 4:
grade=“First Division”;
break;
default:
grade=“Fail”;
break;
}
printf(“%s”,grade);
}
The
goto
statement
•The goto statement is used for branching
unconditionally.
•The goto statement breaks normal sequential
execution of the program.
•The goto requires label to where it will transfer
a control.
•Label is a valid variable name followed
by a colon( : ).
goto
label:
--------------
-------------
--------------
label:
statement;
FORWARD JUMP
label:
statement;
----------------------
-----------------------
----------------------
goto
label;;
BACKWARD
JUMP
Program
to
calculate sum of squares of all integers
main()
{
int sum=0,n=1;
loop:
sum=sum+n*n;
if( n==10)
goto print;
else
{
n=n+1;
goto loop;
}
print:
printf(“\n Sum=%d”,sum);
}
No comments:
Post a Comment