CSE 206 Course Review.

Slides:



Advertisements
Similar presentations
Java Control Statements
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Introduction to C Programming
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
Loops – While, Do, For Repetition Statements Introduction to Arrays
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
Introduction to Programming G51PRG University of Nottingham Revision 2 Essam Eliwa.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
1 Joe Meehean.  call themselves directly  or indirectly void f(){... f();... } void g(){... h();... } void h(){... g();... }
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
1 CS161 Introduction to Computer Science Topic #8.
Lecture 15: Course Review BJ Furman ME 30 16MAY2011.
COMP Loop Statements Yi Hong May 21, 2015.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Identify the Appropriate Method for Handling Repetition
>> Fundamental Concepts in PHP
Repetitive Structures
Chapter 4 – C Program Control
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
UMBC CMSC 104 – Section 01, Fall 2016
Topic 6 Recursion.
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
5.13 Recursion Recursive functions Functions that call themselves
Recursion A problem solving technique where an algorithm is defined in terms of itself A recursive method is a method that calls itself A recursive algorithm.
Computer Science 4 Mr. Gerb
The switch Statement, and Introduction to Looping
Week 7 Part 2 Kyle Dewey.
Expressions and Control Flow in JavaScript
Introduction to C++ Recursion
11/10/2018.
CSC 253 Lecture 8.
Arrays, For loop While loop Do while loop
Iteration with While You can say that again.
CSC 253 Lecture 8.
Outline Altering flow of control Boolean expressions
Chapter 4 - Program Control
3 Control Statements:.
Unit 3 Test: Friday.
Your questions from last session
Computer Science Core Concepts
C Programming Getting started Variables Basic C operators Conditionals
2.6 The if/else Selection Structure
Data Structures and Algorithms Introduction to Pointers
Fundamental Programming
1-6 Midterm Review.
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
Chapter 4 - Program Control
Introduction to Programming
The structure of programming
Chap 7. Advanced Control Statements in Java
Thinking procedurally
CSCE 206 Lab Structured Programming in C
REPETITION Why Repetition?
Lesson 3. Controlling program flow. Loops. Methods. Arrays.
SPL – PS2 C++ Memory Handling.
Presentation transcript:

CSE 206 Course Review

Conditions Switch If/else-if/else When: all conditions depend on a single value Example: switch (var1) { case 1: …; break; default: …; } If/else-if/else When: Any other case Example: if (var1 == 1 || var1 == 2) {...} else if (var > 0) {...} else {...} Evaluated in order, if first if is not true, then it checks the second “else if” If more than one condition: check first condition, if true, checks the second

Conditions if (a == 1) { if (b == 2) { // … if (a == 1 && b == 2) // ALWAYS true } if (a == 1 && b == 2) { // … }

Binary and logical operators Can be used in any condition (if, loops, …) Binary &: and bit by bit |: or bit by bit =: assignment, if (a = 1) is ALWAYS true and WILL make a=1, whatever the previous value ... Logical &&: and ||: or ==: equal !=: different … if (a == b && b != 0)

Loops For While Do-while When: you know the number of iterations Example: for (i = 0; i < n; i++) {...} // initialization; condition; increment While When: you DON’T know the number of iterations Example: while(n > 0) {...} // n must change its value inside the loop! Do-while When: you DON’T know the number of iterations but want to execute at least ONCE Example: do { … } while(n > 0);

Functions Function name Declare before (outside) main(): int function(int parameter1, int parameter2); Define (outside main): int function(int p1, int p2) { return p1+p2; } Call (inside main or other functions): int c = function(a, b); Why: encapsulate parts of the code that solves one problem for readability and to avoid code duplication Good example in HW5_Q1, “isdigit(), “isvowel()”, … (My rule of thumb: any function, including main(), should not be more than ~20 lines of code) return type Returns an int

Fibonacci: loops vs recursion Each function call reserves some space in the stack: for each parameter, where to return after the function, function variables. The stack keeps filling until the recursion finishes! Too many function calls could cause a stack overflow. fib(5) fib(4) fib(3) fib(3) fib(2) fib(2) fib(1) fib(2) fib(1) fib(1) fib(1) fib(1)

Fibonacci Recursive is more readable and intuitive, but it is not the best algorithm to use in this case: 1- It can cause a stack overflow 2- It calculates several times the same result (5 times fib(1), 3 times fib(2), …) If you are interested: One optimization for the recursion version could be: You know the number of Fibonacci numbers you want to print, so you can create an array with those number of positions and initialize all to -1. Instead of using the base case as n < 2 you could replace it with: if that array position index is different than -1, return the value that it contains. That way you would only call each function number just ONCE! Less recursive calls and still more readable. This technique is called Memoization.

Increment and Decrement Operators What is the value when i=5 ++i vs i++ in: a = ++i; a = i++; What is the difference between: for (i = 0; i < n; i++) for (i = 0; i < n; ++i) What position is accessed in each case: array[i++] array [++i]

Conditions if (expression1) if (expression2) Statement; else What could be wrong here?

What is the output int main() { int i = 0; for(i = 0; i < 3; i++); { printf("we are in the loop "); continue; } printf("i=%d\n", i); return 0; }

Where is the bug? int arr[20]; for (i = 0; i <= 20; i++){ arr[i] = 0; } int count; while (count < 50){ count++; } char z = ‘p’; while (z = ‘p’){ z = ‘b’; } char* s1 = “string”; char* s2 = “string”; if (s1==s2){ printf(“We’re equal!\n”); }

Pointers in arrays: consecutive in memory int *end int *start int *current current++ while (current < end) {...}

Pointers in LinkedList a = 1; next a = 4; next a = 21; next a = 8; next NULL Node *head Second in the list (a=4): head->next->a = 3;

Accessing structs Student s; Student *ptr = &s; ptr->grades.test1 = 84.1; // through a pointer s.grades.test2 = 96.2; // through the student (*ptr).uin = 123456789; // beware of the parenthesis!