Lesson 4. Using conditions to process data

Overview

  • Exploring multiple ways to make decisions in C

Desiure for making decisions

Making decision is an ordinary thing for a person to do in one’s typical day; from deciding what to wear to a life-changing decisions. For a program, this is also an essesncial piece to have. In this lesson, we will learn how a decision making is done.

Different types of decision making

There are several ways to make a decision in C. We will explore some of common ways to do so.

if stastement

An if statement is a most common way to make decision. you can use if statement by:

1
2
3
4
5
6
7
8
9
if(condition){
  Do something
}
else if(condition){
  Do something
}
else{
  Do something
}

The condition in the brackets should return true as its result in order for the operation to be executed under that if statement.

Conditions

In C, there are numeroius ways to write a condition. Before makeing any further statements, you should know how to compare the values.

Operation Example Explanation
== A==B Checks if vale of A is equal to B. If the statement is true, this will return true and this will return false when A and B values does not match.
!= A!=B If A has a different value from B, this will return true and will return false when A and B has the same value
> A>B This returns true when A value is larger than B, and returns false when B is larger or equal to A
< A<B This returns true when A value is smaller than B, and returns false when B is smaller or equal to A
>= A>=B This returns true when A value is larger or equal to B, and returns false when B is larger than A
<= A<=B This returns true when A value is smaller or equal to B, and returns false when B is larger than A
&& (A<B)&&(A<C) This will return true if the first and second condition are both true. If one of the conditions are false, this whole condition would return fasle. In this example, A has to be smaller than both B and C to make this statement true.
|| (A<B)||(A<C) This will return true if one of the first and second condition is true. If both of the conditions are false, this whole condition would return fasle. In this example, A should be smaller than B or C to makie this statement true.

else if and else statements

With If and else statements, we can use else if and else statements. For each if statements, you can add infinite numbers of else if statements. else if statement checks the conditions similar to an if statement. However, else if statement after an if statement check the cases that have cased false statements in if statement.

1
2
3
4
5
6
7
8
9
10
11
12
if(A<100){
  Operation 1
}
else if(A<1000){
  Operation 2
}
else if(A<10000){
  Operation 3
}
else{
  Operation 4
}

For example, if variable A had a value of 50, the Operation 1 would have been executed. However, if A had a value of 300, Operation 2 wil be executed. Also, an A with the value of 9000 will execute the Operation 3.

Finally, else statement can only be used at the end of an if statement block. It can also be used once per every if statement block. This does not have any condition checking process with it. Instead, every condition that does not fit an if or else if statement will cause operation under else statement to be executed. For example, A with a value of 100000 will execute Operation 4.

switch case statement

Similar to if statement, switch case statement is a way to check conditions. However, it has some limitations. It can only check condition for integer variable and == comparison:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
switch(A){
  case a:
    Operation 1;
    break;
  case b:
    Operation 2;
    break;
  case c:
    Operation 3;
    break;
  default:
    Operation 4;
    break;
}

due to the restrictions, all switch case statements can be converted to if statement, but not all if statements can be converted to switch case statement. However, switch case statement provides a better readability and slight better performance, it is advised to use switch case statement when it is possible.

switch statement

switch statement is not involved in comparison process, but states which variable the comparison is done for.

case statement

case statement provides the value that the variable from switch statement is being compared with. For example, the variable from the example code above is being compared with a, b and c values. If A has the value of a, Operation 1 will be executed. And if A has the value of b, Operation 2 will be executed. if multiple case statements have same values, the first one from the top will be executed and other case statements will be ignored.

default statement

default statement has a similar role to else statement from if statement block. It can only be used once for each switch statement block and all values that did not activate any of case statements will execute the operation under default statement.

Sample problems

The following section is containing sample problems you might want to try for your self. Sample codes using both if and switch statements will be given when possible.

Problem 1

Get two integer inputs A and B from the user and compare them.

Input

A and B are given in the first line of input and are separated by a single space.

Output

Print one of following on the first line.

  • When A is larger than B, print ‘>’.
  • When A is smaller than B, print ‘<’.
  • When A is equal to B, print ‘==’.

Limitation

  • -10000<=A,B<=10000

Sample IO

Sample input Sample output
1 2 <
10 2 >
5 5 ==

Sample Code

Using if statement

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>

int main()
{
    int A,B;
    scanf("%d %d",&A,&B);
    if(A<B){
        printf("<");
    }
    else if(A>B){
        printf(">");
    }
    else{
        printf("==");
    }
    return 0;
}

Using switch case statement

N/A

Problem 2

Get a test score input from the user and label them to given rule:

  • 90~100: A
  • 80~89: B
  • 70~79: C
  • 60~69: D
  • Others: F

Input

A test score will be given in the first line. Each test score is an integer that is larger or equal to 0, and smaller or equal to 100.

Output

Print the mark for each test score.

Sample IO

Sample input Sample output
100 A
0 F
68 D

Sample Code

Using if statement

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>

int main()
{
    int A;
    scanf("%d",&A);
    if(A>89){
        printf("A");
    }
    else if(A>79){
        printf("B");
    }
    else if(A>69){
        printf("C");
    }
    else if(A>59){
        printf("D");
    }
    else{
        printf("F");
    }
    return 0;
}

Using switch case statement

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>

int main()
{
    int A;
    scanf("%d",&A);
    switch(A/10){
        case 10:
          printf("A");
          break;
        case 9:
          printf("A");
          break;
        case 8:
          printf("B");
          break;
        case 7:
          printf("C");
          break;
        case 6:
          printf("D");
          break;
        default:
          printf("F");
          break;
    }
    return 0;
}

Problem 3

Get an coordinate inputted by the user and determine which quadrant this coordinate is located in.1

Input

A x coordinate will be given in the first line and y coordinate will be given in the second line.

Output

Print a quadrant number (1, 2, 3 or 4) for given point (x, y)

Limitation

(−1000 ≤ x ≤ 1000; x ≠ 0)

(−1000 ≤ y ≤ 1000; y ≠ 0)

Sample IO

Sample input Sample output
12
5
1
9
13
4

Sample Code

Using if statement

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>

int main()
{
    int x,y;
    scanf("%d",&x);
    scanf("%d",&y);
    if(x>0&&y>0){
        printf("1");
    }
    else if(x<0&&y>0){
        printf("2");
    }
    else if(x<0&&y<0){
        printf("3");
    }
    else{
        printf("4");
    }
    return 0;
}

Using switch case statement

N/A

Summing up

This lesson might be hard for some of you. I highly recommend you to practice things you have learned in this lesson on your own. Our next lesson (Lesson 5) will be about loops.

Thank you for following my lesson and please comment or react to my post in the comment section below!