# آبان 85(C) NematAllah Ahmadyan1 Flowchart & Control Structures Introduction to Programming course 40-143c.

## Presentation on theme: "آبان 85(C) NematAllah Ahmadyan1 Flowchart & Control Structures Introduction to Programming course 40-143c."— Presentation transcript:

آبان 85(C) NematAllah Ahmadyan1 Flowchart & Control Structures Introduction to Programming course 40-143c

آبان 85 (C) NematAllah Ahmadyan 2 At a glance Control Structures Control Statements If for while Switch do…while …

آبان 85 (C) NematAllah Ahmadyan 3 IF and IF … ELSE structure  int x = … ;  if ( x%3 == 0 ){  … /* x = 3k */  }else if ( x%3 == 1 ){  … /* x = 3k + 1 */  }else {  … /* x = 3k +2 */  }

آبان 85 (C) NematAllah Ahmadyan 4 FOR structure  int i, j;  for (i=1 ; i<=size-1 ; i++){  for(j=1 ; j a[j+1])  swap(&a[j],&a[j+1]);  }  }  /* this is a main code of bubble-sort */

آبان 85 (C) NematAllah Ahmadyan 5 WHILE & DO … WHILE structure  int input = 0;  int sum = 0 ;  while ( input!=-1 ){  scanf(“%d”, &input );  sum += input ;  }

آبان 85 (C) NematAllah Ahmadyan 6 WHILE & DO … WHILE structure  int number, digit, number_r ;  do {  digit = number % 10 ;  number /= 10 ;  number_r += digit * pow(10,num_digit- i);  ++i ;  }while(number > 0);

آبان 85 (C) NematAllah Ahmadyan 7 BREAK structure  for ( i = 3 ; ; i += 2 ) {  term =  _sign * (pow(x,i) / fact(i)) ;  _sign = -1 * _sign ;  abs = term>0 ? term : - term ;  if ( abs < 0.0001 ) break;  sine = sine + term ;  }  /* calculate sin(x) from  x - x^3 / 3! + x^5/5!... */  /* while(1){} can be used too. */

آبان 85 (C) NematAllah Ahmadyan 8 SWITCH structure  switch(j){  case 0: k=1; break;  case 1: k=2; break;  case 2: k=0; break;  default: {  printf(“ERROR");  exit(1);  }  }

آبان 85 (C) NematAllah Ahmadyan 9 Finally … Commenting Clear code Versus Tricky code Avoid labels: and goto