Decision
Boolean
  • By default, C takes 0 as False, none 0 as True
  • #include <stdio.h>
    
    int main()
    {
    	if (1)
    		printf("True\n"); // print out
    	else
    		printf("Flase\n");
    
    	return 0;
    }
    			
  • C99 introduces the bool type to C, define true/false in stdbool.h
  • #include <stdio.h>
    #include <stdbool.h>
    
    int main()
    {
    	bool b = true;
    
    	if (b)
    		printf("True\n");
    	else
    		printf("False\n");
    
    	b = (10%2 == 0);
    
    	if (b)
    		printf("Even\n");
    	else
    		printf("Odd\n");
    
    	return 0;
    }
    			
    If
  • Braces { and } are used to group declarations and statements together into a compound statement, or block, so that they are syntactically equivalent to a single statement
  • #include <stdio.h>
    
    int main()
    {
    	int age = 20;
    
    	if (age >= 21)
    	{//start block
    		printf("You are allowed to drink ...");
    	}//end block
    	else
    	{
    		printf("You are not allowed to drink ...");
    	}
    
    	return 0;
    }
    			
  • If there is only one line of code, the curly braces can be ignored
  • #include <stdio.h>
    
    int main()
    {
    	int age = 20;
    
    	if (age >= 21)
    		printf("You are allowed to drink ...");
    	else
    		printf("You are not allowed to drink ...");
    
    	return 0;
    }
    			
  • else sentence is not necessary
  • #include <stdio.h>
    
    int main()
    {
    	int age = 20;
    
    	if (age >= 21)
    		printf("You are allowed to drink ...\n");
    
    	return 0;
    }
    			
  • if ... else if ... else
  • #include <stdio.h>
    
    int main()
    {
    	int num = 20;
    
    	if (num < 0)
    		printf("Negative Number ...\n");
    	else if (num >= 0)
    		printf("Non-negative Number ...\n");
    	else
    		printf("Unknown Number ...\n");
    
    	return 0;
    }
    			
  • Nested if ... else ...
  • #include <stdio.h>
    
    int main()
    {
    	int num = 20;
    
    	if (num >= 0)
    	{
    		if (num > 0)
    			printf("Natural Number ...\n");
    		else
    			printf("Zero ...\n");
    	}
    	else
    		printf("Negative Number ...\n");
    
    	return 0;
    }
    			
    Relational Operators
  • ==, !=, >, < >=, <=
  • Logical Operators
  • &&, and
  • ||, or
  • !
  • #include <stdio.h>
    
    int main()
    {
    	int num = 20;
    
    	// and
    	if (num >= 0 && num%2 == 0)
    		printf("Qualified Number ...\n");
    
    	if ((num >= 0) && (num%2 == 0))
    		printf("Qualified Number ...\n");
    
    	// or
    	if (num >= 0 || num%2 == 0)
    		printf("Qualified Number ...\n");
    
    	if ((num >= 0) || (num%2 == 0))
    		printf("Qualified Number ...\n");
    
    	// not
    	if (!(num%2 == 1))
    		printf("Qualified Number ...\n");
    
    	return 0;
    }
    			
    Relational Operators
  • !
  • <, <=, > >=
  • &&
  • ||
  • Switch
    #include <stdio.h>
    
    int main()
    {
    	int option;
    
    	printf("Enter an option (1-3):\n");
    	scanf("%d", &option);
    
    	switch(option)
    	{
    		case 1:
    			printf("Install Software ...\n");
    			break;
    		case 2:
    			printf("Unstall Software ...\n");
    			break;
    		case 3:
    			printf("Readme\n");
    			break;
    		default:
    			printf("Exit ...\n");
    	}
    
    	return 0;
    }
    			
    Reference
  • Operators Precedence in C