ECE 122 Feb. 8, 2005. if Statement A condition is an expression that can be either true, or false. “if” statement allows a program to make a decision.

Slides:



Advertisements
Similar presentations
Control Structures Selections Repetitions/iterations
Advertisements

3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
 Control structures  Algorithm & flowchart  If statements  While statements.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 4 – Introducing Algorithms, Pseudocode and.
Chapter 5 Selection Statements. Topics Controlling program flow selection –if –switch Boolean expressions –boolean primitive type –comparison operators.
Introduction to Computers and Programming Lecture 5 New York University.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Conditional Statements Control Structures.
C++ for Engineers and Scientists Third Edition
Visual C++ Programming: Concepts and Projects
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
Chapter 4 Control Statements: Part I
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Control Structures Session 03 Mata kuliah: M0874 – Programming II Tahun: 2010.
CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
1 C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved. 4.
Chapter 04 Control Statements: Part II. OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
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.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Selection Statements Selection Switch Conditional.
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
Dale Roberts 1 Program Control - Algorithms Department of Computer and Information Science, School of Science, IUPUI CSCI N305.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Lecture 2 Control Structure. Relational Operators -- From the previous lecture Relational Operator Meaning == is equal to < is less than > is greater.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
CMSC 104, Version 9/011 Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else Statement Nesting of.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
Control Statements: Part1  if, if…else, switch 1.
ECE122 Feb 10, Unary Operator An operator that takes only a single operand Plus: + Minus: – Cast: (type). E.g. (double)
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C 1 A.AlOsaimi King Saud University College of Applied studies and Community Service Csc 1101.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
Chapter 7 JavaScript: Control Statements, Part 1
Lecture 3 Selection Statements
Branching statements.
Chapter 4 – C Program Control
Chapter 4: Making Decisions.
Control Statements: Part 1
Java™ How to Program, 10/e Late Objects Version
JavaScript: Control Statements I
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming Fundamentals
JavaScript: Control Statements.
JavaScript: Control Statements I
Chapter 4 Control Statements: Part I
MSIS 655 Advanced Business Applications Programming
3 Control Statements:.
Computer Science Core Concepts
Chapter 4 - Program Control
Branching statements Kingdom of Saudi Arabia
Presentation transcript:

ECE 122 Feb. 8, 2005

if Statement A condition is an expression that can be either true, or false. “if” statement allows a program to make a decision based on the value of a condition.

Equality Operators == e.g. x==ymeans x is equal to y != e.g. x!=ymeans x is not equal to y

Relational Operators >means “greater than” <means “less than” >=means “greater than or equal to” <=means “less than or equal to” e.g. x>y c<d e>=f g<=k

Rule of Operator Precedence All equality operators are of same precedence. All Relational operators are of same precedence. Equality operators are lower in operator precedence than relational operators. Relational operators are lower in operator precedence than arithmetic operators. Assignment operator is lower in operator precedence than equality operator Operator Precedence rule of thumb: When in doubt, add parenthesis!

Rule of Operator Precedence */% +- >= ==!= =

Common Programming Errors Forget left and/or right parenthesis for the condition in “if” statement. Confusing an equality operator, “==”, with an assignment operator, “=” ==, !=, = These operators cannot contain spaces between symbols like “= =” Reverse the operator !=, =, as in “=!, = Place a semicolon immediately after the right parenthesis of condition in an “if” statement

Algorithms A computer program of a finite set of well- defined instructions for solving a problem. A set of actions (statements) & the order in which they execute. Program control specifies the order in which these statements execute.

Algorithm of Wizard’s Valentine Date Get out of bed Take off pajamas Take a shower Get dressed Eat breakfast Drive to meet my date

Algorithm of Nutty’s Valentine Date Get out of bed Take off pajamas Get dressed Take a shower Eat breakfast Drive to meet my date

Order matters The order of statement execution matters. The order of execution are controlled by control structures

Java Control Structure Sequence Structure. By default, JVM executes Java statement in the order they are written. Selection Statements. Selectively execute some statements depending on the value of conditions. “if”, “if…else”, “switch” Repetition Statements. Looping Statement. Program performs statements repetitively as long as certain conditions remain true. “for”, “while”, “do…while”.

if Statement If the condition (boolean) is true, perform the action, otherwise, skip it. if (grade >= 60.0) System.out.println(“You have passed!”); if (grade >= 60) { // use block code in place of a single statement System.out.println(“You have passed!”); System.out.println(“Congratulations!”); }

if.. else Statement If the condition is true, perform the first statement(or block), otherwise, perform the second statement(or block). if (haveTicket) //when haveTicket == true { System.out.println(“Watch game at the Stadium!”); System.out.println(“I am lucky!”); } else //when haveTicket != true { System.out.println(“Watch TV at home.”); System.out.println(“I am okay.”); }

Nested if.. else Statement if.. else can be nested to have multiple selection. If (temperature > 85) System.out.println(“It is hot.”); else if (temperature<68) System.out.println(“It is cold.”); else System.out.println(“It is cool.”);

Dangling else problem Java compiler always associates an “else” with the immediately preceding “if” unless told otherwise by using {} Block code is java statements enclosed within {}. A block code can be place anywhere in a program that a single statement can be placed.

Example int l = 6; int m = 3; if (l > 5) if (m > 5) System.out.println("Both l & m are greater than 5."); else System.out.println("l>5 & m<5"); if (l > 5) { if (m > 5) System.out.println("Both l & m are greater than 5."); } else System.out.println("l<=5"); //We don’t know “m” yet at this point.

Conditional Operator Condition ? Do_if_true : do_if_false Can be used in place of “if.. else” statement It takes three operands. The first operand is the boolean expression. The second operand is the conditional expression if the boolean expression is true. The third operand is the conditional expression if the boolean expression is false. System.out.println(grade>=60 ? “passed” : “failed”);

Reading Assignment “Java 2”. Finish chapter 2. P71-76 of Chapter 3