Control Statements CSCI 125 & 161 / ENGR 144 Statements that - - PDF document

control statements csci 125 161 engr 144
SMART_READER_LITE
LIVE PREVIEW

Control Statements CSCI 125 & 161 / ENGR 144 Statements that - - PDF document

Control Statements CSCI 125 & 161 / ENGR 144 Statements that affect the sequence of Lecture 7 execution of other statements Normal is sequential May want to repeat statements - iteration Martin van Bommel May or may not


slide-1
SLIDE 1

1

CSCI 125 & 161 / ENGR 144 Lecture 7

Martin van Bommel

Control Statements

  • Statements that affect the sequence of

execution of other statements

  • Normal is sequential
  • May want to repeat statements - iteration
  • May or may not want particular statement

executed at all - conditional

Control Statement Structure

  • Control line - first line (e.g. while)
  • Body - statements enclosed in { } that are

repeated

  • Body typically indented to show range of

control statement

Iteration and Loops

  • Iteration - process of repeating an operation
  • Essential for large amounts of data -

repeating statements shortens programs

  • Loop - any portion of program repeated via

control statement

  • e.g. while loop

The while statement

while (condition) { statements } where condition tested before first and every iteration statements executed each time condition true

while operation

  • Condition test performed before every cycle,

including first.

  • If condition is initially false,

body of loop not executed at all.

  • Condition test performed only at beginning of

each loop cycle.

  • Even if condition becomes false during cycle,

cycle is completed.

slide-2
SLIDE 2

2

while example

  • Sum up digits of a number

sum = 0; while (n > 0) { sum = sum + n % 10; n = n / 10; }

Infinite loops

  • A loop cycle that never stops – infinite loop
  • Condition of while statement never false
  • e.g.

while (n >= 0) { sum += n % 10; n /= 10; }

Repeat-N-Times Idiom

  • for (i = 0; i < N; i++)

{ statements to be repeated }

  • May want to add up 10 input numbers

for (i = 0; i < 10; i++) { cin >> value; sum = sum + value; }

Index Variable in Counting Loop

for (i = 0; i < N; i++)

  • Variable i is called the index variable
  • Any variable may be used - traditionally i
  • Must be declared at beginning of function
  • Initial value of i in loop is 0 - first cycle
  • Next cycles value is 1, 2, etc.
  • On last cycle, value is N – 1
  • After loop, value of i is N

Non-Zero Counting Loop

  • It is possible to modify for loop to begin at

another number for (i = first; i <= last; i++)

  • Note the <= operator instead of <
  • Value of i ranges from first to last
  • Used if value of i required in body of loop

for statement

for (init; test; step) { statements } where init is an expression evaluated at beginning test is a condition for continuing the loop step is an expression to prepare next cycle

slide-3
SLIDE 3

3

for example

  • Countdown from 10 to 0

for (t=10; t >= 0; t--) { cout << t; } cout << ”Liftoff!\n”;

for more

  • Note that expressions init, test, and step are
  • ptional, but the semicolons must appear

for (;;)

  • infinite loop

for (;t<10;) same as while (t<10)

Nested for loops

  • Create a 10 x 10 multiplication table

for (row=1; row<=10; row++) { for (col=1; col<=10; col++) { cout << (row * col); } cout << endl; }

Conditional Execution

  • May want to execute a statement only if

some condition applies

  • May wish to choose between two

alternative courses of actions depending on some test

  • Simplest form - if statement

if Statement

if (conditional-test) { …statements executed if test true... } } else { …statements executed if test false... }

Relational & Logical Operators

== Equal != Not equal > Greater < Less >= Greater or equal <= Less or equal

  • Logical operate on results of comparisons

! Not (true if operand false – one operand) && And (true if both true) || Or (true if either true)