Lesson 05: Branches

C/C++ language executes program statements in a sequence. Sometimes we need to alter the flow of the sequence of statements. This is possible using branching statements offered by the C/C++ language. The programmer can jump from one part of the program to another with the help of such statements.

In C/C++, branching statements are classified into two types:

  1. Conditional branching
  2. Unconditional branching

Conditional Branching

It includes the statements which work on checking the condition of giving expression to execute the code.

  • if statement
  • if-else statement
  • Nested if statement
  • else-if ladder
  • switch statement

Unconditional Branching

These statements don't check any condition to execute the code. These statements are pretty much useful to transfer the control from one block to another block over the program.

  • break statement
  • continue statement
  • goto statement
  • return statement

Questions

  1. Write a console application to check whether a given number by the user is EVEN or ODD.
  2. Write a console application to ask users to input three integer numbers, then display the largest number.
  3. Write a console application to ask users to input three values for each side of a triangle, then check whether the triangle is valid or not if sides are given.
  4. The following table shows the salary lists of the ABC company. Write a console application. The system will display their position when the user inputs the salary value.
    Salaryabove \$60,001\$50,000 ~ \$60,000\$40,001 ~ \$50,000\$30,001 ~ \$40,000below \$30,000
    Position Manager Assistant Director Office Clerk Part-Time
  5. Write a temperature-conversion program that allows the user to convert Fahrenheit to Celsius or Celsius to Fahrenheit. Then, carry out the conversion. Use floating-point numbers. Interaction with the program might look like this:

    Type 1 to convert Fahrenheit to Celsius,
    2 to convert Celsius to Fahrenheit: 1
    Enter temperature in Fahrenheit: 70
    In Celsius, that's 21.111111

  6. Write a program to calculate the final price after the discount. The discount rates are as follows.
    Customers can enjoy a 10% discount for a total price ≥ \$50 US dollars, a 15% discount for a total price ≥ \$100 US dollars, 20% discount for a total price ≥ \$200 US dollars. The cashier enters the total price of the customer's purchase, and the system displays the payment amount after the discount on the screen.

© 2024 Air Supply Information Center (Air Supply BBS)