tutorial 3
play

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


  1. Tutorial 3 • Loops • Side Effects 1 CS 136 Spring 2020 Tutorial 3

  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. 2 CS 136 Spring 2020 Tutorial 3

  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) 3 CS 136 Spring 2020 Tutorial 3

  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 4 CS 136 Spring 2020 Tutorial 3

  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) 5 CS 136 Spring 2020 Tutorial 3

  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 6 CS 136 Spring 2020 Tutorial 3

  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 7 CS 136 Spring 2020 Tutorial 3

  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 #.#. .#.# #.#. .#.# 8 CS 136 Spring 2020 Tutorial 3

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend