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

Introduction to Arrays in C

An array is a list of more than one variable having the same name.  Each variable in an array is known as an array element. We differentiate array elements by a subscript, which is  a number inside brackets. All array subscripts begin at zero, not one.

We can control individual elements in an array by their subscripts.

#include <stdio.h>

int main(void){

    int intArray[5];

    intArray[0] = 42;
    intArray[1] = 73;
    intArray[2] = 90210;
    intArray[3] = 64111;
    intArray[4] = 1138;


    printf("intArray[2] = %d\n", intArray[2]);
    printf("intArray[4] = %d\n", intArray[4]);


    return 0;

}

We can define an array as any data type in C. We can have integer arrays, long integer arrays, double arrays, char arrays, and so on. The compiler recognizes that we are defining an array, and not a single non-array variable, when we put brackets after the array’s name.

#include <stdio.h>

int main(void){

    
    char arrayChar[6];
    
    arrayChar[0]='H';
    arrayChar[1]='e';
    arrayChar[2]='l';
    arrayChar[3]='l';
    arrayChar[4]='o';
    arrayChar[5]='!';


    int i = 0;

    while(i<7){
        putchar(arrayChar[i++]);
    }



    return 0;

}


When we define an array, we tell the compiler to reserve a specific number of memory locations for that array by setting how many elements are in the array. Each element in an array uses the same amount of storage as a variable of the same type. C stores all array elements in a contiguous block.

#include <stdio.h>

int main(void){
 
  int n;
  int intArray[5];
 
  double j;
  double dubArray[5];
 
  printf("Size of a single int variable: %lu\n",
     sizeof(n));
 
  printf("Size of the intArray: %lu\n", sizeof(intArray));
 
  printf("Size of a single double variable: %lu\n",
     sizeof(j));
  printf("Size of a the dubArray: %lu\n", sizeof(dubArray));
 
  return 0;
 
}

We need to be sure to keep our subscript values within the range that we set when we declared the array.

We can create a string by initializing a char array with a character string. Note that with a string, we need an extra array element to hold the null zero at the end of the quoted string. A string is really just a char array where the last element is a null zero.

#include <stdio.h>

int main(void){
 
  char state1[15]="Missouri";
  char state2[15]="Nebraska";
 
  printf("%s and %s",
     state1, state2);
 
  char state3[15]={'K', 'a', 'n', 's', 'a', 's', ''};
 
  printf("\nand %s\n", state3);
 
  return 0;
 
}

We should always define an array with the maximum number of desired elements indicated in the subscript, unless we initialize the array at the same time.

#include <stdio.h>

int main(void){
 
  int nums[] = {1,3,5,7,11,13,17};
  long long bigNums[] = {8675309, 6647276};
 
  printf("%lld\n", bigNums[0]);
  printf("%d\n", nums[3]);
 
 
  return 0;
 
}

Our final example is a program to calculate the average of a set of grades given as double float values.

#include <stdio.h>

void printScores(double array[], int size);

int main(void){
 
  char uName[] = "Camden College";
 
  double grades[7] = {97.0, 81.3, 100.0, 89.9, 75.8, 88.5, 93.9};
 
  double averageGrade=0.0;
 
  int i;
 
 
  printScores(grades, 7);
 
  for(i=0; i<7; i++){
    averageGrade+=grades[i];
  }
 
  averageGrade/=7.0;
 
 
  printf("At %s, your average is %.1f\n",
     uName, averageGrade);
 
  return 0;
 
}

void printScores(double array[], int size){
  printf("Here are your grades:\n");
 
  int i;
  for(i=0;i<size;i++){
    printf("%.1f\n", array[i]);
  }
 
}

Note that to pass an array to a function, we need to specify only its name. In the function’s parameter list, we need to state the array type and include brackets after the array name.