Вы находитесь на странице: 1из 17

FLOW OF CONTROL

Selection statements
if statement
The if statement is used to test a particular condition and if the
condition is true, it executes a block of code known as if-block.
if-else statement
The if-else statement provides an else block combined with the if statement
which is executed in the false case of the condition.
If the condition is true, then the if-block is executed. Otherwise, the else-block
is executed.
Ladder if’s statement

MAINGATE

GATE OPEN GATE OPEN


WHEN VALUE WHEN VALUE
1 2

GATE1 BROWN GATE1 RED GATE1 GREEN GATE2 BROWN GATE2 RED GATE2 GREEN
OPEN OPEN OPEN OPEN OPEN OPEN
Q1. Take values of length and breadth of a rectangle from user and
check if it is square or not.

Q2. A company decided to give bonus of 5% to employee if his/her year


of service is more than 5 years.
Ask user for their salary and year of service and print the net bonus
amount.

Q3. Write a program to check whether a number is negative,


positive or zero.
program to calculate profit or loss

Logic to find profit or loss


In primary mathematics classes, you all have learned about profit and loss. If
cost price is greater than selling price then there is a loss otherwise profit.
Formula to calculate profit and loss
Profit = S.P - C.P (Where S.P is Selling Price and C.P is Cost Price)
Loss = C.P - S.P
Write a program to input all sides of a triangle and check whether triangle is
valid or not.
Property of triangle

A triangle is valid if sum of its


two sides is greater than the
third side. Means if a, b, c are
three sides of a triangle. Then
the triangle is valid if all three
conditions are satisfied
a+b>c
a+c>b
b+c>a
Write a program to input angles of a triangle and check whether triangle
is valid or not.

Property of a triangle

A triangle is said to be a valid triangle if and only if sum of its


angles is 180 °
Logic to check triangle validity if angles are given

Step by step descriptive logic to check whether a triangle can be formed or not, if angles are
given.
1.Input all three angles of triangle in some variable say angle1, angle2 and angle3.
2.Find sum of all three angles, store sum in some variable say sum = angle1 + angle2
+ angle3.
3.Check if(sum == 180) then, triangle can be formed otherwise not. In addition,
make sure angles are greater than 0 i.e. check condition for angles if(angle1 != 0 &&
angle2 != 0 && angle3 != 0)
program to check whether triangle is equilateral, scalene or isosceles

Properties of triangle
•A triangle is said Equilateral Triangle, if all its sides are equal. If a,
b, c are three sides of triangle. Then, the triangle is equilateral
only if a == b == c.
•A triangle is said Isosceles Triangle, if its two sides are equal. If a,
b, c are three sides of triangle. Then, the triangle is isosceles if
either a == b or a == c or b == c.
•A triangle is said Scalene Triangle, if none of its sides are equal.
Logic to check equilateral, scalene or isosceles triangle

Step by step descriptive logic to classify triangle as equilateral,


scalene or isosceles triangle.
1.Input sides of a triangle from user. Store it in some variables say
side1, side2 and side3.

2.Check if(side1 == side2 && side2 == side3), then the triangle is


equilateral.

3.If it is not an equilateral triangle then it may be isosceles. Check


if(side1 == side2 || side1 == side3 || side2 == side3), then triangle
is isosceles.

4.If it is neither equilateral nor isosceles then it scalene triangle.


program to count total number of notes in given amount

Logic to count minimum number of denomination for


given amount
There any many optimal algorithms to solve the given
problem. For this exercise to make things simple I
have used greedy algorithm.
Step by step descriptive logic to find minimum
number of denomination.
1.Input amount from user. Store it in some variable
say amt.
2.If amount is greater than 500 then, divide amount
by 500 to get maximum 500 notes required. Store
the division result in some variable say
note500 = amt / 500;
After division, subtract the resultant amount of 500
notes from original amount. Perform
amt = amt - (note500 * 500).
3.Repeat above step, for each note 200, 100, 50, 20,
10, 5, 2 and 1.
if user input: 1250
then
variable note500 = amt/500 means

note500=1250//500 = 2

next is
amt=amt - (note500 * 500 )
amt = 1250 -(2 * 500 )
amt = 1250 -(1000)
amt = 250

next is repeat all the steps


if – elif - else statement
The elif statement allows you to check multiple expressions for TRUE
and execute a block of code as soon as one of the conditions
evaluates to TRUE.

Syntax:

if expression1:
statement(s)
elif expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)
Write a program to input month number and print number of days in that
month.

Month Total days


January, March, May, July,
31 days
August, October, December
February 28/29 days
April, June, September,
30 days
November

Write a program to input week number and print week day.

Вам также может понравиться