Click here to load reader

C 6η διάλεξη

  • View
    1

  • Download
    0

Embed Size (px)

DESCRIPTION

Εντολές βρόγχου

Text of C 6η διάλεξη

  • 1 () 101 () 101 / & / &

    . .

    . .

    **********--mail: mail: [email protected]@ced.tuc.gr**********Web: Web: http://www.ece.tuc.gr/http://www.ece.tuc.gr/kmaniakmaniaOffice: 145 A14Office: 145 A14Office: 145.A14Office: 145.A14Tel: 28210 37222Tel: 28210 37222

    ::http://courses.ced.tuc.grhttp://courses.ced.tuc.gr

    . .intint data[1000];data[1000];

    data[0] data[0] data[999]data[999]

    KK ..intint data[1000];data[1000];intint count;count;count=100;count=100;data[count]=12; /* data[count]=12; /* data[100]=12 */data[100]=12 */

  • 2

    . . main() main() main().main().H H C C . .

    ()()

    ( (iterative statements) iterative statements) CC . . CC , , :: whilewhile dodo--whilewhile forfor ( (body)body) . . ( (condition)condition). . ((fixed fixed point)point)..

    .. ( (structurestructure) ) (.. (.. ( ( ). ). . . ( (statementstatement commandcommand) ) (.. (.. CC) . ) . CC ( (iterative structures). iterative structures).

    forfor block block . . forfor . .for (for ( ;; ;; ;;))for (for ( ; ; ;; ;;));; ; ; ;; ;; C C C.C.

  • 3 forfor . . . . . . .. ( ) ( ) ( ) ( ) for for oo.. C C . . . .for (for (; ; ;; ;;));;

    forfor for for

    for for ((; ; ; ))

    ::#include

    int count;

    main () {for (count = 1; count

  • 4 forfor for for

    for for ((; ; ; ))

    ::#include

    int count;

    main () {count = 0

    for (printf(Counting); count < 20; count+=5){

    printf(%d\n, count);

    return 0;}

    Counting 0

    5

    10

    15

    To printf(Counting) for

    forfor for for

    for for ((; ; ; ))

    ::#include

    int count;

    main () {count = 0

    for (printf(Counting) ; count < 20;){

    printf(%d\n, count+=5;);

    return 0;}

    Counting 5

    10

    15

    To printf(Counting) for

    forfor for for

    for for ((; ; ; ))

    ::#include

    int count;int array[1000];

    main () {

    for (count = 0; count < 1000; array[count++] = 50){

    ;

    }

    , 50 . for

    forfor for for

    for for ((; ; ; ))

    ::#include

    int count;

    main () {

    for (count = 0; count < 1000 && array[count]; count++){

    printf(%d, array[count]);

    }

    H for 0

  • 5 forfor for for

    for for ((; ; ; ))

    ::#include

    int count;

    main () {

    for (count = 0; count < 1000 && array[count] != 0;){

    printf(%d, array[count++]);

    }

    H for 0

    forfor for for

    for for ((; ; ; ))

    ::#include int i, sum=0;main () {

    for (i = 1; i

  • 6 1 1 nn..

    #include #include int i, n;int i, n;, ;, ;main() {main() {printf(Enter the max number to compute the square: );printf(Enter the max number to compute the square: );

    scanf(%d, &n); scanf(%d, &n); for (i = 1; i

  • 7 whilewhile

    main ()main (){{count = 1;count = 1;

    while (count

  • 8 whilewhilewhile (while () )

    . . ( ) ( ) whilewhile while while . . ( ( ) ) C C block block .. 1. 1.

    whilewhilewhile (while () )

    while while bl kbl k block block ( ) . ( ) . , , while while ( ( while while do whiledo while)). .

    1 10. 1 10.

    #include #include

    main() main() {{{{

    int i = 1;int i = 1;int sum = 0;int sum = 0;while (i

  • 9 do whiledo while do while do while

    do do while while (()

    . .

    ::#include #include int i = 0;int i = 0;

    main()main(){ { do do

    { { printf(The number is: %dprintf(The number is: %d\\n, i);n, i);i = i + 1;i = i + 1;

    }}while (i 0 */

    if( sum

  • 10

    int i;int i;for(i = 1; i

  • 11

    E selection 1 4,

    ( (loops)loops) ( (nested loops)nested loops).. , , ..

    #include main() {main() {

    intint k, x, sum = 0;k, x, sum = 0; (1*2 + 1*3 + 1*10) +

    k=1

    5

    x=2

    10 * *xx

    for(k=1; k

  • 12

    3 3

    1

    True

    1

    , 2 , .

    2False

    3 3

    True

    False

    1

    False

    True

    False

    1 , 2 , .

    2True

    3 3#define TRUE 1#define TRUE 1#define FALSE 0#define FALSE 0

    void main() {void main() {intint flag;flag;

    flag = TRUE;flag = TRUE;while (flag)while (flag) /*/* while (flag !=0) */while (flag !=0) */

    (flag)

    1while (flag) while (flag) / / while (flag !=0) /while (flag !=0) /

    {{ 1 1;;if (if ( ) )

    {{flag = FALSE;flag = FALSE;

    }}elseelse

    {{ 2;2;

    }}}}

    }}

    2

    True

    False

    A true, flag false o while

    3 3#define TRUE 1#define TRUE 1

    void main() {void main() {

    while (TRUE) /* while (TRUE) /* */ */

    loop break

    {{ 1 1;;if (if ( ) )

    break;break; 2;2;

    } } }}

    break if { }. break .

    2

    True

    False

    1

  • 13

    3 3 . . . . . . breakbreak . . breakbreak . .

    XX

    , , .. . .intint data[1000]data[1000] datadata intint 10001000intint data[1000],data[1000], datadata intint 1000 1000 ..datadata[0] [0] datadata[999].[999].intint data[1000];data[1000];intint count;count;count = 100;count = 100;data[count] = 12; /*data[count] = 12; /* data[100] = 12; data[100] = 12; */*/

    . . 0 0 0 0 1.1.

  • 14

    XX

    , , . . . .

    () () ..float expenses [12];float expenses [12];p [ ]p [ ]H H expensesexpenses 12 12 floatfloat.. 0 0 11. 11. . .

    XX

    expenses [1] = 100;expenses [1] = 100;H H 100 100 . .

    [8][8] [9][9]expenses [8]expenses [8] == expenses [9];expenses [9];expenses [1+2] = 100; expenses [1+2] = 100; expenses expenses [[33] = 100; ] = 100; expenses[a[2]] = 100; a[2] expenses[a[2]] = 100; a[2] ..

    XX

    . . . . 1 1 n, n, 0. 0.

  • 15

    sizeof() bytes

    . . 2 . 2 .i ti t h k [8][8]h k [8][8] 64 64 h kh kintint checker [8][8]; checker [8][8]; 64 , 64 , checker checker [0][0],checker[0][1]. [[0][0],checker[0][1]. [ 8 8 .] 8 8 .] ! !

    #define MONTHS 12#define MONTHS 12

    intint array[MONTHS];array[MONTHS]; #define #define . .

    intint arrayarray[12[12];];

    DEFINE DEFINE ::const const intint MONTHS = 12;MONTHS = 12;intint array[MONTHS];array[MONTHS];

  • 16

    int array[4] = {100, 200, 300, 400 };int array[4] = {100, 200, 300, 400 };To To ::int array[] = {100, 200, 300, 400 };int array[] = {100, 200, 300, 400 };int array[10] = {1,2,3 };int array[10] = {1,2,3 };

    MM , , ..

    ::intint array [4][3] = { 1,2,3,4,5,6,7,8,9,10,11,12 array [4][3] = { 1,2,3,4,5,6,7,8,9,10,11,12

    };};array[0][0] = 1array[0][0] = 1array[0][0] 1 array[0][0] 1

    array[1][0]=4array[3][1]=11, array[1][0]=4array[3][1]=11, array[3][2]=12array[3][2]=12

    ::intint array [4][3] = { { 1,2,3}, {4,5,6}, {7,8,9}, array [4][3] = { { 1,2,3}, {4,5,6}, {7,8,9},

    {10,11,12} };{10,11,12} };

    AA 500 500 float float 500 * 4 500 * 4 bytesbytes

  • 17

    ? ? ??