1. Technology
Send to a Friend via Email

Structured Programming

3. Branching

clr gif
Join the Discussion

Questions? Comments?

More of this Feature

Introduction Sequence Loops

There are two types of branching statements. The if statement is used for conditional execution of a single statement or to select which of two statements is to be executed. The case statement (sometimes referred to as the select statement) allows for selection of one statement out of three or more that should be executed.

If

if statement

Statements can be executed conditionally by using an if statement. The if statement specifies a condition which gets tested when the if statement is executed. If the condition is true then the following statement is executed otherwise processing skips that statement.

Optionally a second statement can be attached to the if statement that will be executed if the condition is false.

pseudo code

if condition
true-statement
else
false-statement

example

if x > y
print "x is bigger than y"
else
print "x is not bigger than y"

Case

case statement

A case or select statement allows for one of a number of statements to be executed depending on the value of a field. There is usually also an additional statement which is executed if none of the specified values is matched.

pseudo code

case fieldname
value1: statement-1
value2: statement-2
value3: statement-3
otherwise: other-statement

example

case size
1: print "small"
2: print "medium"
3: print "large"
otherwise: print "unknown"

The final structure type we can use with structured programming is looping.

1 | 2 | 3 | 4

Note that this article is copied from the "Ask Felgall" website with the permission of the copyright owner.

©2014 About.com. All rights reserved.