Title: Selection Structures
Course: COP1000: Introduction to Programming > Section: Module Nine

Introduction

In computer science, conditional statements, conditional expressions and conditional constructs are features of a programming language that perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false. Apart from the case of branch predication, this is always achieved by selectively altering the control flow based on some condition. (http://en.wikipedia.org/wiki/Conditional_%28programming%29)

The if–then construct (sometimes called if–then–else) is common across many programming languages. Although the syntax varies quite a bit from language to language, the basic structure (in pseudocode form) looks like this: (http://en.wikipedia.org/wiki/Conditional_%28programming%29)

IF (boolean condition)
(consequent)
ELSE
(alternative)
END IF

The common Boolean conditional operators are equal to, less than or equal to, greater than or equal to, not equal to, and not.  An example of these different operators is (==,<=,>=,!=, !).  Conditional expressions usually follow the following syntax:
5 == 6, this statement evaluates to false as 5 is not equal to 6

In programming, a switch, case, select or inspect statement is a type of selection control mechanism that exists in most programming languages. Its purpose is to allow the value of a variable or expression to control the flow of program execution via a multiway branch. The main reasons for using a switch include improving clarity, by reducing otherwise repetitive coding, and also offering the potential for faster execution through easier compiler optimization in many cases. (http://en.wikipedia.org/wiki/Switch_statement)

In most languages, a switch statement is defined across many individual lines using one or two keywords. A typical syntax is:
1. The first line contains the basic keyword, usually switch, case or select, followed by an expression which is often referred to as the control expression or control variable of the switch statement.
2. Subsequent lines define the actual cases (the values) with corresponding sequences of statements that should be executed when a match occurs.

Objectives

Upon completion of this module, the student will be able to:

Define the following: "IF Statement", "ELSE statement", "Switch Statement"
Employ the conditional operators
Write code that makes a decision based on a Boolean expression using the IF/ELSE
Recognize the structure of a SWITCH and where it is useful

Readings & Resources

Chapter 4: Guess the Number p. 37-44