Iteration & Branching CSC 171 FALL 2001 LECTURE 5.

Slides:



Advertisements
Similar presentations
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Advertisements

Decisions If statements in C.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
ITEC113 Algorithms and Programming Techniques
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Program Design and Development
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
BOOLEAN LOGIC CSC 171 FALL 2004 LECTURE 7. ASSIGNMENT Review Quiz # 2 Start reading Chapter 5.
1 Boolean Algebra & Logic Design. 2 Developed by George Boole in the 1850s Mathematical theory of logic. Shannon was the first to use Boolean Algebra.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
1 CSE 20: Lecture 7 Boolean Algebra CK Cheng 4/21/2011.
Circuit Simplification: Boolean Algebra
Programming for GCSE Topic 3.3: Boolean Logic and Truth Tables
22C:19 Discrete Math Boolean Algebra & Digital Logic Fall 2010 Sukumar Ghosh.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
True or False Unit 3 Lesson 7 Building Blocks of Decision Making With Additions & Modifications by Mr. Dave Clausen True or False.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Computer Science 101 The Boolean System. George Boole British mathematician ( ) Boolean algebra –Logic –Set theory –Circuits –Conditions in if.
XOR and XNOR Logic Gates. XOR Function Output Y is TRUE if input A OR input B are TRUE Exclusively, else it is FALSE. Logic Symbol  Description  Truth.
Chapter 3.5 Logic Circuits. How does Boolean algebra relate to computer circuits? Data is stored and manipulated in a computer as a binary number. Individual.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
True or False: Boolean Expression Boolean expression are expressions which evaluate to "true" or "false“ Primary operators to combine expressions: && (and),
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
ITEC113 Algorithms and Programming Techniques
Control Structures CPS120: Introduction to Computer Science Lecture 5.
Chapter 5: Sequences, Mathematical Induction, and Recursion 5.5 Application: Correctness of Algorithms 1 [P]rogramming reliability – must be an activity.
1 Chapter 4: Basic Control Flow ► Chapter Goals  To be able to implement decisions using if statements  To understand statement blocks  To learn how.
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
CS 240 – Computer Programming I Lab Kalpa Gunaratna –
Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
13 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Lecture 3 Combinational Circuits
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Control statements Mostafa Abdallah
CPS120: Introduction to Computer Science Decision Making in Programs.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
Intro to Loops 1.General Knowledge 2.Two Types of Loops 3.The WHILE loop 1.
Decision Making CMSC 201 Chang (rev ).
Problem-solving with Computers. 2Outline  Computer System  5 Steps for producing a computer program  Structured program and programming  3 types of.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
Dept. of Electrical and Computer Eng., NCTU 1 Lab 5. 3-to-8 Decoder Presenter: Chun-Hsien Ko Contributors: Chung-Ting Jiang and Lin-Kai Chiu.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Algorithms and Pseudocode CS Principles Lesson Developed for CS4 Alabama Project Jim Morse.
Pseudocode (pronounced SOO-doh-kohd)  is a detailed yet readable description of what a computer program or algorithm must do, expressed in a formally-styled.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
7 - Programming 7J, K, L, M, N, O – Handling Data.
Algorithm & Flowchart.
CSC111 Quick Revision.
Lecture 07 More Repetition Richard Gesick.
Computer Science 101 While Statement.
Lecture 4B More Repetition Richard Gesick
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
JavaScript conditional
Outline Altering flow of control Boolean expressions
Introduction to pseudocode
Lecture 20: Combinatorial Circuits I
Computer Science Core Concepts
Lecture 5 Binary Operation Boolean Logic. Binary Operations Addition Subtraction Multiplication Division.
Chapter 4: Repetition Structures: Looping
ECE Digital Electronics
Conditionals.
Presentation transcript:

Iteration & Branching CSC 171 FALL 2001 LECTURE 5

History Boolean Logic George Boole describes his system for symbolic and logical reasoning: An Investigation into the Laws of thought Boolean logic later becomes the basis for computer design

Quote “No matter how correct a mathematical theorem may appear to be, one ought never to be satisfied that there was not something imperfect about it until it also gives the impression of being beautiful.” George Boole - Quoted in D MacHale, Comic Sections (Dublin 1993)

Boolean Logic Boolean expressions boolean expressions can take on one of two values (true or false) 3 <= 5 – true 3 > 5 – false

Boolean Variables int x = 5, y = 3; boolean test1, test2, test3, test4; test1 = x < y ; test2 = x >= y; test3 = test1 && test2; test4 = test1 || test 2;

“AND” OPERATION ABA && B False TrueFalse TrueFalse True

“OR” OPERATION ABA || B False True FalseTrue

“NOT” OPERATION A!A FALSETRUE FALSE

The mathematical basis for computer design An open switch can represent – Zero – False A closed switch can represent – One – True A switch can control another switch – A logic gate

Boolean Operations & Circuits And Or Not X

Branching Logic if (earnings < ) { tax = income * 0.35; } else { tax = income * 0.45; } netpay = earnings – tax;

While repetition int count = 0; while (count < 10) { sum += count; count++; }

Design Technique State the problem Write a pseudocode algorithm Code around the comments

Example Decimal to binary Start with an integer value “x” and an empty string While the value of x is greater than zero – If x is odd, prepend a “1” to the string – If x is even, prepend a “0” to the string – Divide the value of x by 2

Example: Decimal to Binary int x = 10; // Start with an integer value “x” String binstring = “”; // and an empty string while (x > 0) { // While x is greater than zero if ((x % 2) == 0) { // If x is odd, binstring = “ 1” + binstring; // prepend a “1” } else { // If x is even binstring = “ 0” + binstring; // prepend a “0” } x /= 2; // Divide the value of x by 2 }

Trace at the top of the loop x==10, binstring ==“”, (x%2) == 0 x==5, binstring ==“0”, (x%2)==1 x==2, binstring ==“10”, (x%2)==0 x==1, binstring ==“010”, (x%2)==1 x==0, binstring ==“1010”, (x%2)==0

In class example The user wants to know how much money he will have saved year by year for retirement. Questions to ask – How old are you, now? – How much can you save, each year? – How old will you be when you retire? – How much do you need, during retirement? – How old will you be when you die? – What interest rate should we invest at?

In class excercise You design the program (pair up) – Use dialog boxes for input, console for output – Identify the key variables needed – Write a pseudocode program You have 10 minuites to do this