Docsity

Flow Control Structures and Pseudocode: If Statements, Flowcharting, and Select Case, Slides of C programming

Partial preview of the text

Download Flow Control Structures and Pseudocode: If Statements, Flowcharting, and Select Case and more Slides C programming in PDF only on Docsity!

If Statements

04_decisions.ppt

Overview of Topic

  • Pseudocode
  • Flow Control Structures
  • Flowcharting
  • If, If-Else
  • Nested If’s
  • Select Case – Switch statement

Flow Control Structures

  • The order in which statements are

executed.

  • There are four structures.
    1. Sequence Control Structure
    2. Selection Control Structure
      • Also referred to as branching (if and if-else)
    3. Case Control Structure (switch)
    4. Repetition Control Structure (loops)

Flowcharting

  • A flowchart is a pictorial representation of an algorithm or logical steps.
  • Each step is represented by a symbol and the arrows indicate the flow and order of the steps.
  • The shape of the symbol indicates the type of operation that is to occur.
  • Flowcharts may help the move visual students learn and understand logic.

1. Sequence Control Structure

  • The order statements are placed (sequenced) //input intQty = int.Parse(txtQuantity.Text); decPrice = decimal.Parse(txtPrice.Text); //process decSubtotal = intQty * decPrice;

//output txtSubtotal.Text = decSubtotal.ToString(“N”);

  • The only way to display subtotal, statements must be in this order.

Flowchart – Sequence Control

Begin

Input price, qty

subtotal = price * qty

Output subtotal

End

Flowchart – If Statement

Subtotal = qty * price

If taxable

Sales tax = subtotal * Tax Rate

True

False

Output sales tax

Selection Control (If-Else)

  • Use if else statements when there are several options to choose from.

if (radNextDay.Checked == true) decShipping = cdecNEXT_DAY_SHIPPING_RATE;

else decShipping = cdecTHREE_DAY_SHIPPING_RATE;

  • After if else statement, decShipping will have a value.

Condition is an Expression

  • Conditions evaluate to true or false.

if (condition)

true – 1 or more statements

else

false – 1 or more statements

Block of code – Compound Statements

  • Use braces to create a block of statements.
  • Single statements do NOT require braces.

if (intHours > 40) { decRegularPay = 40 * decPayRate; decOvertimePay = (intHours – 40) * (decPayRate * cdec_OVERTIME_RATE); } else { decRegularPay = intHours * decPayRate; decOvertimePay = 0; } decGrossPay = decRegularPay + decOvertimePay;

Nested If Statements

  • First if has to be true in order to continue.

if (intQuantity >= 1) if (decPrice >= 5.00M) decSubtotal = intQty * decPrice; else { MessageBox.Show(“Invalid price entered”); txtPrice.Focus(); txtPrice.SelectAll(); } else { MessageBox.Show(“Invalid quantity entered”); txtQuantity.Focus(); txtQuantity.SelectAll(); }

Matching an Else to If

  • How are else statements matched with an if?
  • Compiler works it’s way back. When an else is encounter, it looks back to find an if that has not been matched to an else.
  • Why do we indent each level?
  • We indent to make programs easier to read. Indenting has no effect on how compiler matches an else to an if.

Relational Operators

= = ! = <

< =

=

Equal to Not Equal to Less than Greater than Less than or equal to Greater than or equal to

Relational Operators are used to form conditions, and conditions can involve constants, variables, numeric operators, and functions.

Assignment (=) vs Comparison (==)

  • if (x = 12) // may generate syntax error.
  • if (x == 12) // comparison
  • The error message generated will have to do

with an implicit data conversion to bool.

  • If you get this error, check that you used 2

equal signs.