Flow control 1: if-statements (Liang 72-80) if(radius < 0) { System.out.println(“cannot get area: radius below zero”); } else { double area = radius *

Slides:



Advertisements
Similar presentations
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.
Advertisements

CS0007: Introduction to Computer Programming
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 4 (Conditional Statements) © CPCS
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Chapter 6 Horstmann Programs that make decisions: the IF command.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
(c) 2003 E.S.Boese1 Conditionals Chapter 10 - Student.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
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
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.
CPS120: Introduction to Computer Science Decision Making in Programs.
CIS 234: Control Structures - Selection Dr. Ralph D. Westfall April, 2010.
Conditionals & boolean operators
Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter Homework #5.
Flow of Control Part 1: Selection
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
Fundamental Programming: Fundamental Programming Introduction to C++
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.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
1 2. Program Construction in Java. 2.4 Selection (decisions)
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
1 Chapter 4: Basic Control Flow ► Chapter Goals  To be able to implement decisions using if statements  To understand statement blocks  To learn how.
Week 8: Decisions Bryan Burlingame 21 October 2015.
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 4 – Logical Operators & Branching.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 If’s – The Basic Idea “Program” for hubby: take out garbage.
CSC Programming for Science Lecture 10: Boolean Expressions & More If ­ Else Statements.
By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
Control statements Mostafa Abdallah
CPS120: Introduction to Computer Science Decision Making in Programs.
CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
Decision Statements, Short- Circuit Evaluation, Errors.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
USING CONDITIONAL CODE AMIR KHANZADA. Conditional Statement  Conditional statements are the set of commands used to perform different actions based on.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
IF STATEMENTS AND BOOLEAN EXPRESSIONS. BOOLEAN EXPRESSIONS Evaluate to a value of true or false Use relational or equivalence operators Boolean operators.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Microsoft Visual Basic 2005 BASICS
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Conditionals & Boolean Expressions
Logical Operations In Matlab.
Selection Statements.
Summary Two basic concepts: variables and assignments Basic types:
Boolean Expressions to Make Comparisons
Lecture Notes – Week 2 Lecture-2
Relational Operators.
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Presentation transcript:

Flow control 1: if-statements (Liang 72-80) if(radius < 0) { System.out.println(“cannot get area: radius below zero”); } else { double area = radius * radius * PI; System.out.println(“circle radius ”+radius+“ has area ”+area); } if-statements allow conditional execution of commands (you can execute different pieces of code in different situations) For example, in the ComputeArea program (2 nd chapter lectures) we could improve the program as follows

If statements and Boolean expressions An expression whose value is true or false is a boolean expression A simple if-statement has the following form: if ( some boolean expression ) { // do this if the boolean works out true } Where I’ve typed some boolean expression above, you can have any expression that returns a value of either true or false (e.g. radius < 0, which is either true or false ). There must be brackets around this expression. else { // do this otherwise } The else part is optional: you can stop here if you want the if but don’t need the else

Comparison (relational) operators (Liang ) int x = 5; To use the if statement, we need to do comparisons (to find out if something is true or false). These comparisons are all boolean expressions. Now consider the value of each of the following expressions x == 5 // true or false? x < 5 // true? false? x != 5 // true? false? x >= 5 // true? false? Assume the following initial value: true : == means ‘check if these two numbers are equal’ false : < means ‘check if the 1 st number is less than the 2 nd ’ false : != means ‘check if these two numbers are NOT equal’ true : >= means ‘check if the 1 st number is greater than or equal to the 2 nd number’

if ( ) { //whatever statements we want to carry out if the above is true } myNumber < 0myNumber >= 0myNumber != 0myNumber == 0 Comparison operator summary These comparison operators are used to compare two numbers (ints floats, doubles etc.) < means ‘less than’> Means ‘greater than’ <= means ‘less than or equal to’>= Means ‘greater than or equal to’ == means ‘equal to’!= Means ‘not equal to’ We can use any of these operators in an if statement. For example, suppose we’re comparing a number variable myNumber with 0: One equal sign means assignment (putting something in a variable). IMPORTANT: the equality operator == uses two equal signs, not one.

Expressions and statements These are statements double d = 1.5; System.out.println("Hello there"); In assignment statements, the Right Hand Side may contain an expression. An expression is something whose value has to be worked out. double d2 = 1.5 * 3.2; An expression has a value, which may be a number..... Or may be the boolean value true or false.... boolean b = (x < 3); b will hold the value of expression (x < 3);

Simple if-statements int x = 3; int y = 2; if (x >= 4) { y = y + 1; System.out.println("We're in the if-statement"); } // what value does y have now? Note that statements inside the if-statement are within a block, and are written with extra indentation

More complex boolean expressions (Liang ) True or false? (x < 5) (x 0) // && means ‘and’ (x 0) // || means ‘or’ !(x < 5) // ! means ‘not’ Assume x = 3 and y = -1. This works out to be true This works out to be false, because while (x 0) is false (the whole thing is only true if the first part is true AND the second part is true ) This works out to be true because (x 0) is false (the whole thing is true if the first part is true OR the second part is true ) This works out to be false. (x < 5) is true, so !(x < 5) (which means NOT (X < 5) ) is false.

The && operator ("and") (true) && true (true) (false)false (false) (true)false (false) false

The || operator ("or") (true) true (true) (false)true (false) (true)true (false) false ||

The ! operator ("not") ! truefalse true

Example of complex conditions if ((mark >= 0) && (mark <= 10)) { System.out.println(“mark is between 0 and 10"); } else { System.out.println(“error: mark not between 0 and 10"); } This would be used in a program for submitting student marks (for your assignments, for example). Notice there are no semi-colons (;) after the if line or the else line.

Another example boolean reducedFare = false; if ((age 65)) { reducedFare = true; } // later in the program.... if(reducedFare) { // do something appropriate }

if.....else if ( ) { // statements for the true case } else { // statements for the false case } How to do either one thing or the other:

Flowchart for if...else boolean expression false true statements for the true case statements for the false case next statement

Sample if...else if (radius >= 0) { area = radius * radius * PI; System.out.println("Area is" + area); } else { System.out.println("Negative input!"); }