CSC115 Introduction to Computer Programming

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Boolean Expressions Conditional Statements & Expressions CSC 1401: Introduction to Programming with Java Lecture 4 – Part 2 Wanda M. Kunkle.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
The If/Else Statement, Boolean Flags, and Menus Page 180
Chapter 4 Making Decisions
C++ for Engineers and Scientists Third Edition
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Conditional Statements For computer to make decisions, must be able to test CONDITIONS IF it is raining THEN I will not go outside IF Count is not zero.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks.
Flow of Control Part 1: Selection
Decision Making - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 10/27/20151.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CSC115 Introduction to Computer Programming Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA 19383
Chapter Making Decisions 4. Relational Operators 4.1.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
CSC115: Matlab Special Session Dr. Zhen Jiang Computer Science Department West Chester University.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I.
TUTORIAL 4 Visual Basic 6.0 Mr. Crone. Pseudocode Pseudocode is written language that is part-code part- English and helps a programmer to plan the layout.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Input, Output and Variables GCSE Computer Science – Python.
Computer Science 1000 LOGO II. Boolean Expressions like Excel and Scratch, LOGO supports three Boolean operators less than (
Decision making If.. else statement.
Chapter 3 Selection Statements
Chapter 4: Making Decisions.
Decisions Chapter 4.
Chapter 4: Making Decisions.
Python: Control Structures
Lecture 3- Decision Structures
Introduction to Programming
The Selection Structure
Factoring if/else code
CSC115 Introduction to Computer Programming
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
Selection CIS 40 – Introduction to Programming in Python
Introduction to Programming
And now for something completely different . . .
CSC115 Introduction to Computer Programming
Executes a block of statements only if a test is true
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
CSC530 Data Structure - Decision
Introduction to Programming
3. Decision Structures Rocky K. C. Chang 19 September 2018
Selection Statements.
CSC115 Introduction to Computer Programming
SELECTIONS STATEMENTS
Chapter 2 Programming Basics.
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
CS2011 Introduction to Programming I Selections (I)
Chapter 5 Decisions.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Chapter 3: Selection Structures: Making Decisions
The Selection Structure
Introduction to Programming
Selection Control Structure
Lecture 9: Implementing Complex Logic
Presentation transcript:

CSC115 Introduction to Computer Programming Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA 19383 zjiang@wcupa.edu

Table of Contents Review of Basic I/O Condition and Decision Making Format/Syntax and Execution Boolean Expression Development Multiple selection Nested if Code alignment and end of block == and is

Review Output Number String Combination (by using + and str) Input Type Conversion (float and int) Processing Feel it in the test

Condition and Decision Making A deterministic output by a given input Big-or-small? Random.randint(1,6), store and not show Guess 123 small or 456 big Won or lost?

No Yes Condition Action 1 Action 2 Action 3

Boolean Expression Action 1 If controlled Action 2 else controlled Yes No Boolean Expression Action 1 If controlled Action 2 else controlled Action 3 11/9/2018

Syntax and Execution If block if condition : action 1 (statements 1) else : action 2 (statements 2) # a blank line to separate action 3 (statement 3)

Boolean Expression Simple condition Complex condition Format <Value> <relational operators> <Value> Number value relational operators ==, !=, <, >, <=, >= !!! Number one error: “a=2” instead of “a==2” String value relational operators left for later discussion Complex condition And, or, not Truth table

Truth table Precedence order F C2 C1 and C2 C1 or C2 not C1 Truth table Precedence order http://www.mathcs.emory.edu/~valerie/courses/fall10/155/resources/op_precedence.html

Relational operators have lower precedence than math operators. (7 - 1) * 5 + 3 > 7* 5 6 * 5 + 3 > 35 30 + 3 > 35 33 > 35 False Relational operators cannot be chained (unlike math operators) 2 <= x <= 10 error! 11/9/2018

Development Process Identify two exclusive options Implement each handling in different action parts Identify the situation (values) for option selection Make a condition so that all the situation value for option part 1 will lead to this condition true. Verify all the situation value for option part 2 will lead to this condition false, otherwise, revise the above condition!

Multiple selection Nested if (chained condition) Example: letter grade

Nested if for multiple section problem If then case 1 Else if then case 2 else … end if End if

Conditional and alternative execution if condition : action 1 (statements 1) # no else! action 3 (statement 3)

Are they different, how much? x = int(input("enter Int: ")) y = 0 if x > 3 : y = 1 if x < 10 : y = 2 else : y =3 print(y) x = int(input("enter Int: ")) y = 0 if x > 3 : y = 1 if x < 10 : y = 2 else : y =3 print(y)

Try 0, 4, 11 and see the results! 3 4 2 11 1 2 4 11 3

Indeed x = int(input("enter Int: ")) y = 0 if x > 3 : y = 1 x = int(input("enter Int: ")) y = 0 if x > 3 : y = 1 if x < 10 : y = 2 else : y =3 print(y) x = int(input("enter Int: ")) y = 0 if x > 3 : y = 1 if x < 10 : y = 2 else : y =3 print(y)

My point One line in wrong place Structural procedure Could cause the program 99.99% different Location/alignment maters! Blank line is useful Structural procedure Ask yourself where (the next program segment line) the computer execution goes Before that, check if you can find the “if” for each “else”

Try enter “3” and “3”: a = int(input("first string: ")) b = int(input ("second string: ")) if a == b : print("same content") else: print("not the same content") if a is b : print ("same string") else: print ("not the same string")

Try this program and enter “3” and “3”. a = input("first string: ") b = input("second string: ") if a == b : print("same content") else: print("not the same content") if a is b : print ("same string") else: print ("not the same string")

Same code, try “a3” and “a3”. a = input("first string: ") b = input("second string: ") if a == b : print("same content") else: print("not the same content") if a is b : print ("same string") else: print ("not the same string")