Java Coding 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Decisions, decisions…!

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Example - The.
Java Coding OOP David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Towards Event-driven programming &
Everyday Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction to.
Introduction to UML David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. …Unified Modeling Language.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
J. Michael Moore Structured Programming CPSC 110 Drawn from James Tam's material.
Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections.
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
Java Coding 3 David Davenport Computer Eng. Dept.,
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Examples.
Introduction to Computer Programming Decisions, Decisions: Boolean Expressions and Selection.
Review: OOP & Arrays David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. …from CS101.
Robo David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction to the engineering.
The If/Else Statement, Boolean Flags, and Menus Page 180
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
Java Coding David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
Java Coding 4 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Method madness.
Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
Java Coding 3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Over & over again!
Java Coding 5 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
From Algorithms to Architecture David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction.
Object-Oriented Programming Using C++ Third Edition Chapter 3 Making Decisions.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Visual C# 2005 Decision Structures. Visual C# Objectives Understand decision making Learn how to make decisions using the if statement Learn how.
Flow of Control Part 1: Selection
Computational Algorithms David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
Relational Operators Relational operators are used to compare two numeric values and create a boolean result. –The result is dependent upon the relationship.
Java Coding David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Syntax for Variables & Constants Input,
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
Java Coding 6 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Collections.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Agenda Basic Logic Purpose if statement if / else statement
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Java Coding OOP_3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Some important Java interfaces +
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
From Algorithms to Architecture David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. lightning introduction.
Chapter#3 Part1 Structured Program Development in C++
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Examples.
An Object-Oriented Approach to Programming Logic and Design Chapter 5 Making Decisions.
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Java Coding 6 David Davenport Computer Eng. Dept.,
Java Coding – part 2 David Davenport Computer Eng. Dept.,
Boolean expressions and if-else statements
Java Coding 2 David Davenport Computer Eng. Dept.,
Making Choices with if Statements
Java Coding Extra David Davenport Computer Eng. Dept.,
CHAPTER 4 Selection CSEG1003 Introduction to Computing
Java Coding 3 – part2 David Davenport Computer Eng. Dept.,
Java Coding 3 David Davenport Computer Eng. Dept.,
Boolean Expressions and If
Java Coding 8 David Davenport Computer Eng. Dept.,
Java Coding 4 David Davenport Computer Eng. Dept.,
SELECTION STATEMENTS (2)
Java Coding 6-extra David Davenport Computer Eng. Dept.,
Selection Statements.
Java Coding 6 – part2 David Davenport Computer Eng. Dept.,
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Java Coding 4 (part2) David Davenport Computer Eng. Dept.,
Java Coding 6_part3 David Davenport Computer Eng. Dept.,
The Selection Structure
Using C++ Arithmetic Operators and Control Structures
Java Coding 6 David Davenport Computer Eng. Dept.,
Java Coding 6 David Davenport Computer Eng. Dept.,
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept.,
Presentation transcript:

Java Coding 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Decisions, decisions…!

IMPORTANT… Students… This presentation is designed to be used in class as part of a guided discovery sequence. It is not self- explanatory! Please use it only for revision purposes after having taken the class. Simply flicking through the slides will teach you nothing. You must be actively thinking, doing and questioning to learn! Instructors… You are free to use this presentation in your classes and to make any modifications to it that you wish. All I ask is an saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it. thank you, David. David

Decision The Java if statement if (condition) statement T ; else statement F ; if (condition) statement T ; where statement is any Java statement condition is a boolean expression

Conditions Any expression with Boolean result boolean variable isOver21 taxablefound Method with boolean result exists() isSent( my ) Operand relationalOperator Operand (where relationalOperator is: >, =, <=, ==, != ) age >= 21 speed == 0 year % 4 != 0 Boolean expressions combined with logicalOperator (where logicalOperator is: &&, ||, ! ) height > 2 && weight 10 ! exists() a Char >= ‘0’ && aChar <= ‘9’

Examples (1) Print message when x is positive if ( x > 0 ) System.out.println( “The value of x is positive”); Print warning if input exceeds threshold if ( inputValue >= THRESHOLD ) System.out.println ( “Warning – overload!”); Report whether x is negative or not if ( x < 0 ) System.out.println ( “Negative”); else System.out.println ( “Positive or zero”); Rewrite with alternative condition Check user’s password if ( !actualPassword.equals( enteredPassword) ) System.out.println( “Sorry, incorrect Password”); else // do secure things!

Examples (2) Compute z as absolute value of x-y if ( x – y < 0 ) z = y – x; else z = x – y; if ( x > y ) z = x - y; else z = y - x; z = x – y; if ( z < 0 ) z = -z; Can also use z = Math.abs(x-y);

Examples (3) Given three values stored in variables first, second, third, store the minimum in a variable called min if ( first < second ) min = first; else min = second; if ( third < min ) min = third; Generalise…?

Examples (4) Avoid divide-by-zero errors if ( y = 0 ) System.out.println( “Error: can’t divide by zero”); else z = x / y; System.out.println( “The result is “ + z ); Use braces (curly brackets) to form compound statement {statement; statement;  statement; } statement; 

Examples (5) Choosing between three alternatives: if ( x < 0 ) System.out.println( “Negative”); else { if ( x == 0 ) System.out.println( “Zero”); else System.out.println( “Positive”); } if ( x >= 0 ) { if ( x == 0 ) System.out.println( “Zero”); else System.out.println( “Positive”); } else System.out.println( “Negative”);

Examples (6) A neater way of writing multi-choice code segments is (nested if): if ( x = 0 System.out.println( “Zero”); else// x >= 0 and x != 0 System.out.println( “Positive”);

Distinguish… if (cond) print “A” else if (cond) print “B” else if (cond) print “C” else print “D” if (cond) print “A” if (cond) print “B” if (cond) print “C” if (cond) print “D”