if, switch, for, while

January 28 2002

more fun with scanf

here's one last detail about scanf: there are some situations where scanf won't wait for the user to type something in. basically, it's possible for the user to "type ahead" of a scanf... for example, suppose we have the following code:

int x;
int y;

scanf("%d", &x);
scanf("%d", &y);
printf("%d %d\n", x, y);

when we run the program, the computer will pause and wait for us to type something in when it processes the first scanf. suppose we type in 4 5. the first scanf will read in the first integer [4], because we only have one %d in the scanf statement. when the computer processes the second scanf statement, it will look for another integer, but we already typed one in [5]. since we already typed one in, the second scanf will not wait for us to type something in, instead, it will just use the 5 that we typed in before as the integer to store in y. so, when the printf runs, it will print 4 5. this bit of scanf weirdness might be useful on this week's lab...

relational operators

name description
< check if one number is strictly less than another
> check if one number is strictly greater than another
<= check if one number is less than or equal to another
>= check if one number is greater than or equal to another
== check if one number is equal to another [don't forget there are two equal signs! if you leave one off, you're writing an assignment statement...]
!= check if one number is not equal to another

boolean operators

name description
&& and: determine if two conditions are both true. this should be a familiar concept... for example, if you want apples and oranges, then you are satisfied only if you have both apples and oranges.
|| or: determine if at least one condition is true. this should also be familiar: if you want apples or oranges, then you are satisfied if you have apples, if you have oranges, or if you have both.
! not: invert the truth of a statement. makes true things false, and false things true.

if statements

if statements tell the computer to do things only if certain conditions are true. for example:

if(1 <= x && x <= 4)
{
  printf("good choice: %d\n", x);
}
else
{
  printf("bad choice\n");
}

the above if statement says: print good choice if x is between 1 and 4. otherwise, print bad choice. you can add something like this to your menu program from last week to make it more intelligent.

switch statements

switch statements tell the computer to do different things depending on the value of some expression. for example:

switch(grade)
{
  case 'a':
  case 'b':
    printf("good\n");
    break;
  case 'c':
    printf("okay\n");
    break;
  case 'd':
  case 'f':
    printf("oops\n");
    break;
  default:
    printf("huh?\n");
}

the above switch will print good if the grade was an 'a' or a 'b', okay if the grade was a 'c', oops if the grade was a 'd' or an 'f', and huh? for any other grade.

don't forget to the break statements!

for loops

for loops tell the computer to do things repeatedly. here's how the computer processes for loops: first, the initialization [the part between the open parenthesis and the semicolon] is run. second, the test [the part between the two semicolons] is run. if the test is true, the body is run. if the test is false, the for loop is done, and the computer continues continues processing commands after the for loop. third, after the body is run, the increment [the part between the semicolon and the closing parenthesis] is run, and the computer goes to the second step again.

this is pretty confusing. let's look at an example:

int i;
for(i=0; i < 3; i++)
{
  printf("%d ", i);
}
printf("\n");

let's see what the computer will do with the above for loop. first, it does the initialization, which sets i to 0. next, it will run the test, which determines if i is less than 3 [true, since 0 is less than 3]. so, the body is run, and the computer will print 0. next, the increment part [i++] is run, which increases the value of i to 1. the test is re-run, and since 1 is less than 3, the body is run again, and the computer prints 1. the increment part is run again, and the value of i increases to 2. the test is re-run, and since it is true [2 is less than 3], the computer prints 2. the increment part is run again, raising the value of i to 3, and the test is re-run, but this time 3 is not less than 3, so the for loop stops, we print the newline, and we're done.

to summarize, the above for loop prints 1 2 3 .

while loops

a while loop is just like a for loop, except there are no initialization or increment parts. there is only a test. here's a while loop that does the same thing as the above for loop:

i=0;
while(i < 3)
{
  printf("%d ", i);
  i++;
}
printf("\n");

exercises

write a program that reads two numbers from the keyboard, and outputs success if exactly one of the numbers is even, and failure in any other case. here are some example inputs and outputs:

input output
1 1 failure
1 2 success
2 1 success
2 2 failure

write a program that calculates exponents. it should read two numbers from the keyboard, the base and the exponent. so, if i input 2 8, it should output 256, which is 2 to the 8th power [2*2*2*2*2*2*2*2 = 256].

write a program that calculates factorials. it should read one number from the keyboard, and output the factorial of the input. so, if i type in 5, it should output 120, which is 5! [5*4*3*2*1 = 120].

write an adding machine program. it should continuously read numbers, and add them up. if the user types in -1, the adding machine stops and outputs the grand total [not counting the -1]. so, if i type in 1 5 7 9 2 -1, the program should output 24 [1+5+7+9+2 = 24]. if i type in 1 2 3 -1, the program should output 6 [1+2+3 = 6].