CSC 131: Introduction to Computer Science

Slides:



Advertisements
Similar presentations
CS107 Introduction to Computer Science Lecture 2.
Advertisements

1 Control Structures (and user input). 2 Flow of Control The order statements are executed is called flow of control By default, statements in a method.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Fundamentals of Python: From First Programs Through Data Structures
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Fundamentals of Python: First Programs
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
Pascal Programming Pascal Loops and Debugging. Pascal Programming Pascal Loops In our first brush with the while do loops, simple comparisons were used.
Decision Making and Branching (cont.)
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Control Structures I Chapter 3
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Chapter 4 C Program Control Part I
CSC 131: Introduction to Computer Science
Chapter 8 Text Files We have, up to now, been storing data only in the variables and data structures of programs. However, such data is not available.
CSC 131: Introduction to Computer Science
Control Structures II Chapter 3
Python: Control Structures
CSC 131: Introduction to Computer Science
Debugging and Random Numbers
Loop Structures.
CSC 131: Introduction to Computer Science
Primitive Data, Variables, Loops (Maybe)
Topics The if Statement The if-else Statement Comparing Strings
Topics Introduction to Repetition Structures
Lecture 07 More Repetition Richard Gesick.
LOOPS.
Lecture 4B More Repetition Richard Gesick
Topics The if Statement The if-else Statement Comparing Strings
And now for something completely different . . .
Outline Altering flow of control Boolean expressions
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
A First Book of ANSI C Fourth Edition
Introduction to Object-Oriented Programming with Java--Wu
Iteration: Beyond the Basic PERFORM
Loops CIS 40 – Introduction to Programming in Python
Coding Concepts (Basics)
Chapter 6 Control Statements: Part 2
3. Decision Structures Rocky K. C. Chang 19 September 2018
Chapter 6: Repetition Statements
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Computing Fundamentals
CSC115 Introduction to Computer Programming
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
CISC101 Reminders All assignments are now posted.
Chapter8: Statement-Level Control Structures April 9, 2019
Chapter 3: Selection Structures: Making Decisions
Another Example Problem
Copyright © Cengage Learning. All rights reserved.
Using C++ Arithmetic Operators and Control Structures
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Controlling Program Flow
CprE 185: Intro to Problem Solving (using C)
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

CSC 131: Introduction to Computer Science Dr. Curry Guinn

Quick Info Dr. Curry Guinn CIS 2025 guinnc@uncw.edu www.uncw.edu/people/guinnc 962-7937 Office Hours: MTR: 10:00am-11:00m and by appointment Teaching Assistant: Zach Krepps Tuesday, 8:00am-9:30am, 12:45pm-3:45pm Thursday, 8:00am-9:30am Office: GTA office – off of CIS 2004 lab

For Next Class, Thursday Homework 3 is due Thursday, 11:59pm, September 7

Today A brief aside as to when computers will have more “brain power” than human beings Chapter 3 Selection Boolean Expressions Iteration – The while loop

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons

When will computers surpass humans in computational ability? Notice this isn’t quite asking when computers will be “smarter” or more “intelligent”

How the Brain Works Neuron Cell body: soma, contains nucleus Single long “output” fiber: axon The axon does eventually branch into axon tips Dendrites are small fibers Connecting junction: synapses, connect dendrites

Neuron Activation Chemical reactions change the electrical potential of the cell body When the electrical potential reaches a certain threshold, a pulse is sent down the axon: action potential Synapses which increase the potential: Excitatory Synapses which decrease the potential: Inhibitory

Learning in the Brain Synaptic connections can become stronger Neurons can form new connections with other neurons Neuron groups can actually shift within the brain (although to what effect?)

Can Digital Computers Ever Be as Smart as Humans? There is a significant gap between the computational power of the human brain and current desktop computers. A rough estimate tells us that the human brain has 100 million times the computational power of a 2 Ghz Intel computer (How this estimate might be computed is explained later.). The fastest super computers, Tianhe-2, for example, has been clocked at 33 petaflop/s Petaflop = 1 quadrillion = 1015 How does that compare?

How much can the brain computer per second? The human brain consists of approximately 100 billion (1011) neurons Each neuron may be connected to many other neurons. Let’s suppose the average is 10,000 (104). 1,000 trillion connections (1015) = 1 quadrillion It takes a neuron 1/200 second to re-fire So, at most, the brain could compute 200 * 10,000 * 100 billion (or 200 million billion) computations per second 200,000 teraflops or 200 quadrillion computations or 200 petaflops Theoretical maximum

Your PC 2 Ghz = 2 billion calculations per second 200,000 teraflops/2 billion = 100,000,000 Core i7-980 @ 4.5Ghz ~ 95 gigaflops 200,000 trillion/95 billion = 2.1 million

Tianhe-2 computer 16,000 nodes with a total of 3,120,000 cores. Space: 720 square meters Weight: > 150 tons When will computers be faster?

So every 2 years … a supercomputer … http://www.bbc.com/news/technology-33718311

Your PC

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.1 What is a Control Structure?

Boolean Expressions The Boolean data type contains two Boolean values, denoted as True and False in Python. A Boolean expression is an expression that evaluates to a Boolean value. Boolean expressions are used to denote the conditions for selection and iterative control statements. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.2 Boolean Expressions

Relational Operators in Python Note that these operators not only apply to numeric values, but to any set of values that has an ordering, such as strings. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.2 Boolean Expressions

Let’s Try It What do each of the following relational expressions evaluate to? Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.2 Boolean Expressions

Membership Operators Python provides a convenient pair of membership operators. These operators can be used to easily determine if a particular value occurs within a specified list of values. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.2 Boolean Expressions

>>> 'Dr.' in 'Dr. Madison' True The membership operators can also be used to check if a given string occurs within another string, >>> 'Dr.' in 'Dr. Madison' True As with the relational operators, the membership operators can be used to construct Boolean expressions. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.2 Boolean Expressions

Boolean Operators George Boole, in the mid-1800s, developed what we now call Boolean algebra. His goal was to develop an algebra based on true/false rather than numerical values. Boolean algebra contains a set of Boolean (logical) operators, denoted by and, or, and not. These logical operators can be used to construct arbitrarily complex Boolean expressions. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.2 Boolean Expressions

Logical and is true only when both its operands are true — otherwise, it is false. Logical or is true when either or both of its operands are true, and thus false only when both operands are false. Logical not simply reverses truth values — not False equals True, and not True equals False. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.2 Boolean Expressions

Let’s Try It What do each of the following relational expressions evaluate to? Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.2 Boolean Expressions

Operator Precedence and Boolean Expressions We saw the notion of operator precedence related to the use of arithmetic expressions. Operator precedence applies to Boolean operators as well. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.2 Boolean Expressions

Operator Precedence and Boolean Expressions As we saw earlier, in the table, high-priority operators are placed before lower-priority operators. Thus we see that all arithmetic operators are performed before any relational or Boolean operators. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.2 Boolean Expressions

If not all subexpressions, As with arithmetic expressions, it is good programming practice for use parentheses, even if not needed, to add clarity and enhance readability, If not all subexpressions, Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.2 Boolean Expressions

Fundamental logically equivalent Boolean expressions. The last two equivalences are referred to as De Morgan’s Laws. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.2 Boolean Expressions

Let’s Try It From the Python shell, enter the following and observe the results. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.2 Boolean Expressions

Selection Control A selection control statement is a control statement providing selective execution of instructions. A selection control structure is a given set of instructions and the selection control statement(s) controlling their execution. We look at the if statement providing selection control in Python next. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.3 Selection Control

If Statement An if statement is a selection control statement based on the value of a given Boolean expression. Note that if statements may omit the “else” part. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.3 Selection Control

Indentation in Python One fairly unique aspect of Python is that the amount of indentation of each program line is significant. In most programming languages, indentation has no affect on program logic—it is simply used to align program lines to aid readability. In Python, however, indentation is used to associate and group statements. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.3 Selection Control

A header in Python is a specific keyword followed by a colon A header in Python is a specific keyword followed by a colon. In the example, the if-else statement contains two headers, “if which 5 = 'F':” containing keyword if, and “else:” consisting only of the keyword else. Headers that are part of the same compound statement must be indented the same amount—otherwise, a syntax error will result. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.3 Selection Control

The set of statements following a header in Python is called a suite (commonly called a block ). The statements of a given suite must all be indented the same amount. A header and its associated suite are together referred to as a clause. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.3 Selection Control

Let’s Try It From IDLE, create and run a Python program containing the code on the left and observe the results. Modify and run the code to match the version on the right and again observe the results. Make sure to indent the code exactly as shown. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.3 Selection Control

Multi-Way Selection Python provides two means of constructing multi-way selection — one involving multiple nested if statements, and the other involving a single if statement and the use of elif headers. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.3 Selection Control

Multi-Way Selection by Use of Nested If Statements Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.3 Selection Control

Let’s Try It From IDLE, create and run a simple program containing the code below and observe the results. Make sure to indent the code exactly as shown. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.3 Selection Control

Multi-Way Selection by Use of elif Header Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.3 Selection Control

Iterative Control An iterative control statement is a control statement providing the repeated execution of a set of instructions. An iterative control structure is a set of instructions and the iterative control statement(s) controlling their execution. Because of their repeated execution, iterative control structures are commonly referred to as “loops.” We look at one specific iterative control statement next, the while statement. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.4 Iterative Control

While Statement A while statement is an iterative control statement that repeatedly executes a set of statements based on a provided Boolean expression (condition). All iterative control needed in a program can be achieved by use of the while statement. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.4 Iterative Control

As long as the condition of a while statement is true, the statements within the loop are (re)executed. Once the condition becomes false, the iteration terminates and control continues with the first statement after the while loop. Note that it is possible that the first time a loop is reached, the condition may be false, and therefore the loop would never be executed. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.4 Iterative Control

Execution steps for adding the first three integers by use of the previous while loop. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.4 Iterative Control

Input Error Checking The while statement is well suited for input error checking in a program. This is demonstrated in the revised version of the temperature conversion program. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.4 Iterative Control

The while loop is used to force the user to re-enter if neither an ‘F’ (for conversion to Fahrenheit) or a ‘C’ (for conversion to Celsius) is not entered. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.4 Iterative Control

Let’s Try It In IDLE, create and run a simple program containing the code below and observe the results. Make sure to indent the code exactly as shown. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.4 Iterative Control

Infinite Loops An infinite loop is an iterative control structure that never terminates (or eventually terminates with a system error). Infinite loops are generally the result of programming errors. For example, if the condition of a while loop can never be false, an infinite loop will result when executed. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.4 Iterative Control

Following is an example program containing an infinite loop. The while loop is an infinite loop (for any value of n 1 or greater) because the value of current is not incremented Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.4 Iterative Control

Let’s Try It From IDLE, create and run a simple program containing the code below and observe the results. Indent the code exactly as shown. To terminate an executing loop, hit ctrl-C. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.4 Iterative Control

Definite vs. Indefinite Loops A definite loop is a program loop in which the number of times the loop will iterate can be determined before the loop is executed. Following is an example of a definite loop. Although it is not known what the value of n will be until the input statement is executed, its value is known by the time the while loop is reached. Thus, it will execute “n times.” Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.4 Iterative Control

An indefinite loop is a program loop in which the number of times that the loop will iterate cannot be determined before the loop is executed. In this case, the number of times that the loop will be executed depends on how many times the user mistypes the input. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.4 Iterative Control

Boolean Flags and Indefinite Loops Often the condition of a given while loop is denoted by a single Boolean variable, called a Boolean flag. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.4 Iterative Control

Boolean variable valid_entries on line 12 in the following program is an example of the use of a Boolean flag for controlling a while loop. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.4 Iterative Control

Coin Change Exercise Program Let’s Apply It Coin Change Exercise Program The following program implements an exercise for children learning to count change. It displays a random value between 1 and 99 cents, and asks the user to enter a set of coins that sums exactly to the amount shown. The program utilizes the following programming features: ➤ while loop ➤ if statement ➤ Boolean flag ➤ random number generator Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.4 Iterative Control

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 3.4 Iterative Control

For Next Class, Thursday Homework 3 is due Thursday, 11:59pm, September 7