if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false.

Slides:



Advertisements
Similar presentations
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Advertisements

3. S/E with Control Structures 3.1 Relational Operators and Expressions 3.2 If and if-else Statements 3.3 The Type Double 3.4 Program Design with the While.
© 2006 Pearson Education Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
1 Chapter 3: Program Statements Lian Yu Department of Computer Science and Engineering Arizona State University Tempe, AZ
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Flow of Control (1) : Logic Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Week 10 Recap CSE 115 Spring For-each loop When we have a collection and want to do something to all elements of that collection we use the for-each.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Logical Operators and Conditional statements
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 Conditionals and Loops Now we will examine programming statements.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking.
Chapter 3: Program Statements
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
© 2006 Pearson Education 1 Obj: to use compound Boolean statements HW: p.184 True/False #1 – 6 (skip 3)  Do Now: 1.Test your “Charge Account Statement”
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
Chapter 3: Program Statements
Chapter 3: Program Statements Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Chapter 5: Conditionals and loops. 2 Conditionals and Loops Now we will examine programming statements that allow us to: make decisions repeat processing.
© 2004 Pearson Addison-Wesley. All rights reserved February 17, 2006 The ‘while’ Statement ComS 207: Programming I (in Java) Iowa State University, SPRING.
© 2004 Pearson Addison-Wesley. All rights reserved February 20, 2006 ‘do’ and ‘for’ loops ComS 207: Programming I (in Java) Iowa State University, SPRING.
If…else statements. Boolean Expressions Boolean expression - An expression whose value is either true or false true = 1 false = 0 Datatype: boolean.
CSC 1051 M.A. Papalaskari, Villanova University Conditional Statements Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University.
Chapter 5 Conditionals and Loops 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights.
Control Flow. Data Conversion Promotion happens automatically when operators in expressions convert their operands For example, if sum is a float and.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking.
Control statements Mostafa Abdallah
Chapter 3: Program Statements Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
1 Program Development  The creation of software involves four basic activities: establishing the requirements creating a design implementing the code.
Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science.
© 2006 Pearson Education Chapter 3: Program Statements Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Chapter 3: Program Statements
Sequence, Selection, Iteration The IF Statement
Operator Precedence Operators Precedence Parentheses () unary
Boolean Expressions and If
Boolean Expressions & the ‘if’ Statement
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 3: Program Statements
Debugging October 3, 2007 ComS 207: Programming I (in Java)
Logical Operators & Truth Tables.
Chapter 3: Program Statements
Chapter 3: Program Statements
Chapter 3: Program Statements
Chapter 3: Program Statements
CS139 October 11, 2004.
What output is produced by the following fragment?
Logic of an if statement
Debugging October 4, 2006 ComS 207: Programming I (in Java)
Chapter 3: Program Statements
Chap 7. Advanced Control Statements in Java
Chapter 3: Program Statements
Conditionals and Loops
‘do’ and ‘for’ loops October 1, 2007 ComS 207: Programming I (in Java)
Boolean Expressions & the ‘if’ Statement
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Presentation transcript:

if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false. If the condition is true, the statement is executed. If it is false, the statement is skipped.

if (sum > MAX) delta = sum - MAX; System.out.println ("The sum is " + sum); First, the condition is evaluated. The value of sum is either greater than the value of MAX, or it is not. If the condition is true, the assignment statement is executed. If it is not, the assignment statement is skipped. Either way, the call to println is executed next.  See Age.java (page 130)Age.java

condition evaluated false statement true

A condition often uses one of Java's equality operators or relational operators, which all return boolean results: == equal to != not equal to < less than > greater than <= less than or equal to >= greater than or equal to Note the difference between the equality operator ( == ) and the assignment operator ( = )

if ( condition ) statement1; else statement2;  See Wages.java (page 134)Wages.java  If the condition is true, statement1 is executed; if the condition is false, statement2 is executed  One or the other will be executed, but not both

condition evaluated statement1 true false statement2

a!a truefalse true

if (total < MAX+5 && !found) System.out.println ("Processing…");  Logical operators have precedence relationships among themselves and with other operators all logical operators have lower precedence than the relational or arithmetic operators logical NOT has higher precedence than logical AND and logical OR

if (count != 0 && total/count > MAX) System.out.println ("Testing…");

if ('+' < 'J') System.out.println ("+ is less than J");  The uppercase alphabet (A-Z) followed by the lowercase alphabet (a-z) appear in alphabetical order in the Unicode character set

Operator += -= *= /= %= Example x += y x -= y x *= y x /= y x %= y Equivalent To x = x + y x = x - y x = x * y x = x / y x = x % y

while ( condition ) statement; while is a reserved word If the condition is true, the statement is executed. Then the condition is evaluated again. The statement is executed repeatedly until the condition becomes false.

statement true condition evaluated false

for ( initialization ; condition ; increment ) statement; Reserved word The initialization is executed once before the loop begins The statement is executed until the condition becomes false The increment portion is executed at the end of each iteration The condition-statement-increment cycle is executed repeatedly

initialization; while ( condition ) { statement; increment; }

statement true condition evaluated false increment initialization