| Main |<C & C++ if and if-else 1 |C & C++ if and if-else 3 >|Site Index |Download |


 

 

 

 

 

 

C LAB WORKSHEET 8_1

C & C++ Selection: if, if-else, if-else-if Part 2

 

 

 

 

 

 

 

Items in this page:

 

  1. The if,if-else construct and the variations.

  2. Activities, questions and answers.

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

 

 

 

 

 

 

More Examples And Practices

 

  • In the following example we will experiment with the if statement, which is similar to the for loop because they both test for conditions before doing the execution. Here, the difference between the two is that the for loop is a loop and the if statement is not. We will use a grading system where grades are given in the Table on the right.

  • For the following experiments, enter the following sample input data line by line, that mean enter each data followed by pressing the return key.

79 91 80 59 100 60 89 45 90

Mark

Grade

0 - 59

F

60 - 69

D

70 - 79

C

80 – 89

B

90 – 100

A

 

Table 1

  
  1. In the following experiment, if the condition is true, then the Fail! is printed otherwise Pass! is printed. Show the output for the following program.

 

#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 < 60)

                  printf("Fail!\n");

           else

                  printf("Pass!\n");

      }

     return 0;

}

  1. If the mark is less than 60, what is printed?

  2. If the mark is 60 or more, what is printed?

A sample input and output.

 

C if-else program control sample input output

 

  1. Fail!

  2. Pass!

 

 

  1. In the following code, only < 60 is replaced. Complete the following program and its flowchart so that the output here is the similar to the previous program. No output is necessary here.

 

#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>= 60)

                  __________________________

           else

                  __________________________

      }

     return 0;

}

 

Note: The small circle means a continuation from other part of the full flowchart.

 

C program control if-else-if flowchart

  1. If the mark is 60 or more, what is printed?

  2. If the mark isn’t 60 or more, what is printed?

 

           if(k >= 60)

                  printf("Pass!\n");

            else

                  printf("Fail!\n");

 

the c & c++ if, if-else and if-else-if program output sample

 

if-else flowchart diagram

 

  1. Pass!

  2. Fail!

 

  1. One doesn’t have to have a matchingelse and if. However, you could have two consecutive if’s. Again, complete the two if’s so that the result would be the same as in previous program.

 

#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 >= 60)

                  _____________________________

           if(k < 60)

                  _____________________________

      }

     return 0;

}

           if(k >= 60)

                 printf("Pass!\n");

           if(k < 60)

                 printf("Fail!\n");

 

using two if for two different conditions program output sample

  
  1. Complete the following two if’s in the program and the simplified flowchart so that the output or result will be same as in previous example.

 

#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<= 59)

                  _____________________________

           if(k>= 60)

                  _____________________________

      }

     return 0;

}

 

C program control if-else-if flowchart cifelseswitchcase 2

  1. Could you replace the second if with simply else?

  2. Could you replace the first if with simply else?,

           if(k<= 59)

                 printf("Fail!\n");

           if(k>= 60)

                 printf("Pass!\n");

 

two if with different relational operators

 

two if with relational operator in the flowchart diagram

  1. Yes.

  2. No.

  1. In the following example, let count the number of passes and fails. Place passes = passes + 1; and fails = fails + 1; under the proper if’s statements and complete the flowchart.

 

#include<stdio.h>

 

int main(void)

{

     int i, k, passes = 0, fails = 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 <= 59)

                  _____________________________

           if(k >= 60)

                  _____________________________

      }

      printf("The number of passes = %d\n", passes);

      printf("The number of fails = %d\n", fails);

     return 0;

}

 

C program control if-else-if flowchart cifelseswitchcase 3

           if(k <= 59)

                 fails = fails + 1;

           if(k >= 60)

                 passes = passes + 1;

 

another variation of the if statements program output sample

 

a flowchart diagram for two if statements

 

 

 

 

 

 

 

  1. In the next experiment, let us see who got 'A' and who didn’t. Complete the two if’s using either > or <. Remember that those who received 90 or above receive an A. When you try your solution by running it, you should end up with 2 who received A’s and 5 who didn’t receive A’s.

 

#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_____________)

                  printf("It's an A!\n");

           if(k ____________)

                  printf("It's not an A!\n");

      }

     return 0;

}

  1. How would the if’s be written if you had used>= and <= instead?

if(k > 89)

    printf("It's an A!\n");

if(k < 90)

    printf("It's not an A!\n");

different relational operators used in the if statements program output sample

 

  1. if(k >= 90)

        printf("It's an A!\n");

    if(k < 90)

        printf("It's not an A!\n");

 

if(k <= 89)

    printf("It's not an A!\n");

if(k > 89)

    printf("It's an A!\n");

  
  1. You should notice that if k is greater than 89, then an A gets printed. Otherwise, out of those who got less than 90, the next if selects the B’s and the final else selects those who received less than 80. Show the output and complete the flowchart for only the if statements.

 

#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 > 89)                               // only A's

                  printf("It's an A!\n");

           elseif (k > 79)                     // non A's, only B's

                  printf("It's B!\n");

           else                                     // others

                  printf("It's not an A or B!\n");

      }

     return 0;

}

 

C program control if-else-if flowchart cifelseswitchcase 4

the C if-else program output example

 

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

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

C if-else flowchart diagram example

 

  
  1. The following experiment will count the number of A’s,B’s and the lower grades. The logic has been rearranged. Complete the flowchart and the program. Place the following three statements properly:

 

lower = lower + 1;a = a + 1; and b = b + 1;

 

#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 >= 80)

                 if(k >= 90)

                        ________________________________

                 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 5

if(k >= 80)

    if(k >= 90)

         a = a + 1;

    else

          b = b + 1;

else

   lower = lower + 1;

 

if-else program output sample

 

two if-else condition flowchart diagram

 

 

 

| Main |<C & C++ if and if-else 1 |C & C++ if and if-else 3 >|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