Logical Operators and While Loops CS303E: Elements of Computers and 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

Chapter 4: Control Structures I (Selection)
Chapter 2 Flow of Control. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives Boolean Expressions Building, Evaluating.
Chapter 4 - Control Statements
Chapter 4 Decision Making Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E. Reingold.
Control Flow Statements: Repetition/Looping
IF statement (i) Single statement. IF ( logical expression ) statement Example: read(*,*) a if (a. lt. 0) a = -a write(*,*) a Or read(*,*) a if (a < 0)
Selection Victor Norman CS104 Calvin College. Reading Quiz Counts toward your grade.
Making Decisions in Python Sec 9-10 Web Design. Objectives The student will: Understand how to make a decision in Python Understand the structure of an.
Overview Program flow Decision / branching / selection structures True & False in Python Comparison operators & Boolean expressions if … if … else if …
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Week 10 Recap CSE 115 Spring For-each loop When we have a collection and want to do something to all elements of that collection we use the for-each.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Geography 465 Assignments, Conditionals, and Loops.
Decision Statements. Control Stuctures Sequential Processing Sequential Processing Do In OrderDo In Order Do TogetherDo Together If / Else Statements.
Chapter 9 IF Statement Bernard Chen. If Statement The main statement used for selecting from alternative actions based on test results It’s the primary.
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.
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Visual Basic CDA College Limassol Campus COM123 Visual Basic Programming Semester C Lecture:Pelekanou Olga Week 4: Understand and implement Decisions.
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
Lesson thirteen Conditional Statement "if- else" ©
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
If statement.  It is made up of three main components:  The keyword itself,  an expression that is tested for its truth value,  and a code suite to.
Expressions Methods if else Statements Loops Potpourri.
Conditional statements and boolean expressions Arithmetic, relational and logical operators.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Control Structure  What is control Structure?  Types of Controls  Use the control structure in VBScript.  Example Summery.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Random Functions Selection Structure Comparison Operators Logical Operator
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
Control Flow (Python) Dr. José M. Reyes Álamo.
Making Choices with if Statements
Logic Gates.
Logical Operators and While Loops
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13]
JavaScript conditional
Intro to Computer Science CS1510 Dr. Sarah Diesburg
IF if (condition) { Process… }
Computers & Programming Languages
Types, Truth, and Expressions (Part 2)
Nate Brunelle Today: Repetition, A Trip To The Moon
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
DESICION TABLE Decision tables are precise and compact way to model complicated logic. Decision table is useful when input and output data can be.
Logic Gates.
Practice with loops! What is the output of each function below?
Chapter 8: More on the Repetition Structure
Logical Operations In Matlab.
Computer Science Core Concepts
Nate Brunelle Today: Conditional Decision Statements
Conditional Logic Presentation Name Course Name
Types, Truth, and Expressions (Part 2)
Logical Operators and While Loops
Nate Brunelle Today: Conditional Decision Statements
ECS15 boolean.
Decisions, decisions, decisions
Nate Brunelle Today: Conditional Decision Statements
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
Decision Statements.
Conditionals.
COMPUTING.
Presentation transcript:

Logical Operators and While Loops CS303E: Elements of Computers and Programming

Quote of the Day Optimism is an occupational hazard of programming: testing is the treatment. - K. Beck

Logical Operators: What are they? Operators that act on boolean expressions Operators that act on boolean expressions –We’ll use them mainly on comparisons The result is a boolean The result is a boolean and, or, and not and, or, and not

Logical Operators: How do they work? Logical Operator ExpressionResult p and qTrue if p and q are both true, otherwise False p or qTrue if p, q, or both p and q are True, False if both are False not pTrue if p is False, False if p is True

Logical Operators: When do we use them? Decisions ( if-elif-else ) Decisions ( if-elif-else ) Loops … Loops …

Examples i = 14 j = 18 test = i<j test = i<j and i==i test = not(i<j) test = i<j or j<i test = i<j and j<i

Examples What about: a or not b and c ? a (or (not b) and c) Precedence:notandor

Question: Logical Operators What is the expected output? i=23j=5k=-19 if(i>20 and k>0): print “i and k” print “i and k” elif(i>20 or j 20 or j<0): print “i or j” print “i or j”else: print “neither one” print “neither one” A. i and k B. i or j C. neither one D. i or j neither one neither one

Loops: What are they? How we tell the computer to repeat a set of commands How we tell the computer to repeat a set of commands –Each repetition is called an iteration Two types: Two types: –Definite: loop a certain number of times Next week Next week –Sentinel (Indefinite): loop until the sentinel value is read while loop while loop

While Loops: Examples while I’m hungry: eat another bite while there are more lines of text in the input file: read the next line from the file

While Loops: When do we use them? Whenever we are unsure how many times the loop should execute: Whenever we are unsure how many times the loop should execute: –Reading lists from the user –Reading information from a file –Converging on a solution (math, chemistry, …) Typically, sentinel loops, where the loop gets and processes values until seeing a special value called the sentinel, that indicates processing should stop. Typically, sentinel loops, where the loop gets and processes values until seeing a special value called the sentinel, that indicates processing should stop.

While Loops: How do they work? Execute a block of code repeatedly as long as the associated condition is true –Is the condition true? Yes, then execute action, then evaluate the condition, if true, execute the action, then evaluate the condition… Yes, then execute action, then evaluate the condition, if true, execute the action, then evaluate the condition… No, then skip the loop body and continue with the rest of the code No, then skip the loop body and continue with the rest of the code

Sentinel Loops: Typical Pseudocode get initial value while value is not the sentinel: process the value process the value get the next value get the next value continue with regular code

While Loops: Syntax while(<cond>):… loop body … Boolean expression Evaluated every time the loop begins Loop continues while this is true Indentation matters!

While Loops: Examples count = 1 while(count<=5): count=count+1 count=count+1 print count

While Loops: Examples #initialize variables to use in the loop num = 1 sum = 0 #as long as num is at most 5, add num to sum while num <= 5: sum = sum+num sum = sum+num num = num+1 #update the loop variable! num = num+1 #update the loop variable! #What happens if you forget? #What happens if you forget? print “The sum of the first five positive\ integers is “, sum

While Loops: Examples userVal=input(“Enter a value or -1 to quit”) sum=0 while(userVal != -1): sum=sum+userVal sum=sum+userVal userVal=input(“Enter a value or -1 to quit”) userVal=input(“Enter a value or -1 to quit”) print sum

Loops: Coding Hints To create a loop, ask yourself these three questions: 1. Where does it start? 2. Where does it end? 3. How do I go from one iteration to the next?

Exercises Write a program that prints the integers from 1 to 15, one number per line. Write a program that prints the integers from 1 to 15, one number per line. Write a program that reads a number n from the keyboard and prints the sum of the first n positive integers Write a program that reads a number n from the keyboard and prints the sum of the first n positive integers Write a program that reads integers from the keyboard and then prints the max and min to the screen Write a program that reads integers from the keyboard and then prints the max and min to the screen

Exercises Prompt the user to enter exam scores, entering a negative number to quit. Find the average and print it. Prompt the user to enter exam scores, entering a negative number to quit. Find the average and print it.

Question: While Loops What is the expected output? count=5while(count>0): print count, print count, count = count – 1 count = count – 1 A C B D

Reminder Project 3 is due on Saturday Project 3 is due on Saturday