Two-Way Selection in C

The two-way selection is the basic decision statement for computers. The decision is based on resolving a binary expression, and then executing a set of commands depending on whether the response was true or false. C, like most contemporary programming languages, implements two-way selection with the if…else statement. An if…else statement is a paired statement used to selectively execute code based on two alternatives.

#include <stdio.h>


int main(void){
  
  int i, j;
  
  i = 5;
  j = 3;
  
  if(j<i){
    printf("%d is less than %dn\n", j, i); 
  } else {
    printf("%d is less thant %d\n", i, j);
  }
  
 
  return 0;
  
}

The test expression must be enclosed in parentheses. Note that the expression can have a side effect. In fact, many test expressions in C include side effects, such as an increment.

#include <stdio.h>

int main(void){
  
  int a = 3;
  
  if(a++<4){ 
    printf("%d is less than 4.\n", a);
  } else {
    printf("%d is less than 4.\n", a);
  }
  
  if(a<4){
    printf("%d is less than 4.\n", a);  
  } else {
   printf("%d is not less than 4.\n", a); 
  }
 
  return 0;
  
}

We can use a compound statement for complex logic in an if…then statement.

#include <stdio.h>

int main(void){

  int answer1 = 0;
  
  int answer2 = 0;
  
  while(1){
    printf("What university did the applicant attend?\n");
    printf("1. Harvard \t 2. MIT \t 3. UC Berkeley \n 4. Stanford \t 5. Caltech \t 6. Other\n");
    scanf(" %d", &answer1);
    if(answer1 > 0 && answer1 <= 6){
      break;
    }
  }
  
  while(1){
    printf("Majored in what subject?\n");
    printf("1. Computer Science \t 2. Computer Engineering \n 3. Management Information Systems \t 4. Other\n");
    scanf(" %d", &answer2);
    if(answer2 > 0 && answer2 <= 4){
      break;
    }
  }
  
  if(answer1!=6 && answer2!=4){
    printf("Schedule an interview with the applicant.\n"); 
  } else {
    printf("Send them a rejection letter.\n");
  }
  
  
  return 0;
  
}

True and false statements can be exchanged by complementing the expression. Any expression in C can be complemented with the not operator, represented by the exclamation point.

#include <stdio.h>

int main(void){
  
  int true = 1;
  int false = 0;
  
  if(true){ 
    printf("That is true.\n");
  } else {
    printf("That is not true.\n");
  }
  
  if(!true){
   printf("That is not true.\n"); 
  } else {
   printf("That is true.\n"); 
  }
  
  if(!false){
    printf("That is not false.\n");
  } else {
   printf("That is false.\n"); 
  }
 
  return 0;
  
}

To get over 300 original lessons on Standard C not seen on this blog, follow this link

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s