C Programming

Answers: Practise Exercise 6

Compound Relationals and switch

1. if sum is equal to 10 and total is less than 20, print the text string "incorrect.".


	if( (sum == 10) && (total < 20) )
		printf("incorrect.\n");

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.


	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 );


	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 );
	}

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