Skip to main content Accessibility help
Internet Explorer 11 is being discontinued by Microsoft in August 2021. If you have difficulties viewing the site on Internet Explorer 11 we recommend using a different browser such as Microsoft Edge, Google Chrome, Apple Safari or Mozilla Firefox.

Chapter 4: Decision Making Control Statements

Chapter 4: Decision Making Control Statements

pp. 110-145

Authors

, Vidyalankar Institute of Technology
  • Add bookmark
  • Cite
  • Share

Summary

Overview

We understand from Chapters 1 and 2 that instructions in a program are executed in a sequence in which we write them in the main() function. Such an execution is called a sequential execution. In many cases it is required to change the sequence of executing instructions based on some condition. The statements that can be used to alter the sequence of executing instructions at run time of the program are called branching or decision making control statements.

C/C++ supports a following branching/decision making control statements:

  • 1. if – else statement

  • 2. else-if ladder

  • 3. switch statement

  • 4. Ternary operator (?:)

  • 5. goto statement

  • In this chapter we discuss the syntax and working of each of the above decision making control statements supported by C/C++.

    if else Statement

    The if-else statement is used to change the sequence of execution by evaluating a specified condition at run time of the program. The condition to be evaluated must be specified in round parenthesis when defining the if block as shown in Figure 4.1. The if-else statement defines two blocks, the first block is called if block and the second block is called else block. The control of execution is transferred inside the if block if the condition specified in the parentheses is evaluated as true, whereas the control of execution is transferred inside the else block if the condition specified is evaluated as false.

    A condition is generally written using relational operators and hence an evaluation of a condition can result into two possible outcomes as ‘true’ or ‘false’. Therefore, it will be correct to say that, in any of the given scenario the control of execution will always be transferred to at least one of the two blocks (if block or the else block). Also, the control of execution will be transferred to exactly one of the two blocks (if block in case the condition is evaluated as true or it can be a else block in case the condition is evaluated as ‘false’).

    About the book

    Access options

    Review the options below to login to check your access.

    Purchase options

    eTextbook
    US$110.00
    Paperback
    US$110.00

    Have an access code?

    To redeem an access code, please log in with your personal login.

    If you believe you should have access to this content, please contact your institutional librarian or consult our FAQ page for further information about accessing our content.

    Also available to purchase from these educational ebook suppliers