If Statements and an Intro to the scanf() Function

Decision making in a program revolves around choosing to execute one set of program statements rather than another. The ability to compare the values of expressions and, based on the results, chose to run on set of statements or another is one of the fundamental building blocks of programming.

 

There are four fundamental operators for making decisions: less than, <, equal to, ==, greater than, >, and not, !. Note that the equal to operator has two successive equal signs. Using these operators, we can build Boolean expressions that resolve to either a true or false value. As we have seen earlier, true in C is represented by 1, and false is represented by 0. The value of a Boolean expression can be stored in a Boolean variable.

#include <stdio.h>


int main(void){
 
  int Methuselah = 969;
  int Noah = 950;
 
 
  if(Methuselah > Noah){
   printf("Methuselah is older than Noah.\n");
  }
 
  if(Noah > Methuselah){
   printf("Noah is older than Methuselah.\n");
  }
 
  return 0;
 
}

 

If an expression evaluates to false, the associated code is not run.

The Boolean expression is contained in parentheses following the if statement. This expression is evaluated to either 0 or 1, and then the associated code is either run or not. Since a Boolean expression resolves to a single value, an if statement’s execution hinges on this value, which means that an if statement can accept a single constant value or a variable as well as a full expression. Note that in C, any nonzero integer is evaluated as being true.

#include <stdio.h>


int main(void){
 
  int a = 8088;
  int b = 8086;
 
 
  if(73){
    printf("73 is a nonzero value.\n");
  }
 
  if(a){
    printf("a is nonzero value.\n");
  }
 
  if(a > b){
   printf("a is greater than b.\n");
  }
 
  return 0;
 
}

The if statement enables us to be selective about what input we accept and what we do with that input. We can use the scanf() function as a simple mechanism for getting user input. The scanf() function uses the same conversion characters that the printf() function uses. The scanf() function reads a value from standard input, and stores in the variable specified in the function call.

 #include <stdio.h>

int main(void){
 
  int num = 0;
 
  printf("Please enter a whole number: ");
 
  //note the ampersand
  //before the variable
  scanf("%d", &num);
 
  if(num > 42){
   printf("Your number is greater than 42.\n");
  }
 
  printf("Have a nice day.\n");
 
  return 0;
 
}#include <stdio.h>

int main(void){
 
  int num = 0;
 
  printf("Please enter a whole number: ");
 
  //note the ampersand
  //before the variable
  scanf("%d", &num);
 
  if(num > 42){
   printf("Your number is greater than 42.\n");
  }
 
  printf("Have a nice day.\n");
 
  return 0;
 
}

It’s a good idea to put a leading space in the format string for the scanf() function; this leading space will tell the scanf() function to ignore any whitespace characters, including newlines.

#include <stdio.h>


int main(void){
 
  char a = 0;
  char b = 0;
 
  printf("Please enter a character: ");
 
  //use %c conversion character
  scanf(" %c", &a);
 
  printf("That character is %c; it's numeric value is %d\n", a, a);
 
  printf("Please enter another character: ");
 
  //don't forget the ampersand
  //before the variable
  scanf(" %c", &b);
 
  printf("That character is %c; it's numeric value is %d\n", b, b);
 
  if(a > b){
    printf("%c [%d] is greater than %c [%d]\n", a, a, b, b);
  }
 
  if(b > a){
    printf("%c [%d] is greater than %c [%d]\n", a, a, b, b);
  }
 
  return 0;
 
}

Note that the scanf() function, like the printf() function, is stored in the stdio.h header file.

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