| Main |< C & C++ if and if-else 2 | C & C++ if and if-else 4 >| Site Index | Download |


 

 

 

 

 

 

 

C LAB WORKSHEET 8a

C & C++ Selection: C/C++ if and if-else Part 3

 

 

 

 

 

 

Items in this page:

 

  1. The C & C++ conditional statement, a selection and flowcharts.

  2. The if, if-else construct, variations and flowcharts.

  3. Activities, questions and answers.

  4. Tutorial reference that should be used together with this worksheet are: C & C++ program control 1 and C & C++ program control 2.

 

 

 

 

 

 

 

 

 

 

 

 

 

  1. The following experiment should give the same result as the previous one. Only the logic has been rearranged. Complete the flowchart and the program so that the output is same as before.

 

#include <stdio.h>

 

int main(void)

{

      int i, k, a = 0, b = 0, lower = 0;

      printf("Enter the sample input line by line:\n");

      for(i = 1; i <= 7; i = i + 1)

      {

            scanf_s("%d", &k, 1);

            if(k < 90)

                  if(k < 80)

                        ________________________________

                  else

                        ________________________________

            else

            ____________________________________

      }

      printf("A's = %d\tB's = %d\tLower = %d\n", a, b, lower);

      return 0;  

}

 

C program control if-else-if flowchart cifelseswitchcase 6

if(k < 90)

       if(k < 80)

             lower = lower + 1;

      else

             b = b + 1;

else

    a = a + 1;

if-else program output example

 

C programming - if-else , true, false flowchart example

   
  1. Next, let us test the conditions for all five grades, namely A, B, C, D and F. In the blank space, for each grade, place the appropriate statement something like the following:

 

printf("A.\n");

 

Each else that is lined up under an if is that condition’s false side. Complete the code and the flowchart.

 

#include <stdio.h>

 

int main(void)

{

      int i, k;

      printf("Enter the sample input line by line:\n");

      for(i = 1; i <= 7; i = i + 1)

      {

            scanf_s("%d", &k, 1);

            if(k < 90)

                  if(k < 80)

                        if(k < 70)

                              if(k < 60)

                                    ____________________________

                              else

                              _____________________________

                        else

                        _____________________________

                  else

                  ______________________________

            else

            ____________________________

      }

      return 0;  

}

 

if(k < 90)

    if(k < 80)

        if(k < 70)

            if(k < 60)

                printf("Grade F.\n");

            else

                printf("Grade D.\n");

        else

            printf("Grade C.\n");

    else

        printf("Grade B.\n");

else

    printf("Grade A.\n");

C programming - many if-else combination program output sample

 

 

 

C program control if-else-if flowchart cifelseswitchcase 7

  • The following is the completed flowchart.

C programming - if-else , true, false flowchart with answers

 

 

  1. On the F (false) side of k < 90? Only A grades are selected. On the T (true) side of that condition, which grades are selected, A, B, C, D and/or F?

  2. On the T side of k < 80?, which grades are selected?

  3. On the T side of k < 70?, which grades are selected? What about on the F side?

  4. Grades that end up getting a B must go through how many decision diamonds?

  5. Grades that end up getting a D must go through how many decision diamonds?

 

Ans:

  1. B, C, D and F.

  2. C, D and F.

  3. On the True side are D and F. On the False side, C was selected.

  4. 2 decision diamonds.

  5. 4 decision diamonds.

   
  1. The following experiment performs the same steps as in the previous one. However, since the logic is rearranged, the printf() will need to be placed at different locations. Complete the code and the flowchart.

 

#include <stdio.h>

 

int main(void)

{

      int i, k;

      printf("Enter the sample input line by line:\n");

      for(i = 1; i <= 7; i = i + 1)

      {

            scanf_s("%d", &k, 1);

            if(k > 69)

                  if(k > 89)

                        ________________________

                  else if(k > 79)

                        ________________________

                  else

                  __________________________

            else if(k > 59)

                  __________________________

            else

                ____________________________

      }

      return 0;  

}

 

if(k > 69)

    if(k > 89)

        printf("Grade A.\n");

    else if(k > 79)

        printf("Grade B.\n");

    else

        printf("Grade C.\n");

    else if(k > 59)

        printf("Grade D.\n");

    else

        printf("Grade F.\n");

C programming - if-else-if , true, false program output example

C program control if-else-if flowchart cifelseswitchcase 8

  • The following is a completed flowchart for the previous question.

C programming - if-else-if-else , true, false flowchart sample answers

  1. F grades will go through two conditions: k > 69?, which would be false and k > 59?, which also would be false. How many conditions that D grades go through?

  2. How many conditions that C grades go through?

  3. How many conditions that A grades go through?

  4. If k > 79? were changed to k <= 80?, then what changes would be necessary in the flowchart?

  5. When a grade that is read into the variable k enters this set of nested if’s, it has a choice of going through how many different paths?

  6. The control of execution may take how many different paths at any one time?

 

Ans:

  1. Also 2 conditions same as F but both are True.

  2. 3 conditions.

  3. 2 conditions.

  4. The True (T) and False (F) positions need to be exchanged for the k > 79 decision diamond.

  5. 5 different paths based on the path toward the Stop.

  6. At any one time it will take 2 paths based on the True (T) and False (F) paths.

 

   
  1. The following experiment performs the same steps as in the last two previous experiments. However, it count the number of grades in each category instead of printing them. Complete the code and the flowchart.

 

#include <stdio.h>

 

int main(void)

{

      int i, k, a=0, b=0, c=0, d=0, f=0;

      printf("Enter the sample input line by line:\n");

      for(i = 1; i <= 7; i = i + 1)

      {

            // for older compiler you can try using scanf()

            scanf_s("%d", &k, 1);

            if(k <= 59)

                  ____________________________

            else if(k <= 89)

                  if(k <= 69)

                        _______________________________

                  else if(k <= 79)

                        _______________________________

             else

                  _______________________________

            else

               _____________________________      

      }

      printf("A's = %d\t", a);

      printf("B's = %d\t", b);

      printf("C's = %d\t", c);

      printf("D's = %d\t", d);

      printf("F's = %d\n", f);

      return 0;  

}

 

if(k <= 59)

    f = f + 1;

else if(k <= 89)

    if(k <= 69)

        d = d + 1;

    else if(k <= 79)

        c = c + 1;

    else

        b = b + 1;

    else

        a = a + 1;

 

 

---------------------------------------------------------

C programming - if-else-if combination program output sample

 

 

C program control if-else-if flowchart cifelseswitchcase 9

 

 

  • The following is the answer for the flowchart diagram.

C programming - if-else flowchart for counting the grades example

 

For each of these questions, choose from among the grades of A, B, C, D and F.

  1. Which grades(s) are selected on the true side of k <= 59?

  2. Which grades(s) are selected on the false side of k <= 59?

  3. Which grades(s) are selected on the true side of k <= 89?

  4. Which grades(s) are selected on the false side of k <= 89?

  5. Which grades(s) are selected on the true side of k <= 69?

  6. Which grades(s) are selected on the false side of k <= 69?

  7. Which grades(s) are selected on the true side of k <= 79?

  8. Which grades(s) are selected on the false side of k <= 79?

  1. F.

  2. A, B, C and D.

  3. B, C and D.

  4. A.

  5. D.

  6. B and C.

  7. C.

  8. B.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  1. Run the following program and key in the following sample input (without the commas): 16, 21, 17, 43, 7, 52, -1. The program will determine the smallest number entered.

 

#include <stdio.h>

 

int main(void)

{

      int k, smallest;

      printf("Enter integers, when");

      printf(" done enter a ");

      printf("negative number\n");

      scanf_s("%d", &k, 1);

      // assign the first number to smallest variable

      smallest = k;

      // iterate while k >= 0

      for( ; k >= 0; )

      {

            // if the entered number is < smallest

            if(k < smallest)

                  // then assign the number to smallest variable...

                  smallest = k;

            // read the next input....repeat

            scanf_s("%d", &k, 1);

      }

      // print the smallest number...

      printf("The smallest number is %d\n", smallest);

      return 0;  

}

 

  1. Draw a tracechart for this experiment (left to you!).

  2. What was the first value of the variable smallest?

  3. The first time that the condition in the if statement was encountered, what were the values of k and smallest?

  4. The second time that the if condition was tested, what were the values of k and smallest?

  5. The third time that the if condition was tested, what were the values of k and smallest?

  6. During the loop, the value of smallest was changed. What were the different values of smallest?

  7. Does an if statement require a corresponding else statement? Why?

  8. Is the scanf_s() executed inside the loop when the k < smallest is true or false, or irrespective of it?

  9. If the data entered were 11, 22, 13, 19, 16, -1, how many times would smallest be changed?

 

To see the flow of this program clearer and used for troubleshooting you can add several line of codes as shown below.

 

#include <stdio.h>

 

int main(void)

{

    int k, smallest;

    printf("Enter integers, when");

    printf(" done enter a ");

    printf("negative number\n");

    scanf_s("%d", &k, 1);

    // assign the first number to smallest variable

    smallest = k;

    printf("smallest = %d, k = %d at pos1.\n", smallest, k);

    // iterate while k >= 0

    for( ; k >= 0; )

    {

        // if the entered number is < smallest

        printf("smallest = %d, k = %d at pos2.\n", smallest, k);

        if(k < smallest)

          // then assign the number to smallest variable...

          smallest = k;

        printf("smallest = %d, k = %d at pos3.\n", smallest, k);

        // read the next input....repeat

        scanf_s("%d", &k, 1);

        printf("smallest = %d, k = %d at pos4.\n", smallest, k);

    }

    // print the smallest number...

    printf("The smallest number is %d\n", smallest);

    return 0;

}

// Sample inputs: 16, 21, 17, 43, 7, 52, -1 and 11, 22, 13, 19, 16, -1

C program control if-else-if flowchart cifelseswitchcase 10

 

 

C programming - if-else program output sample for smallest number

 

 

  1. Left for your assignment.

  2. 16

  3. k = 16, smallest =16.

  4. k = 21, smallest = 16.

  5. k = 17, smallest = 16.

  6. 16 and 7.

  7. Not really. It can be standalone in testing just a condition.

  8. From the flowchart we can see that the scanf_s() executed inside the loop irrespective of k < smallest is True or False.

  9. 0 time. It is already a smallest number entered as the first input.

 

 

C programming - if-else program output sample for debugging C program

 

 

 

 

 

 

 

| Main |< C & C++ if and if-else 2 | C & C++ if and if-else 4 >| Site Index | Download |


The C Selection if, if-else, if-else-if, break, conditional/ternary operator and switch-case-break: Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6