0% found this document useful (0 votes)
35 views9 pages

Decision Making: Instructor: Mr. Neil A. Basabe, MIT

The document discusses different types of decision making statements in programming including if, if/else, and switch statements. It provides details on the syntax and usage of each statement type, and how they can be nested or used together. Key points covered include using if statements to execute code conditionally based on a boolean expression, if/else statements allowing two alternative paths, and switch statements providing an alternative to nested ifs when checking a single variable against multiple values.

Uploaded by

Neil Basabe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views9 pages

Decision Making: Instructor: Mr. Neil A. Basabe, MIT

The document discusses different types of decision making statements in programming including if, if/else, and switch statements. It provides details on the syntax and usage of each statement type, and how they can be nested or used together. Key points covered include using if statements to execute code conditionally based on a boolean expression, if/else statements allowing two alternative paths, and switch statements providing an alternative to nested ifs when checking a single variable against multiple values.

Uploaded by

Neil Basabe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Decision Making

Instructor: Mr. Neil A. Basabe, MIT


if statement
• if statement
– The simplest statement
to make a decision
– A Boolean expression
appears within
parentheses
– No space between the
keyword if and the
opening parenthesis
– Execution always
continues to the next
independent statement
– Use a double equal sign
( == ) to determine
equivalency
if…else Statement
Single-alternative if
• Perform an action, or not
• Based on one alternative

Dual-alternative if
• Two possible courses of action

if…else statement
• Performs one action when a Boolean expression evaluates as true
• Performs a different action when a Boolean expression evaluates as false
• A statement that executes when if is true or false and ends with a
semicolon
• Vertically align the keyword if with the keyword else
• Illegal to code else without if
• Depending on the evaluation of the Boolean expression following if, only
one resulting action takes place
Using Multiple Statements in if and if…else Clauses
• To execute more than one statement, use a pair of curly braces
• Place dependent statements within a block
• Crucial to place the curly braces correctly
• Any variable declared within a block is local to that block
Nesting if and if…else Statements
• Nested if statements
• Statements in which an if structure is contained inside another if structure
• Two conditions must be met before some action is taken
• Pay careful attention to the placement of else clauses
• else statements are always associated with if on a “first in-last out” basis
Using the switch Statement
switch statement
An alternative to a series of nested if statements
Test a single variable against a series of exact integer, character, or string
values

Keywords

switch
Starts the structure
Followed by a test expression enclosed in parentheses
case
Followed by one of the possible values for the test expression and a colon
break
Optionally terminates a switch statement at the end of each case
default
Optionally is used prior to any action that should occur if the test variable
does not match any case
• break statements in the switch structure
• If a break statement is omitted:
• The program finds a match for the test variable
• All statements within the switch statement execute from that
point forward

• case statement
• No need to write code for each case
• Evaluate char variables
• Ignore whether it is uppercase or lowercase

• Why use switch statements?


• They are convenient when several alternative courses of action
depend on a single integer, character, or string value
• Use only when there is a reasonable number of specific matching
values to be tested

You might also like