Tutorial 3 Loops Side Effects 1 CS 136 Spring 2020 Tutorial 3 - - PowerPoint PPT Presentation

tutorial 3
SMART_READER_LITE
LIVE PREVIEW

Tutorial 3 Loops Side Effects 1 CS 136 Spring 2020 Tutorial 3 - - PowerPoint PPT Presentation

Tutorial 3 Loops Side Effects 1 CS 136 Spring 2020 Tutorial 3 Loops: for loops & while loops Using a loop to solve a problem is called iteration . while is similar to if statements but while repeatedly loops back and


slide-1
SLIDE 1

Tutorial 3

  • Loops
  • Side Effects

CS 136 Spring 2020 Tutorial 3

1

slide-2
SLIDE 2

Loops: for loops & while loops

  • Using a loop to solve a problem is called iteration.
  • while is similar to if statements but while repeatedly

“loops back” and executes the statement until the expression is false.

  • General format of a while loop:

setup statement(s) while (expression) { body statement(s) update statement(s) }

  • for loops are a “condensed” version of a while loop.

CS 136 Spring 2020 Tutorial 3

2

slide-3
SLIDE 3

Global Variables

  • C is an imperative programming language
  • Code is divided into functions that serve specific purposes
  • Functions may require access to the same information/variables
  • Declare shared variables in the top level of the program (global

scope)

CS 136 Spring 2020 Tutorial 3

3

slide-4
SLIDE 4

Side Effects and Mutation

  • C is a compiled language
  • Compiler can optimize code for us in translation process
  • Pure functions: only rely on parameters and local variables
  • 1. Cache repeated calls to same function
  • 2. Remove calls to function with unused return value

CS 136 Spring 2020 Tutorial 3

4

slide-5
SLIDE 5

Side Effects and Mutation

  • Sometimes compiler cannot make these optimizations because

the function has side effects:

  • 1. Not using a pure function: function relies on global variables,

so output of function can change even when called with the same parameters (compiler cannot know what output would be without running the program)

  • 2. Functions that read input (result depends on user input)
  • 3. Functions that produce output (compiler might remove calls

to function with no effects except printing)

CS 136 Spring 2020 Tutorial 3

5

slide-6
SLIDE 6

Side Effects and Mutation

  • It is important to document your code and to indicate when a

function has side effects

  • This is so you (and other programmers) know when a function

might have less predictable impacts on the execution of a program

CS 136 Spring 2020 Tutorial 3

6

slide-7
SLIDE 7

Exercise: Managing a bank account Implement a

simple banking interface where we can:

  • deposit: adds money (positive) to your bank balance
  • withdraw: withdraws money (positive) from your bank account

if this will not make it more negative than the overdraft limit, and returns true on success

  • get_balance: gets the current bank balance
  • set_overdraft_limit: sets a non-negative overdraft limit

CS 136 Spring 2020 Tutorial 3

7

slide-8
SLIDE 8

Checkerboard - Exercise

Write the following C program: (use iteration)

// This program reads in two integers (height, width) // and prints a checkerboard with those dimensions

  • There are some I/O tests on Seashell

Example: 4 × 4 checkerboard

// IN 4 4 // OUT #.#. .#.# #.#. .#.#

CS 136 Spring 2020 Tutorial 3

8