Lecture 3-SelectionStatements (Part 1)
Lecture 3-SelectionStatements (Part 1)
Islamabad
(Lahore Campus)
Lecture 3: Structured
Program Development
(Selection Statements)
Outline
Control Structure
If statements
If else statement
Conditional Operator
false
• Syntax errors
• Caught by compiler
• Logic errors:
• Have their effect at execution time
• Non-fatal: program runs, but has incorrect output
• Fatal: program exits prematurely
Set of Statements
Statements after
if Structure
if(a>b)
printf(“Lahore\n”);
printf(“OK\n”);
return 0;
}
/* Program takes input a number from user. If number is divisible by 3 then print the message on the screen
that ‘the number is divisible by three*/
#include<stdio.h>
int main()
Output
{
int n = 0;
Enter a number: 9
printf(“Enter a Number : ”); Number 9 is divisible by 3
scanf(“%d”,&n); OK
if(n%3==0)
{
printf(“Number %d”, n);
printf(“ is divisible by 3\n”);
}
printf(“OK\n”);
return 0;
}
Block-2 Block-1
Statements after
if Structure
int main( )
{
float bs, gs, da, hra ;
printf ( "Enter basic salary " ) ;
scanf ( "%f", &bs ) ;
if ( bs < 1500 )
{
hra = bs * 10 / 100 ;
da = bs * 90 / 100 ;
}
else
{
hra = 500 ;
da = bs * 98 / 100 ;
}
gs = bs + hra + da ;
printf ( "gross salary = Rs. %f", gs ) ;
return 0;
}
• Program takes a number from user and prints greater if the number is > 100 else prints
less or equal to
#include<stdio.h>
int main()
{
int n;
printf(“Enter an integer : ”);
scanf(“%d”,&n);
If(n>100)
printf(“Number is greater than 100”);
else
printf(“Number is less than or equal to100”);
return 0;
}