The C Programming Language - Control Flow

Edusagar - notes - Control Flow
  • Binary Search program


    int binsearch(int x, int v[], int n)
    {
    	int low, high, mid;
    	
    	low = 0;
    	high = n-1;
    	while(low <= high) {
    		mid = (low+high)/2;
    		if (x < v[mid]) {
    			high = mid - 1;
    		} else if (x > v[mid]){
    			low = mid + 1;
    		} else {
    			return mid;
    		}
    	}
    	return -1;
    }
  • In the binary search routine above - there are two comparisons within the while loop.. the same can be minimized as:


    int binsearch1(int x, int v[], int n)
    {
    	int low, high, mid;
    	
    	low = 0;
    	high = n-1;
    	mid = (low+high)/2;
    	
    	while((low <= high) && (v[mid] != x)) {
    		if (x < v[mid]) {
    			high = mid - 1;
    		} else {
    			low = mid + 1;
    		}
    		mid = (low+high)/2;
    	}
    	
    	if (v[mid] == x) {
    		return mid;
    	}
    	return -1;
    }	
    
  • case statement acts as a label, so once the statements in a case block are over, by default the execution falls through to the next case block until unless you take explicit action to escape using break or return. Also, make sure to put an explicit comment wherever you want fall through behavior

  • One can always remove the expressions from for loop, but still the semilcolons are mandatory. Following is an infinite loop which can only be broken up by statements like break or return:


    for(;;) {
    
    }
  • #include <ctype.h>
    int atoi1(char s[])
    {
    	int i,n,sign;
    	
    	// Skip whitespaces
    	for(i=0; isspace(s[i]); i++);
    	
    	// Get and skip sign
    	sign = (s[i] == '-') ? -1 : 1;
    	if (s[i] == '+' || s[i] == '-') i++;
    
    	// get integer value
    	for (n=0; isdigit(s[i]); i++) {
    		n = 10*n + (s[i]-'0');
    	}
    	
    	return n*sign;
    }

  • Comma Operator:

    It is evaluted from left to right. The type and value of the result is the type and value of the rightmost operand.


    void reverse(char s[])
    {
    	int i,j,c;
    	
    	for (i=0, j=strlen(s)-1; i < j; i++, j--) {
    		c = s[i], s[i] = s[j], s[j] = c;
    	}
    }
    

    The commas that separate the arguments in function call/prototype aren't comma operators.

  • do-while() is needed in this program, since string has to have atleast one character, even if n is 0.


    void itoa(int n, char s[])
    {
    	int i, sign;
    	
    	if ((sign = n) < 0) {
    		n = -n;
    	}
    	
    	i = 0;
    	do {
    		s[i++] = n % 10 + '0';
    	} while((n /= 10) > 0)
    	
    	if (sign < 0) {
    		s[i++] = '-';
    	}
    	s[i] = '\0';
    	
    	reverse(s);
    }
  • Continue doesnt apply to switch.

  • goto labels can be attached to any statement in the same function as the goto. The scope of a label is the entire function.

  • non-automatic variables are initialized only once(before program starts executing) and the initializer has to be a constant. Automatic variables are initialized everytime the function or the block in which they are declared is hit. The initializer in this case can be any expression.

comments powered by Disqus