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

Conditional Statements

Uploaded by

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

Conditional Statements

Uploaded by

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

Control Statements

Control statements enable us to specify the flow of program control; i.e., the order in which the
instructions in a program must be executed. They make it possible to make decisions, to perform
tasks repeatedly or to jump from one section of code to another.
Three main types of control flow in programming
D
I
S
C
O
U NO
N
T

ARE YOU ELIGIBLE?????


Branching Statements : Conditional

if

if else
Decision
Making
Control Nested if else
structures

else if ladder

switch
Simple if statement
❖ It is a one way selection statement.
❖ If the Test Condition is true then true block gets executed.
Program to read the shopping amount and apply delivery charges if
the amount is less than 500.
if else
❖ It is a two way selection statement.
❖ If the Test Condition is True then true i.e. if block get executed.
❖ If the Test Condition is False then else block gets executed.
❖ In short either True block gets executed or False block gets executed not both
Program to read age and check if the candidate is eligible to Vote
or not
Program to read num & check if the given number is Even or Odd

Condition is checked twice


Program to find largest of the given two numbers
Any item purchased in Flipkart would require Rs.50/- as delivery charges if the
purchase_amount is less than Rs.500/-. Write a C Program that takes
purchase_amount as input and calculate the total bill to be paid and print
appropriate messages .
A 20% hike is given to the employees whose experience is more than 8 years.
Write a program which reads experience and current salary of the employee as
input and print appropriate messages
Write a program to check if the given alphabet is vowel or not
HOME WORK
1. Write a program to read the Basic_Salary of employee as input
and then Calculate his Gross_Salary, with following details,
– if the Basic_Salary of employee is less than 8000, then,
HRA = 10% of Basic
DA = 90% of basic
– if the Basic_Salary of employee is more than 8000, then,
HRA = 500
DA = 98% of basic
Note: Gross_Salary is found by the formula,
Gross_Salary =Basic + DA + HRA
2. Write a C program to read a character and check if the given
character is vowel or not.
Nested if else

The general form of


nested if else
Nested if else
Write a program to read a number and check if it is positive negative
or zero
Write a program to read three numbers and find the largest of the
three numbers.
Write a program to read x,y coordinate values and find in which
Quadrant the point lies
else if ladder
• It is a multi-way selection General form of else if ladder
statement.
• When we have multiple options
available or we need to take
multiple decisions based on
available condition, we can use
another form of if statement
called else…if ladder.
• If condition becomes true then
the associated block with if
statement is executed and rest of
conditions are skipped.
• If the condition becomes false
then it will check for next
condition in a sequential manner.
• If all condition available in else…if
ladder evaluated to false then
default else block will be
executed.
else if ladder
Program to find the largest of the given three numbers
Write a program to read x,y coordinate values and find in which
Quadrant the point lies
Write a program to read a number and check if it is positive negative
or zero
Write a program to read total marks scored by the student in exam
and find the grade of the student.
HOME WORK

Write a program to calculate tax collection according to


the following formula:
5% Tax, if salary is above 50000
3% Tax , if salary is between 30000 and 50000
2% Tax, is salary is less than 30000
In the end the program should print the calculated tax.
Switch statement
It is simplified multi-way selection 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.

The following rules apply to a switch statement −


• The expression used in a switch statement must have an integral or
enumerated type, or be of a class type in which the class has a single
conversion function to an integral or enumerated type.
• You can have any number of case statements within a switch. Each case is
followed by the value to be compared to and a colon.
• The constant-expression for a case must be the same data type as the
variable in the switch, and it must be a constant or a literal.
• When the variable being switched on is equal to a case, the statements
following that case will execute until a break statement is reached.
• When a break statement is reached, the switch terminates, and the flow
of control jumps to the next line following the switch statement.
• Not every case needs to contain a break. If no break appears, the flow of
control will fall through to subsequent cases until a break is reached.
• A switch statement can have an optional default case, which must appear
at the end of the switch. The default case can be used for performing a
task when none of the cases is true. No break is needed in the default
case.
Program to print the given number in words
Program for Simple Calculator
Program to read an alphabet and check if it is vowel

Program works for only lower case letters


• include <stdio.h>

• int main()
• {
• int wDay;

• printf("Enter weekday number (0-6): ");
• scanf("%d",&wDay);

• switch(wDay)
• {
• case 0:
• printf("Sunday");
• break;
• case 1:
• printf("Monday");
• break;
• case 2:
• printf("Tuesday");
• break;
• case 3:
• printf("Wednesday");
• break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
default:
printf("Invalid weekday number.");
}
printf("\n");
return 0;
}
• C Program to Find the Roots of a Quadratic
Equation
#include <math.h>
#include <stdio.h>
int main() {
double a, b, c, discriminant, root1, root2, realPart, imagPart;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf", &a, &b, &c);

discriminant = b * b - 4 * a * c;

// condition for real and different roots


if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("root1 = %.2lf and root2 = %.2lf", root1, root2);
}
// condition for real and equal roots
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("root1 = root2 = %.2lf;", root1);
}

// if roots are not real


else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart, realPart,
imagPart);
}

return 0;
}

You might also like