Logical Operators and While Loops

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 Statements
Logical Operators and While Loops CS303E: Elements of Computers and Programming.
While loops.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Computer Science 1620 Loops.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
* What kind of loop would I use to complete the following: A. Output all of the prime numbers that are less than 100,000 B. Output the Fibonacci sequence.
More While Loop Examples CS303E: Elements of Computers and Programming.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Chapter 6 Looping CS185/09 - Introduction to Programming Caldwell College.
1 Three C++ Looping Statements Chapter 7 CSIS 10A.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Count and add list of numbers From user input and from file.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
Loops and Simple Functions CS303E: Elements of Computers and Programming.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Loops and Simple Functions COSC Review: While Loops Typically used when the number of times the loop will execute is indefinite Typically used when.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Repetition (While-Loop) version]
Lecture 7: Repeating a Known Number of Times
Python: Control Structures
Chapter 5: Control Structures II
Chapter 5: Control Structure
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Repetition-Counter control Loop
( Iteration / Repetition / Looping )
While Loops in Python.
Chapter 6: Conditional Statements and Loops
Lecture 07 More Repetition Richard Gesick.
Lecture 4B More Repetition Richard Gesick
Logical Operators and While Loops
Control Structure Senior Lecturer
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
TOPIC 4: REPETITION CONTROL STRUCTURE
Types, Truth, and Expressions (Part 2)
Loops CIS 40 – Introduction to Programming in Python
Let’s all Repeat Together
CHAPTER 6: Control Flow Tools (for and while loops)
CHAPTER 5: Control Flow Tools (if statement)
Repetition Statements (Loops) - 2
Loops and Simple Functions
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.
While Loops in Python.
REPETITION Why Repetition?
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Logical Operators and While Loops COSC 1301

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 We’ll use them mainly on comparisons The result is a boolean and, or, and not

Logical Operators: How do they work? Logical Operator Expression Result p and q True if p and q are both true, otherwise False p or q True if p, q, or both p and q are True, False if both are False not p True if p is False, False if p is True Create truth tables

Logical Operators: When do we use them? Decisions (if-elif-else) 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: not and or Just use parentheses!

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

Loops: What are they? How we tell the computer to repeat a set of commands Each repetition is called an iteration Two types: Definite: loop a certain number of times for loop Sentinel (Indefinite): loop until the sentinel value is read 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: 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.

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… 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 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! Parentheses are optional

While Loops: Examples count = 1 while(count<=5): count=count+1 print(count) Count is the loop variable Must be initialized first and then updated in the loop body

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 num = num+1 #update the loop variable! #What happens if you forget? print (“The sum of the first five positive\ integers is “, sum) Infinite loop: <ctrl>-c

While Loops: Examples userVal=int(input(“Enter a value or -1 to quit”)) sum=0 while(userVal != -1): sum=sum+userVal 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 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 For exercises, go through whole process: pseudocode, programming, testing (hand run, print statements…)

Exercises 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=5 while(count>0): print(count) count = count – 1