CS2011 Introduction to Programming I Selections (I)

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.
Intro to CS – Honors I Control Flow: Loops GEORGIOS PORTOKALIDIS
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Week 4 Selections This week shows how to use selection statements for more flexible programs. It also describes the various integral types that are available.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
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.
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
The If/Else Statement, Boolean Flags, and Menus Page 180
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Selection in C.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
Flow of Control Part 1: Selection
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
1 Week 6 Branching. 2 What is “Flow of Control”? l Flow of Control is the execution order of instructions in a program l All programs can be written with.
If Statements If statements: allow the computer to make a decision Syntax: if (some condition) { then do this; } else { then do that; } no semicolons on.
1 2. Program Construction in Java. 2.4 Selection (decisions)
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
03 August 2004 NLP-AI Java Lecture No. 4 Operators & Decision Constructs Satish Dethe.
CSE 1341 Honors Note Set 05 Professor Mark Fontenot Southern Methodist University.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
COMP 110: Spring Announcements Lab 1 was due at noon Lab 2 on Friday (Bring Laptops) Assignment 1 is online TA Office hours online 30-min quiz at.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Basic Conditions. Challenge: ● Ask the user his/her name ● If it’s “Wally,” jeer him ● Pause video and try on your own.
Lesson thirteen Conditional Statement "if- else" ©
Introduction to Programming Python Lab 7: if Statement 19 February PythonLab7 lecture slides.ppt Ping Brennan
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
CS0007: Introduction to Computer Programming
Chapter 4: Making Decisions.
C-Language Lecture By B.S.S.Tejesh, S.Neeraja
Chapter 4: Making Decisions.
CHAPTER 4 Selection CSEG1003 Introduction to Computing
Introduction to Programming
Operator Precedence Operators Precedence Parentheses () unary
Selection Statements by Ahmet Sacan
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,
Chapter 4: Decision Structures and Boolean Logic
Chapter 4: Making Decisions.
Javascript Conditionals.
Bools & Ifs.
If statement.
Logical assertions assertion: A statement that is either true or false. Examples: Java was created in The sky is purple. 23 is a prime number. 10.
Chapter 4: Decision Structures and Boolean Logic
3 Control Statements:.
Introduction to Programming
CS2011 Introduction to Programming I Loop Statements (II)
CS2011 Introduction to Programming I Selections (II)
Selection Statements.
Summary Two basic concepts: variables and assignments Basic types:
Chapter 5: Control Structure
SELECTIONS STATEMENTS
CS2011 Introduction to Programming I Elementary Programming
Chapter 2 Programming Basics.
Relational Operators.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Simple Branches and Loops
CS150 Introduction to Computer Science 1
Introduction to Programming
Agenda Warmup Review Finish 1.2 Assignments Lesson 1.3 (If Statements)
CS2011 Introduction to Programming I Loop Statements (I)
Chapter 4: Decision Structures and Boolean Logic
Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement.
Agenda Warmup Review Finish 1.2 Assignments Lesson 1.3 (If Statements)
Presentation transcript:

CS2011 Introduction to Programming I Selections (I) Chengyu Sun California State University, Los Angeles

Mortgage Calculator Revisited The formula How do we include the case where monthly interests rate is 0? Check if the interest rate entered is 0, and based on the result, select different part of the program to execute.

What's Needed in a Programming Language Branch Statement Check if the interest rate entered is 0, and based on the result, select different part of the program to execute. Boolean Expression Boolean Value

Boolean Expressions Used to check a condition, and the result would be either true or false (i.e. boolean) For example: interestRate == 0 radius > 0

Relational Operators Notice the difference between = and == Name < Less than <= Less than or equal to > Greater than >= Greater than or equal to == Equal to != No equal to Notice the difference between = and == In Java, relational operators can only be used on numbers

Boolean Values and Variables boolean is a data type just like int and double There are only two possible boolean values: true and false Boolean variables can be use to store boolean values (usually the results of some condition checks) Example: SimpleBooleans.java

Example: Addition Quiz Generate two random numbers between 0 and 10 Ask the user to enter the sum of the two numbers Display true or false depending on whether the answer is correct Use java.util.Random to generate the random numbers

Branch Statements if statement condition if-else statement condition true condition if-else statement true condition false

if statement if ( boolean-expression ) { statement(s); } If the boolean expression evaluates to true, execute the statement(s) in {} {} can be omitted if there's only one statement

If-else statement if ( boolean-expression ) { statement(s); } else { If the boolean expression evaluates to true, execute the statement(s) in {} after if (a.k.a. the if block), otherwise execute the statements in {} after else (a.k.a. the else block) {} can be omitted if there's only one statement in the block

Examples Modify Addition Quiz so it display correct/incorrect instead of true/false Use if statement Use if-else statement Modify Mortgage Calculator to handle the case where the interest rate is 0

Check Multiple Conditions Example: LetterGrade.java Conditions Letter Grade score >= 90 A 80 <= score < 90 B 70 <= score < 80 C 60 <= score < 70 D score < 60 F

Use Nested if-else Statements Write proper conditions Understand that certain conditions are already met when we get to the else block Use proper syntax else{ if(){ } else if(){ } else{