Prog Lab3
Prog Lab3
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
?
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
#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
-------------- statement;
------------- ----------------------
-------------- -----------------------
label: ----------------------