002-1 Programming Basics

Embed Size (px)

Citation preview

  • 8/9/2019 002-1 Programming Basics

    1/30

    WEB202D

    WebEngineeringPROGRAMMING BASICS

    1

  • 8/9/2019 002-1 Programming Basics

    2/30

    Recap◦Static Vs D!na"ic Website#

    ◦Webser$er% Br&'ser% ())P*())PS#

    ◦Ser$er+si,e -ang.age $s C/ient+si,e -ang.age#

    2

  • 8/9/2019 002-1 Programming Basics

    3/30

    O.t/ineoC&""entso Variab/e

    o C&nstant

    o Data t!pe

    o Operat&rs an, epressi&ns

    o /&' c&ntr&/ .ncti&n

    o .ncti&no Arra!

    3

  • 8/9/2019 002-1 Programming Basics

    4/30

    St&p44444555o Pr&p&sa/ s.b"issi&n ,ea,/ine6 7 Septe"ber 2018

    8

  • 8/9/2019 002-1 Programming Basics

    5/30

    C&""entso Comment is .se, &r .n,erstan,ing t9e c&,ing '9i/e t9e c&,es is g&ing t&gr&'

    o C&""ent:s;6 a pieces & c&,e t9at t9e P(P parser ign&res

    oOne-line comment 6 en, at t9e en, & /ine ** &r Na"[email protected]'a!

    #

    oMulti-line comments 6 "&re t9an &ne /ine * *

    oEa"p/e6 * )9is is a c&""ent &n 4

    444

    444*

  • 8/9/2019 002-1 Programming Basics

    6/30

    Variab/eo Variable 6 a /&cati&n &r st&ring a piece & ,ata in "e"&r!oWe are g&ing t& "anip./ate t9e" in &.r pr&gra"s

    oR./es re/ate, t& $ariab/es6

    oM.st start 'it9 > sign

    oCase sensiti$e

    o )!pe /ess

    oCan inc/.,e /etters% n."bers an, .n,ersc&re :F; c9aracter% b.t n& space

    oNa"e ".st begin 'it9 .n,ersc&re &r /etter

    oNa"e & $ariab/e s9&./, be "eaning./ :>na"e ? @23 >na"e ? @&9n;

    oEa"p/e6 in, t9e c&rrect &ne:s;# C&./, !&. c&rrect t9e"#

    o FNa"e?@&9n >20a"i/!na"e?@&9ns&n >F.ni$ersit!na"e?S.n'a!

    o>A,,ress ?@Peta/ing a!a >F,.e ,ate?@2018

    H

  • 8/9/2019 002-1 Programming Basics

    7/30

    C&nstanto )9e $a/.e is re"aine, .nc9ange,oDene:@c&nstantna"e%c&nstant $a/.e;

    oEa"p/e 6

    Dene:@Jni$ersit!na"e% S.n'a! Jni$ersit!;

    K

  • 8/9/2019 002-1 Programming Basics

    8/30

    Data t!peo Again% P(P is t!pe /ess55 B! ,ea./t is ,&.b/e

    oChanging type6 p.t na"e & ,ata t!pe in r&nt & $ariab/ebet'een parent9eses

    o

    Ea"p/e6>Age?@13H ** ,&.b/e  13H

    Ec9& gett!pe:>Age; **D&.b/e

    >Age?integer:>Age; **Integer  13

    7

  • 8/9/2019 002-1 Programming Basics

    9/30

    Operat&rs an,

    epressi&nso Operators are s!"b&/s t9at "aLe t9is p&ssibi/it! t& "aLe ne'$a/.e

    o Operand is a $a/.e .se, in c&n.ncti&n 'it9 &perat&r

    o Ea"p/e6 '9ic9 &ne is &peran, an, &perat&rs#

     8

    o Expression is a combination & &peran,s an, &perat&rs t&"aLe t9e res./t

    o Assignment operator 6 taLes t9e $a/.e & its rig9t+9an,&peran, an, assigns it t& its /et+9an, &peran,

    o Ea"p/e 6

    >na"e?@&9n

  • 8/9/2019 002-1 Programming Basics

    10/30

    Operat&rs an,

    epressi&nso Arithmetic operators6 ,& arit9"etic &perati&ns &n&peran,s

    oConcatenation operator 6 a sing/e ,&t :; '9ic9 is

    .sing &r b&t9 string &peran,s t& &in t9e"

    o Ea"p/e6

    Ec9& @)9is is a test

    10

  • 8/9/2019 002-1 Programming Basics

    11/30

    Operat&rs an,

    epressi&nso Incrementing and decrementing 6 t& increase &r,ecrease t9e integer a.t&"atica//!% especia//! in iterati&n

     Ea"p/e6>?

    >?> **Incre"ent

    >?>++ **Decre"ent

    &r

    > **Incre"ent

    ++> **Decre"ent

    11

  • 8/9/2019 002-1 Programming Basics

    12/30

    Operat&rs an,

    epressi&nso Comparison operators 6 t& per&r" test &n &peran,soRet.rn t9e B&&/ean $a/.e

    oJse./ &r c&ntr&/ str.ct.re s.c9 as I an, '9i/e

    state"ent

    12

  • 8/9/2019 002-1 Programming Basics

    13/30

    /&' c&ntr&/ .ncti&no Wit9&.t fow control &pti&n% 'e &.r script c&./, n&t ."p :n&Qeibi/it!;

    oScripts are e$a/.ate, t9eir c&n,iti&ns an, c9ange t9eir be9a$i&rs:,!na"ic;

    o

     I statement 6 e$a/.ates t9e epressi&n bet'een parent9eses I t9eres./t is tr.e% t9e state"ent is eec.te Ot9er'ise% t9e state"ent issLippe,

     Ea"p/e6

    >M&nt9?

    i :>M&nt9 ?? 12 ;

      ec9& Tes% t9atUs tr.e55

     

    13

  • 8/9/2019 002-1 Programming Basics

    14/30

    /&' c&ntr&/ .ncti&no Else6 a/ternati$e b/&cL & c&,e t9at s9&./, be eec.te,i t9e epressi&n res./t is a/se

    o Ea"p/e6

    >-.nc9?@C9icLenI :>-.nc9 ?? @Rice;

    Ec9& @N&t ba,

    e/se

    Ec9& @Its great555

    18

  • 8/9/2019 002-1 Programming Basics

    15/30

    /&' c&ntr&/ .ncti&no Else i 6 t& e$a/.ate "./tip/e epressi&ns be&re ."ping t& t9e,ea./t b/&cL & c&,e

    o Ea"p/e6

    >-.nc9?@C9icLen

    I :>-.nc9 ?? @Rice; ** irst epressi&n e$a/.ati&n

    Ec9& @N&t ba,

    e/se i :>-.nc9 ?? @is9; ** Sec&n, epressi&n e$a/.ati&n

    Ec9& @Its great555

    e/se **,ea./t b/&cL & c&,e

    Ec9& @A'es&"e

    1

  • 8/9/2019 002-1 Programming Basics

    16/30

    /&' c&ntr&/ .ncti&no witch statement 6 A/ternati$e 'a! & c9anging t9eQ&' b! e$a/.ating t9e epressi&n

    o

     On/! &ne epressi&n is e$a/.ate, in s'itc9 state"entb.t eec.ting ,ierent c&,es% acc&r,ing t& t9e res./t

    1H

  • 8/9/2019 002-1 Programming Basics

    17/30

    /&' c&ntr&/ .ncti&noEa"p/e 6>M&&, ? sa,

    s'itc9 :>M&&,;

    case 9app! 6

    ec9& G&&, "&&,

    breaL

    case sa,6

    ec9& Ba, "&&,

    breaL

    ,ea./t6

    ec9& Neit9er 9app! &r sa,

    breaL

    1K

  • 8/9/2019 002-1 Programming Basics

    18/30

    /&' c&ntr&/ .ncti&no !oops 6 t& eec.te t9e b/&cL & c&,es &r se$era/ ti"eso "hile statement 6 eec.te t9e c&,e b/&cL '9i/e t9e epressi&n$a/.e is tr.e

    o Iteration 6 eac9 eec.ti&n & c&,e b/&cL 'it9in /&&p

    o Wit9in t9e c&,e b/&cL% s&"et9ing is c9ange, t9at aects &n t9e'9i/e epressi&n :n& c9anging ? "aLing innite /&&p% s& be care./;

    o Ea"p/e6

    >N."ber?1

    '9i/e :>N."ber=?20; **E$a/.ati&n in t9e beginning  ec9& >N."ber

    >N."ber **C9ange t9e epressi&n $a/.e

    17

  • 8/9/2019 002-1 Programming Basics

    19/30

    /&' c&ntr&/ .ncti&no#o $$ while statement 6 epressi&n is e$a/.ate, at t9een,

    o C&,e b/&cL is eec.te, at /east &nce

    o

     Ea"p/e6>N."ber?1

    D&

      ec9& >N."ber **Eec.te &nce

      >N."ber **C9ange t9e epressi&n $a/.e

    W9i/e :>N."ber=?20; **E$a/.ati&n at t9e en,

    1

  • 8/9/2019 002-1 Programming Basics

    20/30

  • 8/9/2019 002-1 Programming Basics

    21/30

    /&' c&ntr&/ .ncti&no&rea'ing out o loops ( c&./, breaL &.t base, &n t9e res./ts& a,,iti&na/ test

    o Ea"p/e6

    >C&.nter ? +8

    &r :C&.nter=?20>C&.nter;

    i :>C&.nter??0;

    breaL ** st&p 9ere '9i/e reac9 t& Xer&

    e/se

    >ec9& 100*>C&.nter

    21

  • 8/9/2019 002-1 Programming Basics

    22/30

    /&' c&ntr&/ .ncti&no'ipping an iteration with continue statement( &pp&site& breaL state"ent

    oIgn&re t9e err&r

    oEa"p/e6

    >C&.nter ? +8

    &r :C&.nter=?20>C&.nter;

    i :>C&.nter??0;

    c&ntin.e ** Ne$er st&p

    >ec9& 100*>C&.nter

    22

  • 8/9/2019 002-1 Programming Basics

    23/30

    .ncti&no %unction ( "aLes t9e c&,e eas! t& re.seo Decrease s&"e repetiti$e '&rLs

    oIts /iLe a "ac9ine

    o Inp.t is a ra' "ateria/ :$a/.e; an, t9en it '&rLs:pr&cess; &n t9e" t& pr&,.ce t9e pr&,.ct :ne' $a/.e;

    23

  • 8/9/2019 002-1 Programming Basics

    24/30

    .ncti&no A .ncti&n s9&./, be ca//e, 'it9in !&.r scripto )9e $a/.e is passe, t& t9e .ncti&n

    o W9en pr&cessing is nis9e,% a .ncti&n c&./, pass a $a/.e bacL t&t9e ca//ing c&,e

    o )ypes o unction 6

    o B.i/t+in 6 a$ai/ab/e in P(P /ang.age

    oJser+,ene, 6 ,ene b! .ser

    o &uilt-in %unction 6 c&./, be ca//e, b! its na"e an, t9e its inp.t

    bet'een parent9eses oArgument 6 t9e inp.t t9at is passe, t& t9e .ncti&n

    o Ea"p/e6

    o print :@(e//& W&r/,; ** (e//& '&r/, is arg."ent

    28

  • 8/9/2019 002-1 Programming Basics

    25/30

    .ncti&no *ser-de+ned unction 6 is ,ene, b! .sero Ea"p/e6

    .ncti&n 'e/c&"e:; ** ,ening .ncti&n 'it9&.t arg."ent

    ec9& (e//&

    'e/c&"e:;

    .ncti&n test.ncti&n:>tet; ,ening .ncti&n 'it9 arg."ent

    ec9& @>test

     )est.ncti&n:@We/c&"e t& S.n'a! .ni$ersit!; ** .ncti&n na"e is n&t casesensiti$e

    test.ncti&n :@(ere is Peta/ing a!a;

    2

  • 8/9/2019 002-1 Programming Basics

    26/30

    .ncti&no ,eturn statement 6 st&ps eec.ting & t9e .ncti&n an, sen,s t9e $a/.e bacL t& t9eca//ing c&,eolobal .ariable6 accessing &t9er $ariab/e 'it9in t9e script Jsing g/&ba/ be&re $ariab/ena"e

    o Ea"p/e6

    o>test.ncti&n ? 20

    .ncti&n a,,n."ber:>rstn."ber%>sec&n,n."ber%>t9ir,n."ber?20; ** )9irs arg."ent'it9 ,ea./t $a/.e

    g/&ba/ >test.ncti&n ** Accessing $ariab/e t9at is ,ene, s&"e'9ere

    >res./t ? >rstn."ber >sec&n,n."ber >t9ir,n."ber

    >res./t2 ? >test.ncti&n 2

    ret.rn >res./t ** Ret.rn 0

    a,,n."ber:10%20; ** N& t9ir, arg."ent

    2H

  • 8/9/2019 002-1 Programming Basics

    27/30

    .ncti&noExample (

    .ncti&n a,,n."ber:>rstn."ber;

    >rsn."ber ? **  >rstn."ber ? >rstn."ber

    >test ? 2

    A,,n."ber:>test;

    Ec9&test **  2

    oExample (

    .ncti&n a,,n."ber:Y>rstn."ber; ** Pass an arg."ent t& .ncti&n b! reerence

    >rsn."ber ? **

     >rstn."ber ? >rstn."ber

    >test ? 2

    A,,n."ber:>test;

    Ec9&test **  30

    2K

  • 8/9/2019 002-1 Programming Basics

    28/30

    Arra!o Variab/e st&res &n/! &ne $a/.e at a ti"eo Array ( specia/ t!pes & $ariab/e t9at enab/es !&. t& st&re as "an!$a/.es as !&. 'ant

    o

     Its in,ee, 'it9 a Le! :p&siti&n r&" 0; an, $a/.eo Ea"p/e6 ).es,a!

    o>,a!s ?arra!:M&n,a!%).es,a!%We,nes,a!%)9.rs,a!%ri,a!%Sat.r,a!%S.n,a!;

    >,a!sZ0[?\]M&n,a! ** An&t9er 'a! t& ,ene arra!

    >,a!sZ3[?@).es,a!

    >,a!s Z[ ? @test ** Wi// be a,,e, t& t9e en, & t9e arra!

    27

  • 8/9/2019 002-1 Programming Basics

    29/30

    Arra!o Associate 'ey 6 instea, & .sing ,ea./t Le! :0%1%2%;% 'e c&./,.se ,ene, Le!

    o Ea"p/e6

    >st.,ent ? arra!:na"e?&9n%  /astna"e?"a%

      Age?30;

    ec9& >st.,entZ\Age][

    Print >st.,entZ\/astna"e][

    >st.,entZ\ICn."ber][ ? 1238HK7 ** t& a,,

    2

  • 8/9/2019 002-1 Programming Basics

    30/30

    ^ Y A

      En,

    30