Skip to content

Instantly share code, notes, and snippets.

@rszewczyk
Last active October 17, 2017 06:34
Show Gist options
  • Save rszewczyk/5a54e1ce5c3f1120be3eec2db0127eeb to your computer and use it in GitHub Desktop.
Save rszewczyk/5a54e1ce5c3f1120be3eec2db0127eeb to your computer and use it in GitHub Desktop.
Assignment - Class #12 10/12/2017

Assignment - Class 12: 10/12/2017

In this assigment you will learn about conditional branching logic in C.

As part of this assigment you will create a program in a file called branching.c. When you complete the assigment you will submit branching.c by uploading it to Slack. If you do not finish today, you have until the end of class (6:45pm) on Tuesday, 10/17/2017 to turn it in. Note this doesn't necessarily mean you will have time to work on it in class on 10/17. If don't finish today, you should try and finish it before Tuesday.

Step 1

Copy and paste the code below into a new file and save it. Give it the name branching.c. Upload this file to the UMBC Linux server and compile it. Then run the program to make sure it works.

#include <stdio.h>

int main()
{
  int theNumber;

  printf("Enter a number: ");
  scanf("%d", &theNumber);

  printf("You entered %d\n", theNumber);

  return 0;
}

Modify the program by inserting the following section of code at line 11:

  if (theNumber < 10)
  {
    printf("%d is less than 10\n", theNumber);
  }

Save and upload your file to the UMBC Linux server. Then compile and run it. When prompted, type the number 5 and press return. You should see the following output on your screen:

Enter a number: 5
You entered 5
5 is less than 10

Run the program again and this time, when prompted, type the number 11 and press return. Note the difference in output:

Enter a number: 11
You entered 11

Note that the program is executing differently depending on the input you give it. There are two new concepts here.

The first is we've introduced a new type of statement, the if statement. Look at line 12 of the program: if (theNumber < 10). An if statement is formed by the keyword if followed by an expression in parenthesis called the conditional, followed by a block of code surrounded with {}. If the conditional is true, the code in the block is executed. If the conditional is false, the code on line 14 is skipped and the program continues at line 16.

The second new thing is the < operator. This is the less than operator. The result of the less than operator is true if the left side is less than the right side. You can combine operators like < with if statements to take alternate routes through the code - these alternate routes are called branches.

Step 2.

At this point your program should look like the following (if not, make it so):

#include <stdio.h>

int main()
{
  int theNumber;

  printf("Enter a number: ");
  scanf("%d", &theNumber);

  printf("You entered %d\n", theNumber);

  if (theNumber < 10)
  {
    printf("%d is less than 10\n", theNumber);
  }

  return 0;
}

Modify the program by inserting the following section of code at line 16:

  else
  {
    printf("%d is greater than or equal to 10\n", theNumber);
  }

Save and upload your file to the UMBC Linux server. Then compile and run it. When prompted, type the number 5 and press return. The output should be the same as the first time you entered 5:

Enter a number: 5
You entered 5
5 is less than 10

Now run the program a second time and, when prompted, type the number 11. You should see the following output:

Enter a number: 11
You entered 11
11 is greater than or equal to 10

Starting on line 16, we've added another part to our if statement. The keyword else represents the start of the alternate branch to the if statement. The code in the block following else is executed when the conditional (x < 10)) is false.

Let's review what we've done so far:

  • When you entered 5, the condition was true, so line 14 was executed and line 18 was skipped.
  • When you entered 11, the condition was false, so Line 14 was skipped and line 18 was executed.
  • In both cases, after determining which branch to execute and executing it, the program continues at line 20.
  • The two alternate blocks of code are called branches
  • The expression following the keyword if is called a conditional

Step 3

At this point your program should look like the following (if not, make it so):

#include <stdio.h>

int main()
{
  int theNumber;

  printf("Enter a number: ");
  scanf("%d", &theNumber);

  printf("You entered %d\n", theNumber);

  if (theNumber < 10)
  {
    printf("%d is less than 10\n", theNumber);
  }
  else
  {
    printf("%d is greater than or equal to 10\n", theNumber);
  }

  return 0;
}

Modify line 12 by changing the < to >.

Save and upload your file to the UMBC Linux server. Then compile and run it. When prompted, type the number 5 and press return. The output should now look like:

Enter a number: 5
You entered 5
5 is greater than or equal to 10

The > is the greater than operator. It is true when the left side is greater than the right side. Since we've changed our conditional we've reversed the conditions under which each branch will be taken. However, now our output is incorrect. Modify lines 14 and 18 so that the program no longer prints alternative facts.

Step 4

At this point your program should look like the following (if not, make it so):

#include <stdio.h>

int main()
{
  int theNumber;

  printf("Enter a number: ");
  scanf("%d", &theNumber);

  printf("You entered %d\n", theNumber);

  if (theNumber > 10)
  {
    printf("%d is greater than 10\n", theNumber);
  }
  else
  {
    printf("%d is less than or equal to 10\n", theNumber);
  }

  return 0;
}

Add a new line after the } on line 15 and insert the following code at line 16:

  else if (theNumber == 10)
  {
    printf("%d is equal to 10\n", theNumber);
  }

Save and upload your file to the UMBC Linux server. Then compile and run it. When prompted, type the number 10 and press return. The output should look like:

Enter a number: 10
You entered 10
10 is equal to 10

Here we've introduced another operator: ==. This is the equality operator. It is is true if the left side equals the right side. Notes that it uses two equal signs to avoid confusion with the assignment operator (=). Forgetting to use two equal signs when you meant to do a comparison is a common mistake.

Also note that you can have more than two branches by combining a second conditional with an else to form the second branch. If the first conditional (theNumber > 10) is false and the second conditional (theNumber == 10) is true, then second branch is executed and the other two are skipped.

So, the branching logic is now:

  • If theNumber > 10 is true, execute line 14.
  • Otherwise, if theNumber == 10 is true, execute line 18.
  • Otherwise execute line 22.

Observe that if the second conditional is false, it is no longer possible for the user's input to equal ten at line 22 - otherwise line 18 would have been executed and the program would have then skipped to line 24. Therefore we should change line 22 to print the correct output. Go ahead and do that now.

Step 5

At this point your program should look like the following (if not, make it so):

#include <stdio.h>

int main()
{
  int theNumber;

  printf("Enter a number: ");
  scanf("%d", &theNumber);

  printf("You entered %d\n", theNumber);

  if (theNumber > 10)
  {
    printf("%d is greater than 10\n", theNumber);
  }
  else if (theNumber == 10)
  {
    printf("%d is equal to 10\n", theNumber);
  }
  else
  {
    printf("%d is less than 10\n", theNumber);
  }

  return 0;
}

Beginning on line 24, modify the program to do the following:

  • Prompt the user for a number and store it as an integer
  • Print whether the number is even or odd.

For example, here is what my screen looks like when the program is complete:

➜  class12 ./a.out
Enter a number: 5
You entered 5
5 is less than 10

Enter a number: 8
8 is even
➜  class12 ./a.out
Enter a number: 11
You entered 11
11 is greater than 10

Enter a number: 7
7 is odd

Hint for determining if a number is even

Review the mathematical operators from the first class 6 assignment.

Objectives

After completing the assignment, you will be able to

  • Use an if/else statement to perform conditional logic
  • Use the <, > and == operators to perform comparisons
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment