Conditions and if/else. Conditions score > 90 Evaluates to true (1) or false (0) Generally … variable operator variable variable operator constant.

Slides:



Advertisements
Similar presentations
L5:CSC © Dr. Basheer M. Nasef Lecture #5 By Dr. Basheer M. Nasef.
Advertisements

Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Selection Statements & Exam 1 Review Recitation – 2/13/2009 CS 180 Department of Computer Science, Purdue University.
Chapter 5 Selection Statements. Topics Controlling program flow selection –if –switch Boolean expressions –boolean primitive type –comparison operators.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
If/else and switch. Assignments Due – Lab 3 No reading – study for your quiz!
Conditions and if/else. Conditions score > 90 Evaluates to true (1) or false (0) Generally … variable operator variable variable operator constant.
Conditions and if/else. Conditions score > 90 Evaluates to true (1) or false (0) Generally … variable operator variable variable operator constant.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
CSCI 116 Decisions & Validation. Overview Constants Boolean conditions Decision logic Form validation 2.
Conditions and Decisions. Assignments Reading – Chapter 4 –
بسم الله الرحمن الرحيم CPCS203: Programming II. Objectives After you have read and studied this chapter, you should be able to Implement a selection control.
true (any other value but zero) false (zero) expression Statement 2
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Chapter 4 Making Decisions
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
CPS 125: Digital Computation and Programming Selection Structures: if and switch Statements.
Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
1 Session 3: Flow Control & Functions iNET Academy Open Source Web Programming.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Other Conditional Methods. Conditional Operator Conditional Operator (?:) Conditional operator ( ?: ) – Ternary operator: 3 arguments expression1 ? expression2.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
CONTROL STRUCTURE The if, elseif, and else & switch Statements 1.
Flow of Control Part 1: Selection
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Computer Science 111 Fundamentals of Programming I Making Choices with if Statements.
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
CHAPTER 8 CONTROL STRUCTURES Prepared by: Lec. Ghader R. Kurdi.
5. Conditionals & Loops Based on Java Software Development, 5 th Ed. By Lewis &Loftus.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
5. Conditionals & Loops Based on Java Software Development, 5 th Ed. By Lewis &Loftus.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Lesson - 5. Introduction While programming, we usually need to decide the path of the program flow according to the parameters and conditions. Actually.
CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab.
Chapter Making Decisions 4. Relational Operators 4.1.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Programming Perl in UNIX Course Number : CIT 370 Week 3 Prof. Daniel Chen.
What is a Boolean expression?
Control statements Mostafa Abdallah
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
1 9/26/05CS150 Introduction to Computer Science 1 Life is Full of Alternatives.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Conditional Control Structures Chapter 5 Review. The If Statement The if statement is a conditional control structure, also called a decision structure,
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Quiz 1 Exam 1 Next Monday. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) System.out.println(“You have an A!” ); else System.out.println(“You.
I F S TATEMENTS Tarik Booker CS 290 California State University, Los Angeles.
Lecture 3 Selection Statements
Selection (also known as Branching) Jumail Bin Taliba by
Expressions and Control Flow in JavaScript
Other Conditional Methods
Selection (if-then-else)
The switch statement: an alternative to writing a lot of conditionals
Bools & Ifs.
Relational, Logical, and Equality Operators
PROGRAM FLOWCHART Selection Statements.
Presentation transcript:

Conditions and if/else

Conditions score > 90 Evaluates to true (1) or false (0) Generally … variable operator variable variable operator constant

Comparison Operators < > <= >= == –NOT the same as = !=

Object Comparison == compares references Use.equals to compare objects –o1.equals(o2)

Examples x=5 y=8 z=5 MAX=10 initial=‘s’ x<y y>MAX x<=z z>=MAX initial==‘r’ x!=z

Logical Operators && || ! x=5 y=8 z=5 MAX=10 initial=‘s’ x MAX !(x>y)

Precedence (), [], * / % + - = > == != && || =

Short-Circuit Evaluation Stop evaluation when true/false value is determined x=6 y=9 x>2 || y > 13 x 13 var != null && var.getData() == 10

Logical Assignment and Negation in_range = (x>0 && x<=10) # 1 if x between 1-10, 0 otherwise same_initials = (first_initial==‘S’&& last_initial==‘R’) not_same_initials = not(first_initial==‘S’&& last_initial==‘R’) not_same_initials = (first_initial!=‘S’ || last_initial!=‘R’)

DeMorgan’s Theorem !(a && b) => (!(a) || !(b)) !(a || b) => (!(a) && !(b))

if Statement Condition must be surrounded by () Statements must be surrounded by {} Each statement must end with ; if (condition) { statements } if (age >= 16) { System.out.println(“You can get a driver’s license.”); } if (age > 21) { System.out.println(“You can purchase alcohol.”); System.out.println(“You can gamble.”); } if(age >= 16 && age < 21) { System.out.println(“You can drive but you cannot gamble.”); }

if/else Statement if(condition) { statements } else { statements } if(grade > 60) { System.out.println(“You passed the class.”); System.out.println(“Next up, CS112.”); } else { System.out.println(“Sorry, you did not pass.”); System.out.println(“Try again next semester.”); }

Nested if Statements if (condition) { statement } else { statement } } else { statement } if(grade > 60) { System.out.println("You passed the class.“); if(grade > 90) { System.out.println("You passed with an A!“); } } else { System.out.println("Sorry, you did not pass.“); }

Chained Conditionals if(num > 0 && num <= 10) { System.out.println(“Your number is between 1 and 10”); } else if(num > 10) { System.out.println(“Your number is too high”); } else { System.out.println(“Your number is too low”); }

Using Functions String getGrade(double score) { if(score > 90) { return “A”; } else if(score > 80) { return “B”; } else if(score > 70) { return “C”; } else if(score > 60) { return “D”; } else { return “F”; }

switch Statement switch(value) { case 1: //do something break; case 2: //do something break; default: //do something }

Exercises 1.Write a method that takes as input three integers representing a month, day, and year, converts the integer representation of the month to a string, and prints the date in the format Month_String Day, Year Example: March 17, 2004