0% found this document useful (0 votes)
18 views

Prog Lab3

Uploaded by

Balaram Pal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Prog Lab3

Uploaded by

Balaram Pal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Decision Control Structure

By
Prof. Ramesh Ch. Mohapatra
NIT, Rourkela
Recap
• Lecture 1: Introduction to C
• Lecture 2: Variables, Data Types, Operators
General Outline

• Conditional Statements
• If-Else
• Simple If
• Simple If else
• Multiple If else
• Nested If else
• Switch
• Break and continue
• Conditional operator
• goto statement
Control Statements
• Block
• It is a group of statements enclosed within
{}
•{
Statement-1
Statement-2
---------------
---------------
}
4
Control Statements
• Decision making and branching statement
• if----else
• switch
• Decision making and looping statement
• while
• do---while
• for
• Jumping statement
• break
• continue
• goto 5
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 conjunction with an expression
Syntax- if (condition)
{ statement 1; Entry
……………..
}
Test expression
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)
Entry
{
statement block;
True
} Test expression?
statement x ;
Ex: if ( category == sports) False Statement block
{
marks=marks+bonus; Statement x
}
printf ( “%f”,marks);
Example: simple if statement
int main()
{
int a,b,c,d;
float ratio;
printf(“\n Enter four integer values:”);
scanf(“%d %d %d %d”, &a, &b, &c, &d);
if(c-d !=0)
{
ratio= (float) (a+b)/(float)(c-d);
printf(“Ratio= %f”, ratio); Output: Enter four integer values:
} 12 23 34 45
return 0; Ratio= -3.181818
}
if…else statement Entry
Syntax- if( Condition)
{ True False
Condition
true block statement;
}
else True block statement False block statement
{
false block statement;
}
statement x; 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
int 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);
return 0;
}
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 x ;
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;
Multiple if else
Example:
main()
{
int a;
printf(“Enter value of a between 1 to 7 “);
scanf(“%d”, &a);
if(a==1)
printf(“Monday”);
else if(a==2)
printf(“Tuesday”);
else if(a==3)
printf(“Wednesday”);
else if(a==4)
printf(“Thursday”);
else if(a==5)
printf(“Friday”);
else if(a==6)
printf(“Saturday”);
else if(a==7)
printf(“Sunday”);
else
printf(“Not valid”);
}
Output: Enter value a: 4
Thursday
Maximum of Three Numbers
Maximum of three numbers?
Maximum of three numbers: Using Logical Operators
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.
Switch case statements: Syntax
Switch case execution
• Entered value for x is 1.
Switch case execution
• Entered value for x is 1.
Output: One
• Two
• Not one or two
break and default
• break; /*this is a statement which can break a switch */
• break exits the switch block.
• default:statements /*optional*/
• The control is transferred to default, if it exists and none of the cases
matches the expression value.
• One can not have something like case j <= 20:
• All that we can have after the case is a constant expression.
Switch case execution: break
statements
• Entered value for x is 1.
Output: One

• Entered value for x is 2.
Output: Two
• Entered value for x is 3.
Output: Not one or two
Syntax:
switch (expression)
{
case value-1:
block1;
break;
case value-2:
block2;
break;
.
.
.
default:
default block;
break;
}
statement x;
Switch case with a char
continue
• In a situation when we want to take control to the beginning of the
loop, bypassing the statements inside the loop which have not yet
been executed.
• When continue is encountered inside any loop, control automatically
passes to the beginning of the loop.
• Continue is usually associated with an if.
Example:
Main()
{
int i;
for (i = 1; i<11; i=i+1)
{ Takes Control here
if(i==5)
continue;
else
printf(“%d”, i);
}
}
Output: 1 2 3 4 6 7 8 9 10 5 is missing
Conditional Operator
• Syntax :
• (Condition? true_value: false_value);
• ‘? ‘ and ‘: ‘ are sometimes called ternary operators since they take
three arguments.
• Example :

#include <stdio.h>
int main()
{
int x, y ;
Scanf(“%d”,&x);
y = ( x >5 ? 3 : 4 ) ;
printf("x value is %d\n", x);
printf("y value is %d", y);
}
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;

------------- ----------------------

-------------- -----------------------

label: ----------------------

statement; goto label;;

FORWARD JUMP 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);
}
Assignments
1. Write a program to read mark in a subject and print its corresponding grade.
Form (90-100)=E
(80-89)=A
(70-79)=B
(60-69)=C
(50-59)=D
(40-49)=P
(<40)=F
2. WAP to check if the number is odd or even
3. WAP take three values from the keyboard and check if they are sides
of right angled triangle.
4. WAP to find the largest of three numbers x, y and z using a nested if
block.

You might also like