Docsity

Understanding Loops, Lists, Printing & Controls in VBA, Slides of C programming

Partial preview of the text

Download Understanding Loops, Lists, Printing & Controls in VBA and more Slides C programming in PDF only on Docsity!

Loops, Lists, Printing

10_loops.ppt

Overview of Topics

  • While Loop
  • Do While Loop
  • For Loop
  • Pretest vs Posttest
  • Nested Loops
  • List and Combo Boxes
  • Printing Controls

Flowchart Symbols

Begin or End Input or Output

Processing Decision

Branch or Direction of Flow

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 (select)
    4. Repetition Control Structure (loops)

While is a Pretest Loop - Example

intCount = 1; //initialize controlling variable

while (intCount < 4)

{ txtCount.Text = intCount.ToString(โ€œN0โ€); intCount++; //add one

}

Output:

1 2 3

While is a Pretest Loop

  • Pretest - controlling condition is evaluated before executing loop body.
  • Controlling variable must be initialized.
  • Condition must be true to enter loop body.
  • There is NO semi-colon after the condition.
  • It is possible that body is not executed at all.
  • Condition must be false to get out of loop.
  • Controlling variable should be modified within the loop.
  • Execution continues with next statement after Loop.

Do-While is a Posttest Loop -Example

intCount = 1; //initialize controlling variable

do

{ txtCount.Text = intCount.ToString(โ€œN0โ€); intCount++;

} w hile (intCount < 4 );

Output:

1 2 3

Do-While Posttest Loop

  • Posttest - controlling condition is evaluated after executing loop body.
  • So, body is always executed at least one time.
  • Initialization of controlling variable not necessarily required.
  • Condition must be false to get out of loop.
  • There is a semi-colon after the condition.
  • Controlling variable should be modified within the loop.
  • Execution continues with next statement after Loop.

Pretest vs. Posttest

  • Pretest
    • For pretest loops the terminating condition is at the top of the loop.
    • It is possible that the body is not executed if the condition to get into the loop is not met.
  • Posttest
    • For posttest loops the terminating condition is at the bottom of the loop.
    • The body is always executed at least one time.

Loop Summary

  • Infinite loop
    • A loop that never ends.
    • While โ€“ condition always evaluates to true.
    • Controlling variable must be altered within the loop.
    • Click on the close form icon to stop the program.
    • Use Control-Break to enter debug mode.
  • When to use a While or Do-While will become evident as we continue to use and learn each loop.
  • Nested Loop is a loop inside another loop (see next slide).

For Loop

  • Good when a task needs to be completed a

fixed number of times.

  • Good when counting with fixed increments.
  • Compares to a While Pretest loop.

for (initialization; condition; action)

body

For Loop

//For includes initialization and ending condition

for ( int intCount = 1; intCount < 4; intCount++;) { txtCount.Text = intCount.ToString(โ€œN0โ€); }

Output:

1 2 3

Flowchart โ€“ For Pretest Loop

count = 1

while count < 4

Output count

count +

Next statement

False

True

Represents Skip or Exit Loop Loop

Nested For-Next Loops

  • For loops may contain other For loops.
  • The second loop must be completely

contained inside the first loop.

  • The second loop should have a different

controlling variable.

  • Should indent inner loops for readability.