5 Conditional Statements
5 Conditional Statements
W3schools.in, Tutorialspoint.com
UNIT-II Conditional Statements in C Geeksforgeeks.org, codeforwin.org
1
Conditional statements in C
C conditional statements allow you to make a decision, based
upon the result of a condition. These statements are
called Decision Making Statements or Conditional Statements.
Conditional Statements in C
If statement
if statement
if-else statement
Nested if-else statement
goto statement
switch statement
Conditional Operator
W3schools.in, Tutorialspoint.com
UNIT-II Conditional Statements in C Geeksforgeeks.org, codeforwin.org
If Statement
“If statement” in C is used to control the program flow based on
some condition, it's used to execute some statement code block if
the expression is evaluated to true. Otherwise, it will get skipped.
This is the simplest way to modify the control flow of the program.
Syntax
if(test_expre
ssion)
{
statement
1;
statement
2;
...
}
W3schools.in, Tutorialspoint.com
UNIT-II Conditional Statements in C Geeksforgeeks.org, codeforwin.org
Example: if Statement
#include<stdio.h>
main()
{
int a = 15, b =
20;
if (b > a)
{
printf("b is
greater");
}
}
W3schools.in, Tutorialspoint.com
UNIT-II Conditional Statements in C Geeksforgeeks.org, codeforwin.org
If-else Statement
Syntax
if(test_expressio
n)
{
//execute your
code
}
else
{
//execute your
code
}
W3schools.in, Tutorialspoint.com
UNIT-II Conditional Statements in C Geeksforgeeks.org, codeforwin.org
If-else Statement
#include<stdio.h>
main()
{
int a, b;
printf("Please enter the
value for a:");
scanf("%d", &a);
printf("\nPlease the value
for b:"); scanf("%d", &b);
if (a > b)
{
printf("\n a is
greater");
}
else
{
printf("\n b is
greater");
UNIT-II Conditional Statements in C
W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
If-else-if Statement
Syntax
if(test_expression)
{
//execute your code
}
else if(test_expression
n)
{
//execute your code
}
else
{
//execute your code
}
W3schools.in, Tutorialspoint.com
UNIT-II Conditional Statements in C Geeksforgeeks.org, codeforwin.org
#include<stdio.h> If-else-if Statement
main()
{
int a, b;
printf("Please enter the value
for a:");
scanf("%d", &a);
printf("\nPlease enter the
value for b:");
scanf("%d", &b);
if (a > b)
{
printf("\n a is greater than
b");
}
else if (b > a)
{
printf("\n b is greater than
a");
}
else
{ W3schools.in, Tutorialspoint.com
UNIT-II
printf("\nConditional Statements in C
Both are equal"); Geeksforgeeks.org, codeforwin.org
Nested if-else Statement
Syntax
if(test_expression 1)
{
if(test_expression 2)
{
//Statement block Executes
// when the boolean test
expression
// two is true.
}
}
else { //else statement block }
W3schools.in, Tutorialspoint.com
UNIT-II Conditional Statements in C Geeksforgeeks.org, codeforwin.org
Example: Nested if-else Statement
W3schools.in, Tutorialspoint.com
UNIT-II Conditional Statements in C Geeksforgeeks.org, codeforwin.org
Exercise on if-else statements
1. Write a C program to input any character and check whether it is alphabet,
digit or special character.
2. Write a C program to check whether a character is uppercase or
lowercase alphabet.
3. Write a C program to check whether a number is even or odd.
4. Write a C program to check whether a year is leap year or not.
5. Write a C program to find all roots of a quadratic equation.
6. Write a C program to calculate profit or loss.
7. Write a C program to input marks of five subjects Physics, Chemistry,
Biology, Mathematics and Computer. Calculate percentage and grade
according to following:
Percentage >= 90% : Grade A
Percentage >= 80% : Grade B
Percentage >= 70% : Grade C
Percentage >= 60% : Grade D
Percentage >= 40% : Grade E
Percentage < 40% : Grade F
W3schools.in, Tutorialspoint.com
UNIT-II Conditional Statements in C Geeksforgeeks.org, codeforwin.org
Exercise on if-else statements
Write a C program to input any character and check whether it is alphabet, digit or
special character.
W3schools.in, Tutorialspoint.com
UNIT-II Conditional Statements in C Geeksforgeeks.org, codeforwin.org
Exercise on if-else statements
Write a C program to check whether a character is uppercase or lowercase
alphabet.
W3schools.in, Tutorialspoint.com
UNIT-II Conditional Statements in C Geeksforgeeks.org, codeforwin.org
Exercise on if-else statements
Write a C program to find all roots of a quadratic equation.
W3schools.in, Tutorialspoint.com
UNIT-II Conditional Statements in C Geeksforgeeks.org, codeforwin.org
Exercise on if-else statements
Write a C program to find all roots of a quadratic equation.
W3schools.in, Tutorialspoint.com
UNIT-II Conditional Statements in C Geeksforgeeks.org, codeforwin.org
Exercise on if-else statements
Write a C program to input marks of five subjects Physics, Chemistry, Biology,
Mathematics and Computer. Calculate percentage and grade
W3schools.in, Tutorialspoint.com
UNIT-II Conditional Statements in C Geeksforgeeks.org, codeforwin.org
Exercise on if-else statements
Write a C program to input marks of five subjects Physics, Chemistry, Biology,
Mathematics and Computer. Calculate percentage and grade
W3schools.in, Tutorialspoint.com
UNIT-II Conditional Statements in C Geeksforgeeks.org, codeforwin.org
Exercise on if-else statements
Write a C program to check whether a year is leap year or not
W3schools.in, Tutorialspoint.com
UNIT-II Conditional Statements in C Geeksforgeeks.org, codeforwin.org
goto Statement
C supports a unique form of a statement that is the goto Statement which is
used to branch unconditionally within a program from one point to another.
Although it is not a good habit to use the goto statement in C, there may be
some situations where the use of the goto statement might be desirable.
W3schools.in, Tutorialspoint.com
UNIT-II Conditional Statements in C Geeksforgeeks.org, codeforwin.org
Example: goto Statement
#include<stdio.h>
void main()
{
int age;
g:
//label name
printf("you are Eligible\n");
s:
//label name
printf("you are not Eligible");
printf("Enter you age:");
scanf("%d", &age);
if(age>=18)
goto g; //goto
label g
else
goto s; //goto
label s
getch(); W3schools.in, Tutorialspoint.com
UNIT-II Conditional
} Statements in C Geeksforgeeks.org, codeforwin.org
switch Statement
Syntax
switch(variable)
{
case 1:
//execute
your code
break;
……….
case n:
//execute
your code
break;
default:
//execute
your code
}
W3schools.in, Tutorialspoint.com
UNIT-II Conditional Statements in C Geeksforgeeks.org, codeforwin.org
switch Statement
A switch statement allows a variable to be tested for equality against a list of
values. Each value is called a case, and the variable being switched on is checked
for each switch case.
W3schools.in, Tutorialspoint.com
UNIT-II Conditional Statements in C Geeksforgeeks.org, codeforwin.org
Example: switch Statement
W3schools.in, Tutorialspoint.com
UNIT-II Conditional Statements in C Geeksforgeeks.org, codeforwin.org
Disadvantages of switch Statement
Disadvantages of switch statements
float constant cannot be used in the switch as
well as in the case.
You can not use the variable expression in case.
You cannot use the same constant in two
different cases.
We cannot use the relational expression in case.
W3schools.in, Tutorialspoint.com
UNIT-II Conditional Statements in C Geeksforgeeks.org, codeforwin.org
Exercise: switch Statement
1. Write a C program to print day of week name using switch case.
2. Write a C program print total number of days in a month using switch
case.
3. Write a C program to check whether a number is even or odd using
switch case.
4. Write a C program to check whether an alphabet is vowel or
consonant using switch case.
5. Write a C program to find roots of a quadratic equation using switch
case.
6. Write a C program to create Simple Calculator using switch case.
W3schools.in, Tutorialspoint.com
UNIT-II Conditional Statements in C Geeksforgeeks.org, codeforwin.org
Exercise: switch Statement
Write a C program to print day of week name using switch case.
#include <stdio.h>
void main()
{
int week; /* Input week number from user */
printf("Enter week number(1-7): ");
scanf("%d", &week);
switch(week)
{
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
W3schools.in, Tutorialspoint.com
UNIT-II Conditional Statements in C Geeksforgeeks.org, codeforwin.org
Exercise: switch Statement
Write a C program to print day of week name using switch case.
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
case 7:
printf("Sunday");
break;
default:
printf("Invalid input! Please enter week number between
1-7.");
} // End of Switch Statement
getch();
} // End of Program (main Function)
W3schools.in, Tutorialspoint.com
UNIT-II Conditional Statements in C Geeksforgeeks.org, codeforwin.org
Exercise: switch Statement
Write a C program to check whether an alphabet is vowel or consonant using switch case.
#include <stdio.h>
void main()
{
char ch; // Input an alphabet from user
printf("Enter any alphabet: ");
scanf("%c", &ch);
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u': printf("Vowel");
break;
W3schools.in, Tutorialspoint.com
UNIT-II Conditional Statements in C Geeksforgeeks.org, codeforwin.org
Exercise: switch Statement
Write a C program to check whether an alphabet is vowel or consonant using switch case.
case 'A':
case 'E':
case 'I':
case 'O':
case 'U': printf("Vowel");
break;
default: printf("Consonant");
} // End of Switch
getch();
} // End of program
W3schools.in, Tutorialspoint.com
UNIT-II Conditional Statements in C Geeksforgeeks.org, codeforwin.org
Exercise: switch Statement
Write a C program to check whether a number is even or odd using switch case.
#include <stdio.h>
void main()
{
int num; // Input a number from user
printf("Enter any number to check even or odd: ");
scanf("%d", &num);
switch(num % 2) // If n%2 == 0
{
case 0: printf("Number is Even");
break;
case 1: printf("Number is Odd");
break;
} //End of Switch
getch();
} // End of program
W3schools.in, Tutorialspoint.com
UNIT-II Conditional Statements in C Geeksforgeeks.org, codeforwin.org
Exercise: switch Statement
Write a C program to create Simple Calculator using switch case.
#include <stdio.h>
void main()
{
char op;
float num1, num2, result=0.0f;
printf("WELCOME TO SIMPLE CALCULATOR\n");
printf("----------------------------\n");
printf("Enter [number 1] [+ - * /] [number 2]\n");
scanf("%f %c %f", &num1, &op, &num2);
switch(op)
{
case '+': result = num1 + num2;
break;
case '-': result = num1 - num2;
break;
W3schools.in, Tutorialspoint.com
UNIT-II Conditional Statements in C Geeksforgeeks.org, codeforwin.org
Exercise: switch Statement
Write a C program to create Simple Calculator using switch case.
W3schools.in, Tutorialspoint.com
UNIT-II Conditional Statements in C Geeksforgeeks.org, codeforwin.org
Exercise: Find the output
#include <stdio.h>
void main()
{ char ch='c'; int a=8, b=7, c=0, d;
switch(ch) {
default: printf("all the values entered are wrong");
case 'a': d=a||b?c&&b?40:60:30;
printf("\n value of d is::%d",d);
case 'b': if( printf(""))
printf("hello world");
else
printf("bye");
case 'c': break;
}
W3schools.in, Tutorialspoint.com
UNIT-II Conditional Statements in C Geeksforgeeks.org, codeforwin.org
THANK YOU
W3schools.in, Tutorialspoint.com
UNIT-II Conditional Statements in C Geeksforgeeks.org, codeforwin.org