Upload
cmdoromal
View
222
Download
0
Embed Size (px)
Citation preview
8/18/2019 Program Elements and Structure
http://slidepdf.com/reader/full/program-elements-and-structure 1/31
PROGRAM ELEMENTS ANDSTRUCTURE
8/18/2019 Program Elements and Structure
http://slidepdf.com/reader/full/program-elements-and-structure 2/31
Objectives
To know the basic elements that is used to consal!oithms
To undestand the natue o" each elements To detemine the basic stuctue that will be use
"omulate al!oithm
8/18/2019 Program Elements and Structure
http://slidepdf.com/reader/full/program-elements-and-structure 3/31
Program Elements
Thee basic elements o" al!oithm ae as "ollows Constants $aiables E%&essions
8/18/2019 Program Elements and Structure
http://slidepdf.com/reader/full/program-elements-and-structure 4/31
Constant
A liteal 'alues that does not chan!e duin! &o!ae%ecution( )t is *%ed 'alue
8/18/2019 Program Elements and Structure
http://slidepdf.com/reader/full/program-elements-and-structure 5/31
Types of Constant
+( Numeic Constant , ma- be an inte!e o ea
Chaacteistics
a( Commas and blank s&aces cannot be included wiconstant
b( The constant can be &eceded b- a minus si!n
c( The 'alue o" constant cannot e%ceed s&eci*ed and ma%imum bounds( .o each t-&e o" constant/ theswill 'a- "om one com&ile o" a &o!am to anothe
8/18/2019 Program Elements and Structure
http://slidepdf.com/reader/full/program-elements-and-structure 6/31
Types of Numeric Constant
+(+ )nte!e Numeic Constant
, a whole numbe/ it has an e%act 'alue and doecontain decimal &oint
E%am&le#
The "ollowin! ae ille!al numeic constant
+0/111 ille!al chaacte 2/3
+4 56 78 ille!al chaacte 2blank s&ace3
587,+11,544 ille!al chaacte 2,3
90 -17005000
8/18/2019 Program Elements and Structure
http://slidepdf.com/reader/full/program-elements-and-structure 7/31
Types of Numeric Constant
+(5 Real Numeic Constant
, numbe that contains a decimal &oint o an e%both:( )t has a&&o%imate 'alue
E%am&le#
The "ollowin! ae not 'alid eal constant
51 Eithe a decimal &oint o an e%&onent must b
+/546(15 ille!al chaacte 2/3
3.30 415.05
8/18/2019 Program Elements and Structure
http://slidepdf.com/reader/full/program-elements-and-structure 8/31
Types of Constant
5( Chaacte Constant
, a sin!le chaacte/ enclosed in an a&osto&he ;uotation maks:
E%am&le# ‘F’ ‘w’ ‘7’ ‘*’ ‘’
8/18/2019 Program Elements and Structure
http://slidepdf.com/reader/full/program-elements-and-structure 9/31
Types of Constant
7( Stin! Constant
, consist o" an- consecuti'e chaacte enclosed;uotation maks
E%am&le# “Fee”“wi” “790”
8/18/2019 Program Elements and Structure
http://slidepdf.com/reader/full/program-elements-and-structure 10/31
Variables
)s a &otion o" the com&ute<s main memo- use
a numeic o a stin! constant )t e&esents a cetain 'alue The 'alue stoed in the 'aiable ma- be chan!e
&o!am e%ecution
)t can onl- hold one 'alue at a time
8/18/2019 Program Elements and Structure
http://slidepdf.com/reader/full/program-elements-and-structure 11/31
Rules in Defining a Variable Name
Must contain onl- lettes/ numbes and=o an undescoe 2>3
Must be!in with a lette Undescoe must not a&&ea consecuti'el-
Must be uni;ue
Must not be a ke-wod o ese'ed wod o" an- &o!ammin! la
E%am&le#
The "ollowin! ae not 'alid
NO STUD ille!al chaacte 9blank s&ace:
6ct does not stat with a lette
MA>>LE undescoe a&&eas consecuti'el-
S Sum Num1F_Name
8/18/2019 Program Elements and Structure
http://slidepdf.com/reader/full/program-elements-and-structure 12/31
Types of Variables
+( Numeic $aiable
, contain data o hold numeic 'alue onl-
T-&es o" Numeic $aiable
a( )nte!e Numeic $aiable ? holds inte!e 'alue on
b( Real Numeic $aiable ? holds eal 'alue onl-
5( Chaacte $aiable, contain chaacte 'alue onl-
7( Stin! $aiable
, contain o holds stin! 'alues onl-
8/18/2019 Program Elements and Structure
http://slidepdf.com/reader/full/program-elements-and-structure 13/31
Expressions
A !ou& o" &o!am elements consistin! o" o&e
o&eatos O&eands ? can be a constant o 'aiable O&eatos ? can be an aithmetic and elational
E%&essions alwa-s -ield a 'alue o" a cetain t-&de&endin! on the t-&e o" e%&essions
T-&es o" e%&essions Aithmetic E%&essions Stin! E%&essions
@oolean E%&essions
8/18/2019 Program Elements and Structure
http://slidepdf.com/reader/full/program-elements-and-structure 14/31
Aritmetic Expressions
An e%&ession consistin! o" numeic constants o
'aiables se&aated b- an aithmetic o&eato w-ields a numeic 'alue
The !eneal "omat o" a numeic e%&ession is a
E%am&le#
operand operator operand opoperand
4 * !
4 and ! are operand"* #perator $%u&tip&i'ationS(m)o&
8/18/2019 Program Elements and Structure
http://slidepdf.com/reader/full/program-elements-and-structure 15/31
Aritmetic Operators
Operator Purpose
+ Addition
- Subtaction
* Multi&lication
/ Di'ision
% Modulus Di'isionModulus Di'ision ? di'ision in which etuns the emainde &otion
10 + 3 , 19+ 10 ,
17 + ! , 1
8/18/2019 Program Elements and Structure
http://slidepdf.com/reader/full/program-elements-and-structure 16/31
Aritmetic Expressions
ow to detemine the t-&e o" an aithmetic e%&
+( Combinin! somethin! o" t-&e rea& withsomethin! o" t-&e eithe inteer o rea& aesults to rea&5( Combinin! an-thin! with di'ision si!n alw-ields to rea&
7( Combinin! two thin!s o" t-&e inteer eiaddition 9B:/ subtaction9,: o multi&lication-ields to inteer t-&e8( Placin! a minus si!n 9,: in "ont o" anaithmetic e%&ession does not chan!e its t
8/18/2019 Program Elements and Structure
http://slidepdf.com/reader/full/program-elements-and-structure 17/31
Aritmetic Expressions
Real and inte!e will -ield eal 'alue
Di'ision will -ield eal 'alue
)nte!e Di'ision 9it tuncates the emainde &at o" the ;
!.0 / 1 , 3.0!.0 1 , !.0!.0 - 1 , 1.0!.0 * 1 , !.0
!.0 1.0 , !.0!.0 1 , !.0
! ! , 1
5 ! , !
8/18/2019 Program Elements and Structure
http://slidepdf.com/reader/full/program-elements-and-structure 18/31
Complex Aritmetic Expressions
Numeic e%&essions can be mi%ed to contain se
o&eands and o&eatos( Paenthesis can be used to !ou& a cetain &oti
e%&ession
/ ! !, / ! !, / 1.0, 7.0
$3 / 4 *10 , $3 / 4 *10, 7 *10, 70
10 * 4 * , 10 * 4
!.0, 40 !.0,
8/18/2019 Program Elements and Structure
http://slidepdf.com/reader/full/program-elements-and-structure 19/31
Prece!ence Rules Aritmetic Expression
Ode o" e'aluation
+( )" &aenthesis ae &esent/ then the- detemthe ode o" o&eation5( )" the ode is not detemine the &aenthesis/ =/ o&eations ae e'aluated *st be"oe B
7( )" thee is still the ;uestion o" which o&eatiodo *st/ the com&ellin! o&eations in thei le"t i!ht ode
8/18/2019 Program Elements and Structure
http://slidepdf.com/reader/full/program-elements-and-structure 20/31
Aritmetic Expression " Or!er of Evaluation1. !0 4 * 9 +3
, !0 4 * 9 +
3 , 5 * 9 +3
, 45 +3 , 0
!. 5 * 3 5 * 10 - !
, 5 * 3 5 * 10 - ! , 15 5 * 10 - , 3 * 10 - !0 , 30 - !0 , 30 2 40 , -10
3. - $15 2 / 10 *$ 11 / , - 7 / 10 *$ 11 / , - 7 / 10 *17
, -7 / 170
4. 5 * $1! * 10 $!5 , 5 * $1! * 10 5 , 5 * $1!0 5 , 5 * !4 , 1!0
8/18/2019 Program Elements and Structure
http://slidepdf.com/reader/full/program-elements-and-structure 21/31
#tring Expression
Consists o" stin! constants o stin! 'aiables se&aated b- o&eato which -ield to stin! 'alue
The !eneal "omat o" a stin! e%&ession is as "ollows#
E%am&le#
Stin! e%&ession use onl- one o&eato/ the B o&eato( The eeco&eato on stin! is concatenation
Concatenation , a&&endin! the 'alue o" the stin!
operand operator operand operoperand
, “e"u" ” , “wi&& ”6 , “uide u"” ,‘ ’ , / / 6
, “e"u" wi&& uu"”
8/18/2019 Program Elements and Structure
http://slidepdf.com/reader/full/program-elements-and-structure 22/31
$oolean Expression
E%&ession which -ields eithe TRUE9+: o .ALSE Eithe a Relational E%&ession o Lo!ical E%&ess
8/18/2019 Program Elements and Structure
http://slidepdf.com/reader/full/program-elements-and-structure 23/31
Relational Expressions
@oolean e%&ession consistin! o" numeic o sti
e%&essions/ numeic o stin! 'aiables and numstin! constant se&aated b- elational o&eato
The !eneal "omat o" a elational e%&ession is "ollows#
@oth o" the o&eand must be o" the same t-&e/ teithe both numeic o both stin!
The o&eand can be a numeic o stin! e%&essnumeic o stin! 'aiable o numeic o stin! c
operand operator operand
8/18/2019 Program Elements and Structure
http://slidepdf.com/reader/full/program-elements-and-structure 24/31
Relational Operator
Operator Purpose
> Geate than
>= Geate than e;ual to
< Less than
<= Less than o e;ual to
<> Not e;ual to
8/18/2019 Program Elements and Structure
http://slidepdf.com/reader/full/program-elements-and-structure 25/31
Relational Expressions
Rules in e'aluatin! Relational e%&essions#+( Fhen e'aluatin! a elational e%&ession/ the 'alu
o&eand is com&aed to the 'alue o" the i!ht o&eadetemine whethe the entie e%&ession is tue o "5( )" the o&eand is 'aiable/ then coes&ondin! 'alused "o com&aison7( )" the o&eand is a numeic o stin! e%&ession/ t
e%&ession is e'aluated *st to detemine its *nal 'abe used "o com&aison8( )" a &otion o" numeic o stin! e%&ession is 'a
its coes&ondin! 'alue will be used to ai'e at *the said e%&ession
4( Fhen com&ain! stin! 'alues/ each chaacte o
8/18/2019 Program Elements and Structure
http://slidepdf.com/reader/full/program-elements-and-structure 26/31
Relational Expression
8ate , 100
ota&_6S , 50ota&_: , 46omte'; , 40Name" , “#< #N =>”
8e&ationa& ?@pre""ion :nterpretation
8ate A 5ota&_6S B A ota&_:ota&_6S B A 40Name" B A “rnie”“6S” A “CDC”
“#E” , “do”
8D?
8D?
8D?
8D?FS
?FS
8/18/2019 Program Elements and Structure
http://slidepdf.com/reader/full/program-elements-and-structure 27/31
%ogical Expressions
An e%&essions consistin! o" one o moe elatio
e%&essions se&aated b- the lo!ical o&eato theithe TRUE o .ALSE
The "ollowin! ae the lo!ical o&eatos# NOT AND OR
8/18/2019 Program Elements and Structure
http://slidepdf.com/reader/full/program-elements-and-structure 28/31
%ogical Expressions
To e'aluate lo!ical e%&ession/ tuth table must
N# rut; a)&eN# rue ,FS?N# Fa&"e ,8D?
N rut; a)&erueN rue , 8D?rueN Fa&"e , FSFa&"e N rue ,FS?Fa&"e N Fa&"e ,FS?#8 rut; a)&e
rue#8 rue , 8D?rue#8 Fa&"e , 8D?Fa&"e #8 rue , 8D?
Fa&"e #8 Fa&"e ,
8/18/2019 Program Elements and Structure
http://slidepdf.com/reader/full/program-elements-and-structure 29/31
%ogical Expression
Conside the "ollowin! 'aiables and its 'alue#6S , 15: , 0N%? , “=6”< , 500
8D?8D? N
,8D?
1. $6S A,10 N $6SB,!0
!. $: , 1 #8 $: ,!
FS?
FS?
#8
, FS?
3. N# $N%? ,
FS?N#,8D?
4. $<A 500 #$<B400
FS?
#8
FS? #8,8D
8/18/2019 Program Elements and Structure
http://slidepdf.com/reader/full/program-elements-and-structure 30/31
&ierarcy or Priority of %ogical Operator
E%&essions in &aenthesis .ist
NOT Second
AND Thid
OR .outh
8/18/2019 Program Elements and Structure
http://slidepdf.com/reader/full/program-elements-and-structure 31/31
%ogical Expression
Conside the "ollowin! 'aiables and its 'alue#
, 1N%? , “#N”H6 , 100
1. $,1 #8 $,! #8$,38D? FS
?FS?
#8
#88D? #8
FS?,8D
?
!. $N%? , “#N” N N# $$A,50N$B,1508D? FS
?8D?N$
N#
N $FS
?
N#
8D? N
8D?N8D?,
3. $N%? ,“#N” N $H6A,508D? 8D?N
8D? #
,8D?