Assigning value Conditional statement...

29
Assigning value Conditional statement Loops Examples Computer Algebra Systems Fundamentals of Programming Sándor Czirbusz [email protected] http://compalg.inf.elte.hu/~czirbusz/ Department of Computer Algebra Informatics Institute, ELTE D2.711A 2011-2012 spring

Transcript of Assigning value Conditional statement...

  • Assigning value Conditional statement Loops Examples

    Computer Algebra SystemsFundamentals of Programming

    Sándor [email protected]

    http://compalg.inf.elte.hu/~czirbusz/

    Department of Computer AlgebraInformatics Institute, ELTE

    D2.711A

    2011-2012 spring

    http://compalg.inf.elte.hu/~czirbusz/

  • Assigning value Conditional statement Loops Examples

    Index

    1 Assigning valueIn MapleIn Sage

    2 Conditional statementThe IF in MapleThe IF in Sage

    3 LoopsIn Maple

    The for/from loopThe for/in loopThe while loop

    General loopsLoops in Sage

    4 Examples

  • Assigning value Conditional statement Loops Examples

    In Maple

    In Maple

    name := expr;(name1, ..., namen) := (expr1, ..., exprn);

    Test it: assigned(name)Undoing it:

    name:=’name’unassign(’name1’,’name2’,...)evaln( name );

    The statement assign:assign(a,b), assign(A=B), assign(L) :

    it evaluates both sides.

    parameters in form “a,b”: a = b (for simple variables)parameters in form “A,B”: A,B expression sequences withsame length

  • Assigning value Conditional statement Loops Examples

    In Maple

    In Maple

    name := expr;(name1, ..., namen) := (expr1, ..., exprn);Test it: assigned(name)

    Undoing it:

    name:=’name’unassign(’name1’,’name2’,...)evaln( name );

    The statement assign:assign(a,b), assign(A=B), assign(L) :

    it evaluates both sides.

    parameters in form “a,b”: a = b (for simple variables)parameters in form “A,B”: A,B expression sequences withsame length

  • Assigning value Conditional statement Loops Examples

    In Maple

    In Maple

    name := expr;(name1, ..., namen) := (expr1, ..., exprn);Test it: assigned(name)Undoing it:

    name:=’name’unassign(’name1’,’name2’,...)evaln( name );

    The statement assign:assign(a,b), assign(A=B), assign(L) :

    it evaluates both sides.

    parameters in form “a,b”: a = b (for simple variables)parameters in form “A,B”: A,B expression sequences withsame length

  • Assigning value Conditional statement Loops Examples

    In Maple

    In Maple

    name := expr;(name1, ..., namen) := (expr1, ..., exprn);Test it: assigned(name)Undoing it:

    name:=’name’

    unassign(’name1’,’name2’,...)evaln( name );

    The statement assign:assign(a,b), assign(A=B), assign(L) :

    it evaluates both sides.

    parameters in form “a,b”: a = b (for simple variables)parameters in form “A,B”: A,B expression sequences withsame length

  • Assigning value Conditional statement Loops Examples

    In Maple

    In Maple

    name := expr;(name1, ..., namen) := (expr1, ..., exprn);Test it: assigned(name)Undoing it:

    name:=’name’unassign(’name1’,’name2’,...)

    evaln( name );

    The statement assign:assign(a,b), assign(A=B), assign(L) :

    it evaluates both sides.

    parameters in form “a,b”: a = b (for simple variables)parameters in form “A,B”: A,B expression sequences withsame length

  • Assigning value Conditional statement Loops Examples

    In Maple

    In Maple

    name := expr;(name1, ..., namen) := (expr1, ..., exprn);Test it: assigned(name)Undoing it:

    name:=’name’unassign(’name1’,’name2’,...)evaln( name );

    The statement assign:assign(a,b), assign(A=B), assign(L) :

    it evaluates both sides.

    parameters in form “a,b”: a = b (for simple variables)parameters in form “A,B”: A,B expression sequences withsame length

  • Assigning value Conditional statement Loops Examples

    In Maple

    In Maple

    name := expr;(name1, ..., namen) := (expr1, ..., exprn);Test it: assigned(name)Undoing it:

    name:=’name’unassign(’name1’,’name2’,...)evaln( name );

    The statement assign:assign(a,b), assign(A=B), assign(L) :

    it evaluates both sides.

    parameters in form “a,b”: a = b (for simple variables)parameters in form “A,B”: A,B expression sequences withsame length

  • Assigning value Conditional statement Loops Examples

    In Maple

    In Maple

    name := expr;(name1, ..., namen) := (expr1, ..., exprn);Test it: assigned(name)Undoing it:

    name:=’name’unassign(’name1’,’name2’,...)evaln( name );

    The statement assign:assign(a,b), assign(A=B), assign(L) :

    it evaluates both sides.parameters in form “a,b”: a = b (for simple variables)

    parameters in form “A,B”: A,B expression sequences withsame length

  • Assigning value Conditional statement Loops Examples

    In Maple

    In Maple

    name := expr;(name1, ..., namen) := (expr1, ..., exprn);Test it: assigned(name)Undoing it:

    name:=’name’unassign(’name1’,’name2’,...)evaln( name );

    The statement assign:assign(a,b), assign(A=B), assign(L) :

    it evaluates both sides.parameters in form “a,b”: a = b (for simple variables)parameters in form “A,B”: A,B expression sequences withsame length

  • Assigning value Conditional statement Loops Examples

    In Sage

    in Sage

    Exept the variable “x” the [varlist =] var(’[varlist]’)declaration is compulsory. If the variables exist, then themvalue is deleted.

    Assigning value: var = expressIn the same manner: var1, var2, . . . = kif1, kif2 ,. . . .If there is one variable and on the right side there areseveral expression, then the type of variables “tuple”.

  • Assigning value Conditional statement Loops Examples

    In Sage

    in Sage

    Exept the variable “x” the [varlist =] var(’[varlist]’)declaration is compulsory. If the variables exist, then themvalue is deleted.Assigning value: var = express

    In the same manner: var1, var2, . . . = kif1, kif2 ,. . . .If there is one variable and on the right side there areseveral expression, then the type of variables “tuple”.

  • Assigning value Conditional statement Loops Examples

    In Sage

    in Sage

    Exept the variable “x” the [varlist =] var(’[varlist]’)declaration is compulsory. If the variables exist, then themvalue is deleted.Assigning value: var = expressIn the same manner: var1, var2, . . . = kif1, kif2 ,. . . .

    If there is one variable and on the right side there areseveral expression, then the type of variables “tuple”.

  • Assigning value Conditional statement Loops Examples

    In Sage

    in Sage

    Exept the variable “x” the [varlist =] var(’[varlist]’)declaration is compulsory. If the variables exist, then themvalue is deleted.Assigning value: var = expressIn the same manner: var1, var2, . . . = kif1, kif2 ,. . . .If there is one variable and on the right side there areseveral expression, then the type of variables “tuple”.

  • Assigning value Conditional statement Loops Examples

    The IF in Maple

    Conditional statement I

    if cond1 thenstatement_sequ1

    elif cond2 thenstatement_sequ2

    elif cond3 thenstatement_sequ3............

    elsestatement_sequN

    end if

  • Assigning value Conditional statement Loops Examples

    The IF in Maple

    Conditional statement II

    Constructing and evaluating conditional statements:

    Relations: ,

    Logical operators: and, or, xor, implies, not

    Logical constants: true, false, FAIL

    Boolean evaluation: evalb, testeq, or is

    Remark: The logic in Maple interpreter is a trivalent logic.

  • Assigning value Conditional statement Loops Examples

    The IF in Sage

    Conditional statement

    if cond1:statement_sequ1

    elif cond2:statement_sequ2

    elif cond3:statement_sequ3............

    else:statement_sequN

    There are not end of block statement; teh colon is compulsory.

  • Assigning value Conditional statement Loops Examples

    The IF in Sage

    Constructing conditional statements

    Testing : expr1 == expr2 or kif1 is kif2

    Logical operators: and, or, notThe logical evaluation is more natural the in maple.

  • Assigning value Conditional statement Loops Examples

    The IF in Sage

    Constructing conditional statements

    Testing : expr1 == expr2 or kif1 is kif2Logical operators: and, or, not

    The logical evaluation is more natural the in maple.

  • Assigning value Conditional statement Loops Examples

    The IF in Sage

    Constructing conditional statements

    Testing : expr1 == expr2 or kif1 is kif2Logical operators: and, or, notThe logical evaluation is more natural the in maple.

  • Assigning value Conditional statement Loops Examples

    In Maple

    The for/from loop

    for counter from init by increment to final dostatement_seq

    end do

    The clauses from, by and to are optional, they are in arbitraryorder. Default values: by =1, from = 0.

  • Assigning value Conditional statement Loops Examples

    In Maple

    The for/in loop

    for variable in expr dostatement_seq

    end do

    The value of variable runs through all value of the compoundstructure "expr" and executed every time. Such is in PHP theloop foreach.

  • Assigning value Conditional statement Loops Examples

    In Maple

    The while loop

    while condition dostatement_seq

    end do

  • Assigning value Conditional statement Loops Examples

    General loops

    General loops

    for counter from initial by increment to finalwhile condition dostatement_seqend do

    for variable in expression while condition dostatement_seq

    end do

    Influencing the run of loop: break,next

  • Assigning value Conditional statement Loops Examples

    Loops in Sage

    The FOR iteration I

    for counter in range():statement_seq

    The parameters of range are the usual start value(the default is0), end value and the step (default = 1).Differently from other languages the loop does not run on theend value.Variants:

    range : it makes a listxrange : it is an iterator, not a list, therefore it is faster.srange : it is the slower (there are other parameters), it

    makes a list too.

  • Assigning value Conditional statement Loops Examples

    Loops in Sage

    The FOR iteration I

    for counter in obj:statement_seq

    here the “obj” is a compound object, mostly a list. The colon iscompulsory here; the body of loop continues until the end ofindentation.

  • Assigning value Conditional statement Loops Examples

    Loops in Sage

    The while loop

    wile logical_cond:statement_seq

    It is the usual while of Python.

  • Assigning value Conditional statement Loops Examples

    Examples

    In practice.

    Assigning valueIn MapleIn Sage

    Conditional statementThe IF in MapleThe IF in Sage

    LoopsIn MapleGeneral loopsLoops in Sage

    Examples