You are on page 1of 22

National Institute of Electronics & Information Technology

Gorakhpur Center
Ministry of Electronics & Information Technology (MeitY), Government of India

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Contents to be covered
• Control Statement
• if, if...else, else…if…else statement with examples

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Conditional Statement
C program executes program sequentially. Sometimes, a program requires
checking of certain conditions in program execution. Conditional statements
help you to make a decision based on certain conditions.

Followings are the different conditional statements used in C.


If Statement
If-else Statement
Nested If-else Statement

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


if() Statement

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Example of if() Function
#include<stdio.h>
#include<stdio.h> void main( )
void main( ) {
{ int qty, dis = 0 ;
int a=10; float rate, tot ;
int b=20; printf ( "Enter quantity and rate " ) ;
if(a<b) scanf ( "%d %f", &qty, &rate) ;
printf(“\n a is smaller than b”); if ( qty > 1000 )
} dis = 10 ;
tot = ( qty * rate ) - ( qty * rate * dis / 100 ) ;
printf ( "Total expenses = Rs. %f", tot ) ;
}
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
if()-else Statement

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Example of if()-else Statement
#include<stdio.h>
#include<stdio.h> void main( )
void main( ) {
{ float bs, gs, da, hra ;
int num; printf ( "Enter basic salary " ) ;
printf ( "Enter the number " ) ; scanf ( "%f", &bs ) ;
scanf ( "%d", &num ) ; if ( bs < 1500 )
if(num%2==0) {
printf(“\n num is even”); hra = bs * 10 / 100 ;
else da = bs * 90 / 100 ;
printf(“\n num is odd”); }
else
} {
hra = 500 ;
da = bs * 98 / 100 ;
}
gs = bs + hra + da ;
printf ( "gross salary = Rs. %f", gs ) ;
}
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Nested If-Else Statement

In this syntax, the condition is checked first. If it


is true, then the program control flow goes inside
the braces and again checks the next condition. If
it is true then it executes the block of statements
associated with it else executes else part.

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Example of Nested If-Else Statement
main( )
#include<stdio.h> {
void main( ) int m1, m2, m3, m4, m5, per ;
{ printf ( "Enter marks in five subjects " ) ;
int i ; scanf ( "%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5 ) ;
per = ( m1 + m2 + m3 + m4 + m5 ) / 5 ;
printf ( "Enter either 1 or 2 " ) ; if ( per >= 60 )
scanf ( "%d", &i ) ; printf ( "First division ") ;
if ( i == 1 ) else
printf ( "You are Welcome !" ) ; {
else if ( per >= 50 )
printf ( "Second division" ) ;
{ else
if ( i == 2 ) {
printf ( “I m great" ) ; if ( per >= 40 )
else printf ( "Third division" ) ;
printf ( “I m the best !" ) ; else
printf ( "Fail" ) ;
} }
} }
http://www.nielit.gov.in/gorakhpur }
/GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
else if Clause

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Example of else if Clause
main( )
{
int m1, m2, m3, m4, m5, per ;
printf ( "Enter marks in five subjects " ) ;
scanf ( "%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5 ) ;
per = ( m1 + m2 + m3 + m4 + m5 ) / 5 ;
if ( per >= 60 )
printf ( "First division" ) ;
else if ( per >= 50 )
printf ( "Second division" ) ;
else if ( per >= 40 )
printf ( "Third division" ) ;
else
printf ( "fail" ) ;
}

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Example based on character
Any character is entered through the keyboard, write a program to determine
whether the character entered is a capital letter, a small case letter, a digit or a
special symbol. The following table shows the range of ASCII values for various
characters.

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Example based on character
void main()
{
char c;
clrscr();
printf("enter the character");
scanf("%c",&c);
if(ch>=65 && ch<=90)
printf("\n Capital letter is %c",c);
if(ch>=97 && ch<=122)
printf("\n small letter is %c",c);
if(ch>=48 && ch<=57)
printf("\n digits is %c",c);
If((ch>=0 && ch<=47)||(ch>=58 && ch<=64)||(ch>=91 && ch<=96)||(ch>=123 && ch<=127))
printf("\n special character is %c",c);
getch();
}
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Quiz
Time

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Quiz
Time

1. What will be the output of the following C code?


2. What will be the output of the following C code?
#include <stdio.h>
#include <stdio.h>
int main()
int main()
{
{
int x = 1;
int x = 0;
if (x > 0)
if (x++)
printf("inside if\n");
printf("true\n");
else if (x > 0)
else if (x == 1)
printf("inside elseif\n");
printf("false\n");
}
}
a) inside if b) inside elseif
a) true b) false
c) inside if d) compile time error
c) compile time error d) undefined behavior
Answer: b
inside elseif Answer: a

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Quiz
Time

3. What will be the output of the following C 4. What will be the output of the following C
code? code?
#include <stdio.h> #include <stdio.h>
int main() int main()
{ {
int x = 0; int x = 0;
if (x == 0) if (x == 1)
printf("true, "); if (x >= 0)
else if (x = 10) printf("true\n");
printf("false, "); else
printf("%d\n", x); printf("false\n");
} }
a) false, 0 b) true, 0 a) true b) false
c) true, 10 d) compile time error c) Depends on the compiler d) No print
Answer: b statement Answer: d

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Quiz
Time

5. Which of the following is an invalid if-else


statement? 7. What will be the output of the following C code?
a) if (if (a == 1)){} b) if (func1 (a)){} #include <stdio.h>
c) if (a){} d) if ((char) a){} int main()
Answer: a {
int a = 1;
6. What will be the output of the following C code? if (a)
#include <stdio.h> printf("All is Well ");
int main() printf("I am Well\n");
{ else
int a = 1; printf("I am not a River\n");
if (a--) }
printf("True"); a) Output will be All is Well I am Well
if (a++) b) Output will be I am Well I am not a River
printf("False"); c) Output will be I am Well
} d) Compile time errors during compilation
a) True b) False Answer: d
c) True False d) No Output Answer: a
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Quiz
Time

8. What will be the output of the following C code?


#include <stdio.h> 9. What will be the output of the following C code?
void main() #include <stdio.h>
int x;
{
int x = 5; void main()
if (x < 1) {
if (x)
printf("hello");
if (x == 5) printf("hi");
printf("hi"); else
else printf("how are u");
printf("no"); }
a) hi b) how are you
}
a) hi b) hello c) compile time error d) error
c) no d) error Answer: b
Answer: a

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Quiz
Time

10. What will be the output of the following C code?


#include <stdio.h>
void main()
{
int x = 5;
if (true);
printf("hello");
}
a) It will display hello b) It will throw an error
c) Nothing will be displayed d) Compiler dependent Answer: b

 
 
 
 

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Assignments
 Write a program to input the number and check it is even or odd.
 Write a program to check a number is positive or negative.
 Write a program to check greatest among two numbers.
 Write a program to check a year leap year or not.
 Write a program to check a three digit number is palindrome or not.
 Write a program to input the cost price and selling price of an item and check for
profit or loss. Also calculate it.
 Write a program to check greatest among three numbers.

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


References

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Thank You
Any Query

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia

You might also like