c++lect2

Upload: manumanuela

Post on 03-Jun-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 C++Lect2

    1/11

    C++ Lecture 2 POO

    5. C/C++ statements

    Every C/C++ statements has at its end a semicolon ;or a bracket }.

    5.1. Empty statement

    This statement is used when the presence of a statement or function that does notexecute anything is needed.

    . ;

    5.2. Expression statement

    General syntax:

    . Expression;

    Examples:y = ( x+1) *( x- 1) ; / / as si gn: v =Expression ; f ( x , y ) ; / / cal l f unct i on: f(p , p p n ) ; 1 2. . .c++; - - c; / / I ncr ement / Decr ement ( Pre / Post )

    5.3. Compound statement (statement block)

    This statement uses { and } to group several statements as a unit.

    {DeclarationsStatements

    }

    Example:{i nt i ; / l ocal var i abl e i n t hi s bl ock;/f ( i ) ; / / i can be used j ust i n t hi s compound st at ement ; } ; / / a f t e r } i s not ;

    5.4. The if statement

    Is a decision structure with one or two branches having the following syntax:

    . if (Expression) statement 1; [else statement 2; ]

    As we remember, in any C/C++ program, the value 0 means FALSE , and any other valuemeans TRUE . Every expression has a value!!!

    23.03.06 1

    Example:

  • 8/12/2019 C++Lect2

    2/11

    C++ Lecture 2 POO

    { / / P( x , y ) f i r s t quar t er ( i f x or y is negative the sign is changed)f l oat x, y;pr i nt f ( "ent er t he coor di nat es P( x, y) = ") ;scanf ( "%f %f ", &x, &y); / / poi nt Pi f ( x>0)

    i f ( y

  • 8/12/2019 C++Lect2

    3/11

    C++ Lecture 2 POO

    f or (m=1; m

  • 8/12/2019 C++Lect2

    4/11

    C++ Lecture 2 POO

    3. When the expression is evaluated, if its true , control is transferred back to the topof the loop; if it is false , control is transferred to the next program statement.

    Examples:do

    { = %b; a=b; b=r ; }r a while ( r ) ; / / b! =0 ! ! !

    gcm=a;

    i f ( b) / / or do r =a%b, a=b, b=r ;while ( b ) ;

    gcm=a;

    5.8. The for statement (The for loop)

    The typical form of a for loop is:

    for ( ; ; )1 ;

    The internal logic of a for loop that is as follows:

    1. The first expression (i.e. expression 1) is evaluated;

    2. The second expression (i.e. expression 2, named termination condition) is evaluated;

    3. If the termination condition is true then

    a. the body (i.e. ) of the loop is executed;

    b. the third expression is evaluated;

    Step 3. is repeated until the termination condition became false

    4. If the termination condition is false then the control of the program is transferred tothe first statement following the loop.

    The for statement has the same effect with the following structure:;1 while ( )

    { ;;

    }

    Vice versa (see the while statement):for ( ; ; )

    < statement >;

    The do while loop (see do while statement) is similar with the following:

    for ( ; ; )

    23.03.06 4

    {;

  • 8/12/2019 C++Lect2

    5/11

    C++ Lecture 2 POO

    if(!< expression> )break ;

    }

    Example:

    i nt i , n; l ong f ; / / f

    n! pr i nt f ( " \ n l et s n: " ) ; scanf ( "%d", &n) ; f =1;f or ( i =1; i

  • 8/12/2019 C++Lect2

    6/11

    C++ Lecture 2 POO

    if (e x pr 2) goto et ;... }

    ... et : ... // statement

    6. Function call

    Call function is a particular case of the expression instruction:

    (arguments list) ;

    A function can be also called inside an expression of a statement:

  • 8/12/2019 C++Lect2

    7/11

    C++ Lecture 2 POO

    #i ncl ude ;#i ncl ude ;i nt Sf (i nt& f, i nt k){ i nt p=0;

    whi l e (!(f %k)) {f / =k; p++; }return p;

    }

    mai n (){ cl rscr( );i nt n, f 2=0, Uc=1;cout n;f or ( i nt i =2; i

  • 8/12/2019 C++Lect2

    8/11

  • 8/12/2019 C++Lect2

    9/11

    C++ Lecture 2 POO

    7. Constants declaration

    Another way to define a constant is by using the Const modifier ::

    [ ] const < name Var> [ = Value] ;

    or const [ ] 4) , Di gi t ( c& C0F ) ) ; }

    whi l e ( c!= Esc ) ;}

    Example 2:/ / ProgramErr or Const

    # i ncl ude # i ncl ude

    i nt Cons t ( cons t i nt i , i nt cons t j ){i =1; // Error CONST_.CPP 7: Cannot modify a const object j =1; // Error CONST_.CPP 8: Cannot modify a const object r e turn i +j ;

    }voi d mai n ( ){cl r s cr ( ) ;

    const i nt i =10;i =5; // Error CONST_.CPP 13: Cannot modify a const object

    i nt const j =5; j =5; // Error CONST_.CPP 14: Cannot modify a const object cout

  • 8/12/2019 C++Lect2

    10/11

    C++ Lecture 2 POO

    Example 3:/ / Progr am Exampl e Const

    #i ncl ude #i ncl ude

    i nt Const ( const i nt * x, i nt * const y, const i nt * const z){x[ 0] =y[ 0] ; // Error CONST_.CPP 7: Cannot modify a const object

    y=x; // Error CONST_.CPP 8: Cannot modify a const object z[ 0] =y[ 0] ; // Error CONST_.CPP 9: Cannot modify a const object z=x; // Error CONST_.CPP 10: Cannot modify a const object y[ 0] =z[ 0] ; // Correct, y[i] can be modified x=y; // Correct, x address can be modified r et ur n x[0] +y[0]+z[ 0] ;

    }voi d mai n ( ){ cl r s cr ( ) ;

    i nt a[ 10] , b[ 10] , c [10] ; / / cout

  • 8/12/2019 C++Lect2

    11/11

    C++ Lecture 2 POO

    8. Introduction synonym for an existing data type

    Assigning a data type a name is realised as follows:

    typedef < type> newName ;

    Example: typedef int Integer ;typedef float Real; Integer i , j ;Real X[ 10] ;typedef int Sir[100];typedef float Matr[10][10]Sir X,Y;

    Matr A,B;

    9 . Contents

    p a g .

    5. C/C++ statements ................................................................................ 1

    5.1. Empty statement....................................................................................... 1

    5.2. Expression statement................................................................................ 1

    5.3. Compound statement (statement block)................................................... 1

    5.4. The if statement........................................................................................ 1 5.5. The switch statement................................................................................ 2

    5.6. The while statement (while loops)........................................................... 3

    5.7. The dowhile statement (dowhile loops) ....................................... 3

    5.8. The for statement (The for loop).............................................................. 4

    5.9. The break statement ................................................................................. 5

    5.10. The continue statement......................................................................... 5

    5.11. The go to statement .............................................................................. 5

    6. Function call ........................................................................................ 6

    6.1. The address operator (&) ......................................................................... 7

    7. Constants declaration ......................................................................... 9

    8. Introduction synonym for an existing data type............................ 11

    9. Contents ............................................................................................. 11

    23.03.06 11