Top Banner
CONTROL STRUCTURE TOPIC 5
39
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript

CONTROL STRUCTURETOPIC 5Sequential Control StructureNormally each activity involve 3 phases:- input, process, output.INPUT ---------->PROCESS ----------> OUTPUT (data)(data manipulation) (information)But we also have activities that does not require inputSequential structure has a series of instructions that will executed one y one follows its sequence!he most importantin sequential structure is arran"ement # sequence of instructions that form the al"orithm$n calculatin" the total of 3 numers, statements involve are input statement, output statement and assi"nment statement. !o solve this prolem, we arran"e the instructions as follows:start display Enter tree num!ers " input no#$ no%$ no& total no# ' no% ' no& output Te total of tree num!ers is $ totalend$t is also can e written usin" numer as follows:#( start%. display Enter tree num!ers " &( input no#$ no%$ no&)( total no# ' no% ' no&*( output Te total of tree num!ers is $ total+( end$n example of calculatin" an area of a circle, there are two assi"nment statements:&i' PI &(#)%&ii' area PI , radius , radius(t &ii', content of variale area depend on the result of arithmetic operation &content of variales PI and radius')eans, statement &i' must written efore statement &ii'So, the al"orithm for this prolem:#( start%( output Enter a radius of a -ir-le &( input radius)( PI &(#)%*( area PI , radius , radius+( output Te area of a -ir-le is $ area.( end*xample : Calculate area of two circles#( start%( output Enter radius for t/o -ir-les &( input radius#$ radius%)( PI &(#)%*( area# PI , radius# , radius#+( area% PI , radius% , radius%.( output Te area of first -ir-le is $ area#0( output Te area of se-ond -ir-le is $ area%1( end+oo, at the statement line - and .. !he calculation of area# and area% are undependale on each other. So, N/! an error if we chan"e the position of statement - and . as follows:#( start%( output Enter radius for t/o -ir-les &( input radius#$ radius%)( PI &(#)%*( area% PI , radius% , radius%+( area# PI , radius# , radius#.( output Te area of first -ir-le is $ area#0( output Te area of se-ond -ir-le is $ area%1( endBut statement 0 must written efore calculatin" the area# and area% ecause these values depend on variale PI*xercise: &a' Calculate the circumference and area of a circle.&' 1ou will e "iven a numer. $dentify the result and remainder when that numer divided with 3&c' $dentify the len"th etween % coordinate point 2 &x3,y3' and 1 &x%, y%'. !he len"th can e calculated usin" the followin" formula:&d' Calculate the total cumulative investment at the end of the first year when an investor invest some money at early of a year. !he interest rate is 30.-4 per year. !otal cumulative is total investment added with interest received y the investor.Selection Control Structuresolutions that we can choose ased on the true condition also ,nown as a !ran- ( condition is when control flow reaches a point where it can proceed to one of two &or more' alternatives !he condition is what determines which step to proceed to 5hen your al"orithminvolve conditional, we must familiar first with the operator two operators involved which are relational operator and lo"ical operator 6elational /perator - is the relationship which values can have with one another.Operator Meaning7 8reater than79 8reater than or equal: +ess than:9 +ess than or equal99*qual to;9 Not equal+o"ical /perator - is the way that the relationships can e connected.Operator Meaning > /6; N/!Statements are executed ased on the conditions. !here are three types of selection:3. if selection%. if#else selection3. multiway selection &nested if'If selectionwill performs an action if a condition is !6?* or s,ip if the condition is @(+S*.Syntax:if 2-ondition> statement*xample 3: =isplay the statement A)(+*B if the "ender is male.conditiontruefalsestatementSolution: Crolem =efinition:Input" 3enderOutput"4isplay 567E8 if 3ender is male(l"orithm:startInput 3enderif (3ender 9 9 male8) tendisplay 567E8 endgender ==male?truefalseInput genderDisplay MALE*xample %: ( direct sellin" company "ives the commission to their employees re"ardin" on sales have een done. Sales Commission79 3DDDD 3D479 -DDD and : 3DDDD -4: -DDD 345rite an al"orithm to calculate the commission for the one employee. Solution:Crolem =efinition:Input" Sales Output" -ommission:ormula"Commission 9 Sales , ;(# ifSales > 9 #;;;; Commission 9 Sales , ;(;* if *;;; 29 Sales 2#;;;; Commission 9 Sales , ;(;& if Sales 2 *;;;(l"orithm:start output Enter sales " R5 input SalesIf Sales > 9 #;;;; tenCommission Sales , ;(#If *;;; 29 Sales 2#;;;;Commission Sales , ;(;*If Sales 2 *;;;Commission Sales , ;(;& outputTe -ommission is R58$ CommissionendInput SalesSales >=1 ?truefalsefalsetruefalsetrue!ommission = Sales " #1$%=Sales %1 ?!ommission = Sales " #$Sales %$ ?!ommission = Sales " #&'utput ()e commission is *M + !ommissionIf/else Selectionwill perform an action from two conditionsStatement3 will e executed if the condition is !6?* or Statement% will e executed if the condition is @(+S*.Syntax:if 2-ondition>statement#elsestatement%conditionstatement1falsetruestatement,*xample 3: =isplay the statement A)(+*B if the "ender is male, statement A@*)(+*B if the "ender is female.Solution: Crolem =efinition:Input" 3enderOutput" 4isplay 567E8 if 3ender is maleor 8:E567E8 if 3ender is female(l"orithm:startdisplay Enter 3ender 5 or : input 3enderif (3ender 9 9 :ormula"ta> 9 salary , ;(;% if salary 2);;;ta> 9 salary , ;(+* if salary >9);;;(l"orithm:startdisplay Enter salary " R5 input salaryIf salary 2 );;; ta> salary , ;(;%else ta> salary , ;(;+*endIfdisplay Ta> is " $ ta>endsalary % 0?falsetrueInput salaryDisplay (a1 is 2 + ta1ta1=salary " #,ta1=salary " #3$Display Enter salary 2 *M*xample 0: 5rite an al"orithm in which the user enters their salary and their a"e. $f the person is over E-, increase their salary y 3D4 of their salary./therwise, increase their salary y -4 of their salary. =isplay the new salary in either case.Solution:Crolem =efinition:Input" salary$ a3e Output" ne/ salary :ormula " ne/ salary 9 salary ' (salary , ;(#;) if a3e > .* ne/ salary 9 salary ' (salary, ;(;*) if a3e 2 9 .*(l"orithm:startoutput Enter salary and a3e input salary$ a3eIf a3e > .*ne/?salary salary ' ( salary , ;(#;)elsene/?salary salary ' (salary , ;(;*)endIfoutputNe/ salary is R5 $ ne/?salaryendage > 4$?falsetrueInput salarydisplay 5e6 salary is *M + ne67salaryne67salary = salary 8 salary " #1ne67salary = salary 8 salary " #$Input ageDisplay Enter salary and age )ultiway selection &Nested if'Nested if is the if statement inside other if statements or else statements, and so on. *xample 3: =isplay the status of a student ased on their mar, elow:Mark Status79 E- *xcellent-D :9 mar, : E- )oderate: -D @ailSolution:(l"orithm:startInput mar@If mar@ 2 *;Status fail8 ElseIf mar@ 2 .*Status 5oderate8 ElseStatus E>-ellent8 endIfOutput StatusendInput mar9mar9 % $mar9 % 4$display StatusStatus = /ailStatus = ModerateStatus = E1cellenttruetruefalsefalse*xample %: @ind and display the minimum numer of 3 numers.Solution:Crolem =efinition:Input" & num!ersOutput "minimum num!er(l"orithm:startInput no#$ no%$ no&If (no# 2 no%) and (no# 2 no&)output no# !lo-@ #else if (no%2no#) and (no% 2 no&)output no%else !lo-@%output no&endifendNote" Ae -an see te ifBelse statement in te se-ond !lo-@(*xample 3: @ind the "rade of a student. !he ran"e of input is D F 3DD.Mark GradeD F 0G @-D F .0 C.- F EG BHD F 3DD (Solution:Crolem =efinition: Input " mar@Output" 3rade(l"orithm:startInput mar@If mar@ 2 *;Crade