Module 3 (Selection Structures)

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Switch statement expression

- the expression of a switch statement must result in an integer type (byte, short, int, long) or a char type. - it cannot be a Boolean value of a floating point value (float or double) - you cannot perform relational checks with a switch statement.

There are four main categories of control structures:

1. Sequence - Very boring. Simply do one instruction then the next and the next. Just do them in a given sequence or in order listed. Most lines of code are this. 2. Selection - This is where you select or choose between two or more flows. The choice is decided by asking some sort of question. The answer determines the path (or which lines of code) will be executed. (If then else can be used in this category) 3. Iteration - also known as repetition, it allows some code (one to many lines) to be executed (or repeated) several times. The code might not be executed at all (repeat it zero times), executed a fixed number of times or executed indefinitely until some condition has been met. Also known as looping because the flowcharting shows the flow looping back to repeat the task. Iteration can be accomplished with "test before loops, counting loops, and test after loops" (while can be used in this category) . 4. Branching - A control structure that allows the flow of execution to jump to a different part of the program. This category is rarely used in modular structured programming.

Control structures

The mechanisms that allow us to control the flow of execution within a program.

4. Logical Operators

• Booleans expressions can also use the following logical operators: Logical NOT Logical AND Logical OR -They all take Boolean operands and produce Boolean results -Logical NOT is a unwary operator (it operates on one operand) -Logical AND and OR are binary operators (each operates on two operands) •The Logical NOT operation is also called "logical negation" or "logical complement" • If some Boolean condition is "a" true, then "NOT a" is false; if "a" is false, then "NOT a" is true.

There are some commonly followed conventions to help make pseudocode written by one programmer easily understood by another programmer. Most of these conventions follow two concepts:

• Use indentation to show the action part of a control structure. • use an ending phrase word to end a control structure.

Motivation

Programs often need more than one path of execution, however. Many algorithms require a program to execute some statements only under certain circumstances. This can be accomplished with decision structures.

Selection Statements

-A selection (conditional) statement allows us to choose which statement (or block of statements) will be executed next, based on the what a Boolean expression evaluates to (true or false) Four selection structures: • if statement - causes a block of code to execute if a Boolean expression is true. • if-else statement - will execute one block of code if the Boolean expression is true, another if it is false. • if-else-if statement - tests a series of Boolean expressions and execute corresponding block when it finds one that is true. • switch statement - let's the value of a variable or expression determine where the program will branch to.

3. Relational Operators

A conditional expression uses a relational operator, which returns a Boolean result (t or f): > greater than < less than == equals ! not != not equal >= greater than or equal <= less than or equal ***Note the difference between the equality operator (==) and the assignment operator (=)

Iteration

A control structure that allows some lines of code to be executed many times

Branching

A control structure that allows the flow of execution to jump to a different part of the program.

Sequence

A control structure where you do the items in the sequenced list.

Structured programming

A method of planning programs that avoids the branching category of control structures.

default case

A switch statement can have an optical default case -The default case has no associated value and simply uses the reserved word default. - if the default case is present, control will transfer to the default case if no other case value matches - if there is no default case, and no other value matches, control falls through to the statement after the switch statement.

The "Do While" and "Repeat Until" iteration control structures:

Don't need an ending phrase. We simply use the first word, then the action part, followed by the second word with the test expression.

What's the ending phrase word for "Case"

Endcase

What's the ending phrase word "for"

Endfor

What's the ending phrase word for "If then Else"

Endif

What's the ending phrase word for "while"

Endwhile

Flowcharting

Method of documenting (charting) the flow (or paths) that a program would execute.

break statement

Often used as he last statement in each case's statement list. •A break statement caused control to transfer to the end of the switch statement. •If a break statement is not used, the flow of control will continue into the next case. • Sometimes this May be appropriate, but often we want to execute only the statements associated with once case.

The Boolean

Something that resolves true or false -You can get Boolean values several different ways •Simple •Complex -These Boolean will be used (later) to make decisions.

1. Flow of Control

The Oder or statement execution is called the flow of control -Unless specified otherwise, the order of statement execution through a method is linear (sequential): one statement after another in sequence. -Some programming statements allow us to •decide whether or not to execute a particular statement. •execute a statement over and over, repetitively -These selection (decision) statements are based on Boolean expressions (or conditions) that evaluate to true or false.


Ensembles d'études connexes

aaaaaa nsg 102 important, #2. Pulmonary Embolism, Anticoagulation (Lilley Ch.26) - Textbook, MODS/ARDS/DIC (Infection/SIRS&Sepsis/Severe Sepsis&Septic Shock/MODS) - IGGY Ch 32: Respiratory Problems, Day 8 of the New Year: All about the Heart, #2. Pul...

View Set

Glaciers 5:Glaciers, Deserts, and Wind

View Set

Differences Between Canine and Feline Thoracic Limb

View Set

WOH2040 - final exam study guide

View Set

Safety in Academic Chemistry Laboratories

View Set

EW 2 Unit 12 p 120 предлоги движения

View Set