CS305j Introduction to Computing More Conditional Execution 1 Topic 13 More Conditional Execution " Great dancers are not great because of their technique;

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

CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
BUILDING JAVA PROGRAMS CHAPTER 4 Conditional Execution.
Copyright 2010 by Pearson Education Building Java Programs Chapter 4 Lecture 4-2: Advanced if/else ; Cumulative sum reading: 4.1, 4.3, 4.5; "Procedural.
1 BUILDING JAVA PROGRAMS CHAPTER 4 CONDITIONAL EXECUTION.
Repetition Chapter 4: Control structures. Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 2 Loop Statements After reading and studying this Section,
Basic Logical Operations (fascinating)
If Statements & Relational Operators Programming.
June 10, 2015ICS102: while & do-while1 while and do-while Statements.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
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.
Intro to Java while loops pseudocode. 1 A “Loop” A simple but powerful mechanism for “making lots of things happen!” Performs a statement (or block) over.
James Tam AND OR NOT NAND NOR XOR Basic logic operations (fascinating)
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: if and if/else Statements reading: 4.2 self-check: #4-5, 7, 10, 11 exercises:
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Logical Operators and Conditional statements
The If/Else Statement, Boolean Flags, and Menus Page 180
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
Copyright © Texas Education Agency, Computer Programming For Loops.
CS305j Introduction to Computing While Loops 1 Topic 15 Indefinite Loops - While Loops "If you cannot grok [understand] the overall structure of a program.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 4: Conditional Execution.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
CS305j Introduction to Computing Conditional Execution 1 Topic 12 Conditional Execution "We flew down weekly to meet with IBM, but they thought the way.
Topic 11 Scanner object, conditional execution Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Building Java Programs Chapter 4 Conditional Execution Copyright (c) Pearson All rights reserved.
Conditionals (Cont’d). 2 Nested if/else question Formula for body mass index (BMI): Write a program that produces output like the following: This program.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CHAPTER 4: CONDITIONAL STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
© 2005 Lawrenceville Press Slide 1 Chapter 5 Relational Operators Relational OperatorMeaning =greater than.
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: if and if/else Statements reading: 4.2 self-check: #4-5, 7, 10, 11 exercises:
Decisions  Relational operators  Operate on two numbers or two Strings  ==, !=, >, >=,
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
CSC 1051 M.A. Papalaskari, Villanova University Conditional Statements Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University.
1 Building Java Programs Chapter 5 Lecture 5-3: Boolean Logic and Assertions reading: 5.3 – 5.5.
IB Computer Science – Logic
Building Java Programs Chapter 4 Conditional Execution Copyright (c) Pearson All rights reserved.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Control statements Mostafa Abdallah
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: if and if/else Statements reading: 4.2 self-check: #4-5, 7, 10, 11 exercises:
Operators.
1 CS161 Introduction to Computer Science Topic #6.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
TestScore < 80 testScore * 2 >= < w / (h * h) x + y != 2 * (a + b) 2 * Math.PI * radius
TOPIC 8 MORE ON WHILE LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
AP Computer Science A – Healdsburg High School 1 Unit 7 - Conditional statements - Logical operators in Java - Example.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
General Condition Loop A general condition loop just loops while some condition remains true. Note that the body of the loop should (eventually) change.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
CS305j Introduction to Computing Parameters Case Study 1 Topic 10 Parameters Case Study " Thinking like a computer scientist means more than being able.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright 2008 by Pearson Education 1 The if statement Executes a block of statements only if a test is true if ( test ) { statement ;... statement ; }
Chapter 4 Repetition Statements (loops)
Bool operators and, or, not
Outline Altering flow of control Boolean expressions
Relational Operators Operator Meaning < Less than > Greater than
Lecture Notes – Week 3 Lecture-2
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Building Java Programs
SELECTIONS STATEMENTS
CS2011 Introduction to Programming I Selections (I)
Boolean logic suggested reading: 5.2.
Repetition Statements (Loops) - 2
Topics discussed in this section:
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
Primitive Data Types and Operators
Presentation transcript:

CS305j Introduction to Computing More Conditional Execution 1 Topic 13 More Conditional Execution " Great dancers are not great because of their technique; they are great because of their passion." -Martha Graham Based on slides for Building Java Programs by Reges/Stepp, found at

CS305j Introduction to Computing More Conditional Execution 2 Logical operators && || !  Boolean expressions can be joined together with the following logical operators:  The following 'truth tables' show the effect of each operator on any boolean values p and q: OperatorDescriptionExampleResult && and(9 != 6) && (2 < 3)true || or(2 == 3) || (-1 < 5)true ! not!(7 > 0)false pqp && qp || q true false true falsetruefalsetrue false p!p truefalse true

CS305j Introduction to Computing More Conditional Execution 3 Using Logical Operators if( 5 <= x <= 10 ) // syntax error if( 5 <= x && x <= 10 ) //okay if( 5 <= x || x <= 10 ) // okay, but...

CS305j Introduction to Computing More Conditional Execution 4 Evaluating Tests int x = 42; int y = 17; int z = 25; y < x && y <= z x % 2 == y % 2 || x % 2 == z % 2 x = y + z !(x < y && x < z) (x + y) % 2 == 0 || !((z - y) % 2 == 0) What is the result of each of the following expressions?

CS305j Introduction to Computing More Conditional Execution 5 More Practice  Write a method to count the number of factors in a positive integer 6 -> 1, 2, > 1, 2, 3, 4, 6, 12  Write a method to determine if a given number is prime, divisible only by itself and 1  Write a program to determine if two numbers are relatively prime, they don't share any factors other than 1.

CS305j Introduction to Computing More Conditional Execution 6 More Practice  Write a program that reads two numbers from the user and tells whether they are relatively prime (have no common factors other than 1). –Examples: Type two numbers: and 16 are relatively prime (run #2) Type two numbers: and 21 are not relatively prime 7 is a factor of 7 and 21