Decisions: Conditional Statements (informally called If Statements) IST 256 Application Programming for Information Systems.

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

Discussion1 Quiz. Q1 Which of the following are invalid Java identifiers (variable names)? a) if b) n4m3 c) Java d) e) DEFAULT_VALUE f) bad-choice.
Logic & program control part 2: Simple selection structures.
Subject: Information Technology Grade: 10
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu.
Chapter 6 Horstmann Programs that make decisions: the IF command.
Additional control structures. The if-else statement The if-else statement chooses which of two statements to execute The if-else statement has the form:
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
1 Selection in C. 2 If / else if statement:  The else part of an if statement can be another if statement. if (condition) … else if (condition) … else.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
An intro to programming The basic nitty-gritty that’ll make you scratch your head and say, “This is programming?”
Control Structures: Getting Started Sequence and Selection also arithmetic operators, data types, logical operators.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
ACSE th Conference The Iconic Programmer Stephen Chen.
Selection in C.
S517 Web Programming Assistant Professor Xiaozhong Liu
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
Programming Logic and Design Fourth Edition, Introductory
CS0004: Introduction to Programming Relational Operators, Logical Operators, and If Statements.
Programming Logic and Design Sixth Edition
Chapter 3 Making Decisions
CHAPTER 4: CONDITIONAL STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
Flow of Control Part 1: Selection
1 CSC103: Introduction to Computer and Programming Lecture No 7.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
© 2000 Scott S Albert Selection Structures Structured Programming 256 Chapter 4.
Control Structures (A) Topics to cover here: Introduction to Control Structures in the algorithmic language Sequencing.
22/11/ Selection If selection construct.
Selection Statements. Introduction Today we learn more about learn to make decisions in Turing ▫Nested if statements, ▫case statements.
Selection Structures: if and switch Statements. 2 Selection Statements –In this chapter we study statements that allow alternatives to straight sequential.
Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.
REVIEW No curveballs this time …. PROBLEM TYPE #1: EVALUATIONS.
ICT Introduction to Programming Chapter 4 – Control Structures I.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Computer Programming TCP1224 Chapter 5 The Selection Structure.
S517 Web Programming Assistant Professor Xiaozhong Liu
ITP © Ron Poet Lecture 6 1 More on if. ITP © Ron Poet Lecture 6 2 Remembering Tests  We often want to remember the result of a test, so that we can use.
2011-T1 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington  Rashina Hoda and Peter Andreae COMP 102 Rashina Hoda.
31/01/ Selection If selection construct.
REVIEW No curveballs this time …. PROBLEM TYPE #1: EVALUATIONS.
A: A: double “4” A: “34” 4.
Fundamental Programming Fundamental Programming More Expressions and Data Types.
Topics: Selection Statements. Processing Involving Selecting Instructions An instruction that allows deviation and selection to take place uses the ‘IF’
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
Computer Science 1000 Algorithms III. Multiple Inputs suppose I ask you to write a program that computes the area of a rectangle area = length * width.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 5 Making Decisions.
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
General Condition Loop A general condition loop just loops while some condition remains true. Note that the body of the loop should (eventually) change.
CS 31 Discussion, Week 2 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:30-1:30pm (today)
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
Web Programming: Loop Xiaozhong Liu
Numbers and arithmetic
Introduction to Decision Structures and Boolean Variables
Numbers and Arithmetic
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Web Programming: If Statement and Servlet
Introduction to C++ October 2, 2017.
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
SELECTION STATEMENTS (1)
Review Operation Bingo
And now for something completely different . . .
Visual Basic – Decision Statements
Selection Statements.
Conditional Logic Presentation Name Course Name
The if Statement Control structure: logical design that controls order in which set of statements execute Sequence structure: set of statements that execute.
Lecture 5 Binary Operation Boolean Logic. Binary Operations Addition Subtraction Multiplication Division.
Presentation transcript:

Decisions: Conditional Statements (informally called If Statements) IST 256 Application Programming for Information Systems

Previous Examples of Statements Declaration with initialization String name = “Obama”; String name = “Obama”; Declaration (without initialization) int age; int age; Declaration of multiple variables double price, taxamount, result; double price, taxamount, result; Assignment statements price = 20.55; price = 20.55; Variable type Variable name (no space, start With alphabet, case sensitive) Variable value (optional) (optional)

More Assignment Statements pricewithtax = price * ;The value on the right-hand-side of an assignment statement can be computed with an arithmetic expression pricewithtax = price * ; This can include other variables or even the same variable price = 5; price = price ; 3 Move that value to be the new value of the variable First compute the value

Problem with different possible outputs (for the same input) Input Output 1 Output 2 Example: Age > 20? Request Wine Yes, Great! No, you’re not old enough No, you’re not old enough

Conditional Statement Conditional statement can decide different actions based on a decision: If-then-else statement If (condition) { [some actions] } else { [some different actions]}

Example 1 If your age > 20, serve you wine; else, serve you soda… int age; String drink; age = ??; if (age > 20) { drink = “wine”; } else { drink = “soda”;}System.out.println(drink);

Practice 1 Shopping problem. If you purchase $50 or more, you get 10% off for your total bill; otherwise, you get 5% discount.

Example 2 If you have data plan, your monthly payment is 59.99; if you don’t have data plan, you only pay boolean dataplan; double payment; dataplan = true; //true or false if (dataplan) { payment = 59.99; } else { payment = 39.99;}

Multiple conditions If you are VIP user, and you purchased over $100, you get 15% discount boolean VIP; double payment; //VIP and payment input if ( VIP && payment > 100.0) { payment = payment – (payment * 0.15); }

Another Shopping problem. Suppose that there is a String variable called PayStatus that is either “Hourly” or “Salaried”, and that we want to compute the Pay of an employee using a variable Hours that tells how many hours they worked that week, and the variable PayRate that tells how much per hour that they make. Salaried employees are always paid as if they worked 40 hours, no matter how many hours they actually worked. If hourly employees work more than 40 hours in a week, then they get 1.5 times their pay rate for any hours over 40.