C Programming

Practise Exercise 6

Compound Relationals and switch

1. The statement that tests to see if sum is equal to 10 and total is less than 20, and if so, prints the text string "incorrect.", is

Statement 1
	if( (sum = 10) && (total < 20) )
	   printf("incorrect.");
Statement 2
	if( (sum == 10) && (total < 20) )
	   printf("incorrect.");
Statement 3
	if( (sum == 10) || (total < 20) )
	   printf("incorrect.");

2. if flag is 1 or letter is not an 'X', then assign the value 0 to exit_flag, else set exit_flag to 1.

Statement 1
	if( (flag = 1) || (letter != 'X') )
	   exit_flag = 0;
	else
	   exit_flag = 1;
Statement 2
	if( (flag == 1) || (letter <> 'X') )
	   exit_flag = 0;
	else
	   exit_flag = 1;
Statement 3
	if( (flag == 1) || (letter != 'X') )
	   exit_flag = 0;
	else
	   exit_flag = 1;

3. rewrite the following statements using a switch statement


		if( letter == 'X' )
			sum = 0;
		else if ( letter == 'Z' )
			valid_flag = 1;
		else if( letter == 'A' )
			sum = 1;
		else
			printf("Unknown letter -->%c\n", letter );

Statement 1
	switch( letter ) {
	   case 'X' : sum = 0; break;
	   case 'Z' : valid_flag = 1; break;
	   case 'A' : sum = 1; break;
	   default  : printf( "Unknown letter -->%c\n", letter ); break;
	}
Statement 2
	switch( letter ) {
	   case 'X' : sum = 0;
	   case 'Z' : valid_flag = 1;
	   case 'A' : sum = 1;
	   default  : printf( "Unknown letter -->%c\n", letter );
	}
Statement 3
	switch( letter ) {
	   case "X" : sum = 0; break;
	   case "Z" : valid_flag = 1; break;
	   case "A" : sum = 1; break;
	   default  : printf( "Unknown letter -->%c\n", letter ); break;
	}

ęCopyright B Brown. 1984-1999. All rights reserved.