| Main |< C & C++ while & do-while loop 2 | C & C++ if and if-else 2 >| Site Index | Download |


 

 

 

 

 

 

C LAB WORKSHEET 8

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

 

 

 

 

 

 

Items in this page:

 

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

  2. The if, if-else construct 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.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 
  • In this lab worksheet we will deal wit another C/C++ program control, a selection. The if, if-else construct and its variation used in C/C++ when we need to make a selection from the given options based on the certain condition. For example:

if (x == 3)

      printf("The if condition is fulfilled!\n");

printf("The if condition is not fulfilled!\n");

  • From the code snippet, if x = 3, then the first printf() will be executed else the second printf() will be executed. So a simplest if form is shown below.

if (condition)

{

      statement(s);

}

next_statement;

  • If the condition is true, the statement(s) is executed else the next_statement is executed. Similar to the loop discussed in the previous worksheet, if there is more than one line of code, you need to use the curly braces. Let make the previous example as a full working program.

  • Create a project named ifprog. Then add C++ source file named ifprogsrc to the project. Don’t forget to set your project to be compiled as C code. Then try the following C program.

#include <stdio.h>

 

int main(void)

{

      int x;

      printf("Enter an integer: \n");

      // you can try using scanf() for older compilers...

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

      if(x == 3)

      {

            printf("x = %d\n", x); 

            printf("The if condition is fulfilled!\n");

      }

      printf("This is the next statement, after the if body...\n");

      return 0;

}

  • If 3 is entered the output is shown below. The x == 3 is true then the if statement is executed.

C if-else program control sample output

C if-else program control sample console output

#include <stdio.h>

 

int main(void)

{

      int x;

      printf("Enter an integer: \n");

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

      if(x == 3)

      {

            printf("x = %d\n", x); 

            printf("The if condition is fulfilled!\n");

      }

      else

      {

            printf("x = %d\n", x);

            printf("The if condition is not fulfilled!\n");

      }

      return 0;

}

  • If 3 is entered, the output is shown on the right.

C if-else program control sample output

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • If other than 3 is entered, the output is shown on the right.

Another C if-else program control sample output

if (condition)

{

      statement 1;

}

else

{

      statement 2;

}

next_statement;

#include <stdio.h>

 

int main(void)

{

      int x;

      printf("Enter your mark: ");

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

      if(x <= 50)

      {

            printf("Your grade is F - FAIL.\n");

            printf("You need to repeat the paper\n");

      }

      else if(x <= 60)

            printf("Your grade is E - CONDITIONAL PASS.\n");

      else if(x <= 70)

            printf("Your grade is D - PASS.\n");

      else if(x <= 80)

            printf("Your grade is C - GOOD.\n");

      else if(x <= 90)

            printf("Your grade is B - VERY GOOD.\n");

      else if(x <= 100)

            printf("Your grade is A - EXCELLENT.\n");

      return 0;

}

 

C if-else program control sample input output

Another C if-else program control sample input output

More C if-else program control sample input output

if (condition1)

{

      statement 1;

}

else if (condition2)

{

      statement 2;

}

else if(...)

}

      statement ...;

}

next_statement;

#include <stdio.h>

 

int main(void)

{

      int year, train;

      char curr_post;

      printf("Are you qualified for a Manager promotion?\n");

      printf("Enter the number of year: ");

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

      printf("Enter the number of training received: ");

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

      printf("Enter your current post(J - Junior, S - Senior, A - Asst. Manager): ");

      // don't forget the space before %c else the execution will stop prematurely...

      scanf_s(" %c", &curr_post);

      if(year >= 10)

            if(train >= 5)

                  if(curr_post == 'A' || 'a')

                        printf("Qualified to be promoted to Manager!\n");

                  else

                  {

                        printf("Become Assistant Manager first.\n");

                        printf("Need more training.\n");

                        printf("Need more experience.\n");

                  }

            else

            {

                  printf("Need more training.\n");

                  printf("Need more experience.\n");

            }

      else

            printf("Need more experience.\n");

      return 0;

}

 

C if-else program control sample input output - multiple if

 

 

 

 

 

| Main |< C & C++ while & do-while loop 2 | C & C++ if and if-else 2 >| 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