CS156: Intro to C, Part I

Spring 2018

Control Structures

See this page as a slide show

CS156 Control Structures

Control Flow

Control Flow: if-else

if (condition)
    statement;      // Do this if the condition is true
if (condition)
    statement;      // Do this if the condition is true
else
    statement;      // Do this if the condition is false

Control Flow: Conditions

Control Flow: Conditional Operators

Happens first
( )
* / %
+ -
> <= >=
== !=
!
&&
||
=
Happens last
OperationMeaning
a == ba is equal to b
a != ba is not equal to b
a < ba is less than b
a > ba is greater than b
a <= ba is less than or equal to b
a >= ba is greater than or equal to b
!atrue if a is false, false if a is true
a && btrue if a and b are both true
a || btrue if a or b is true

Logical Operators

x!x
falsetrue
truefalse
xyx && yx || y
falsefalsefalsefalse
falsetruefalsetrue
truefalsefalsetrue
truetruetruetrue

Example

What is the output of the following program?

int x=5, y=0;
printf("x=%d\n", x);
printf("y=%d\n", y);
printf("!x=%d\n", !x);
printf("!y=%d\n", !y);
printf("x||y=%d\n", (x||y));
printf("x&&y=%d\n", (x&&y));
x=5
y=0
!x=0
!y=1
x||y=1
x&&y=0

Example 4

printf("1 < 2 = %d\n", 1 < 2);
printf("2 >= 4 = %d\n", 2 >= 4);
printf("1<2 && 2<-3 = %d\n", 1<2 && 2<-3);
printf("1<2 || 2<-3 = %d\n", 1<2 || 2<-3);
1 < 2 = 1
2 >= 4 = 0
1<2 && 2<-3 = 0
1<2 || 2<-3 = 1

Example 5

int a=2, b=4;
if (a < b)
    printf("Good\n");
else
    printf("Bad\n");
Good

Control Flow: Common Problems

if (condition)                    if (condition) {
    if (condition2)                   if (condition2)
        statement;                      statement;
else                              }
    statement;                    else
                                      statement;

Control Flow: Common Problems

#include <stdio.h>
int main() {
   int x,y,z; char smallest;
   printf("Enter x: ");
   scanf("%d", &x);
   printf("Enter y: ");
   scanf("%d", &y);
   printf("Enter z: ");
   scanf("%d", &z);
   if (x < y)
        if (x < z)
            smallest = 'x';
   else if (y < z)
            smallest = 'y';
   else 
            smallest = 'z';
    printf("%c is smallest\n", smallest);
    return 0;
}

Comparing characters

You can compare characters to each other:

    char grade='C';
    if (grade > 'C')          // failing grade?
        printf("You'll be back!\n");

Order of characters

Example - characters

Need to handle reading in the enter key!

    #include <stdio.h>
    int main() {
        char letter, enter;
        printf("Enter a letter: ");
        scanf("%c%c", &letter, &enter);
        while (letter != '.') {
            if (letter >= 'a' && letter <= 'z')
                printf("%c is LOWER case\n", letter);
            else if (letter >= 'A' && letter <= 'Z')
                printf("%c is UPPER case\n", letter);
            else
                printf("%c is not in my alphabet.\n", letter);
            printf("Enter another letter (or . to quit): ");
            scanf("%c%c", &letter, &enter);
        }
        return 0;
    }

No shortcuts

Here are some things that you’d think might work, but they don’t:

WrongRight
if (x==1||2)if (x==1 || x==2)
if (a<b<c)if (a<b && b<c)

The old switch-er-roo!

The switch construct lets you pick one from a number of alternatives:

    switch (some value) {
        case this value: 
            statements;
            break;
        case that other value: 
            statements;
            break;
        default: 
            statements;
    }

The value must be an int or char, or an expression, e.g., j+2, or toupper(c).

The Switch Statement

A switch statement makes a multi-way decision:

int year;
scanf("%d", &year);
switch (year) {
    case 1957: printf("Finest year in all history\n");  break;
    case 1969: printf("One small step for [a] man\n");  break;
    case 2003: printf("Punks born in this year\n");     break;
    case 2004: printf("Slackers born in this year\n");  break;
    case 2005: printf("Clowns to the left of me\n");    break;
    case 2006: printf("Jokers to the right\n");         break;
    case 2025: printf("Welcome to the FUTURE!!!\n");    break;
    default:   printf("Boring year\n");                 break;
}

Switching on chars

char grade = 'A';
switch (grade) {
    case 'A': printf("Good Job!\n");  break;
    case 'B': printf("Also Good.\n"); break;
    default : printf("Study more.\n");
}
Good Job!
char grade = 'C';
switch (grade) {
    case 'A': printf("Good Job!\n");  break;
    case 'B': printf("Also Good.\n"); break;
    default : printf("Study more.\n");
}
Study more.

Break is important

char grade = 'A';
switch (grade) {
    case 'A': printf("Good Job!\n");
    case 'B': printf("Also Good.\n");
    default : printf("Study more.\n");
}
Good Job!
Also Good.
Study more.

The break statement is important. Without it, execution would continue through the statements of the switch until the end.

The Switch Statement : Example: vowels.c

#include <stdio.h>
int main() {
   char letter, enter;
   printf("Enter a character: ");
   scanf("%c%c", &letter, &enter);
   switch (letter) {
      case 'a': case 'A': case 'e': case 'E':
      case 'i': case 'I': case 'o': case 'O':
      case 'u': case 'U': 
         printf("A vowel: %c\n", letter);
         break;
      default:
         printf("Not a vowel: %c\n", letter);
   }
   return 0;
}

Sometimes we want to leave out the break statement.

Careful!

if (a > b)
    if (b > c)
        min = c;
else
    min = a;

Careful!

if (a > b) {
    if (b > c)
        min = c;
}
else
    min = a;

Careful!

int a = 5;
int b = 9;
if (a = b)
    val = b;

Careful!

int a = 5;
int b = 9;
if (a = b)      // Perhaps a == b would be better
    val = b;

Look at that if statement carefully: that’s =, not ==. That will:

Careful!

int x=3;
if (x >= 4);
    printf("Hello\n");
Hello

Careful!

int x=3;
if (x >= 4);
    printf("Hello\n");
// Is just like this:
int x=3;
if (x >= 4)
    /* Nothing */;
printf("Hello\n");

Careful!

Remember that scanf requires the ampersand, but printf forbids it.

To illustrate the difference:

int x=11, y=22, z=33;
printf("%d %d %d\n", x, y, z);
printf("%p %p %p\n", &x, &y, &z);
11 22 33
0x7ffce4b75f7c 0x7ffce4b75f78 0x7ffce4b75f74

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-02-24T10:56

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building