nestedif2.c

/*
 * show a cleaner version of a nested if statement
 # you can set x to 9, 10, 11, 12 by defining X9, X10, X11, X12
 * default is x = 10
 *
 * Matt Bishop, ECS 36A
 * April 9, 2024	original version
 */
#include <stdio.h>

int main(void)
{
	int x = 10;		/* variable for if condition */

	/* now reser the value of x as desired */
#if defined(X9)
	x = 9;
#elif defined(X11)
	x = 11;
#elif defined(X12)
	x = 12;
#endif
	
	/* now the nested ifs -- note the { } in the elses */
	/* you can omit them but I put them in for clarity */
	if (x == 12)
		printf("x is 12!\n");
	else if (x == 11)
		printf("x is 11!\n");
	else if (x == 10)
		printf("x is 10!\n");
	else
		printf("I have no clue what x is :(\n");

	/*
	 * that's it, folks!
	 */
	return(0);
}


UC Davis sigil
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email: mabishop@ucdavis.edu
ECS 36A, Programming & Problem Solving
Version of April 2, 2024 at 12:13PM

You can get the raw source code here.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh