The most important decision statement

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

Chapter 13 Control Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Control Structures Conditional.
Programming Languages and Paradigms The C Programming Language.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Introduction to Control Statements Presented by: Parminder Singh BCA 5 th Sem. [ Batch] PCTE, Ludhiana 5/12/ Control Statements.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 4 Control Structures I: Selection.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
UNIT II Decision Making And Branching Decision Making And Looping
Computer Science Selection Structures.
Chapter 4: Control Structures I J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
COP-3330: Object Oriented Programming Flow Control May 16, 2012 Eng. Hector M Lugo-Cordero, MS.
Copyright Curt Hill A Quick Introduction to Looping Breadth not Depth.
Lecture 4 Control Structures MIT – AITI What are Control Structures? Control structures are a way to alter the natural sequence of execution in.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 4 Control Structures I: Selection.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
Control Flow Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Conditionals-part21 Conditionals – part 2 Barb Ericson Georgia Institute of Technology Nov 2009.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
1 Class Chapter Objectives Use a while loop to repeat a series of statements Get data from user through an input dialog box Add error checking.
Copyright © Curt Hill The C++ IF Statement The most important decision statement Part 1.
Copyright © Curt Hill Further Picture Manipulation Considering position.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Copyright © Curt Hill Flow of Control A Quick Overview.
TOPIC 8 MORE ON WHILE LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright © Curt Hill The C++ IF Statement More important details More fun Part 3.
The for Statement A most versatile loop
Chapter 4: Control Structures I
Lecture 4b Repeating With Loops
Chapter 4: Control Structures I
The switch Statement, and Introduction to Looping
More important details More fun Part 3
The for-each or for-all loop introduced in Java 5
Chapter 5: Control Structures II
Building Java Programs
Chapter 4: Control Structures I
The C++ IF Statement Part 2 Copyright © Curt Hill
Outline Altering flow of control Boolean expressions
Arrays in Java What, why and how Copyright Curt Hill.
CMSC 202 Java Primer 2.
CSE 1020:Control Structure
Practice with loops! What is the output of each function below?
Decision Structures and Indefinite Loops
Other displays Saving Arrays Using fors to process
The Java switch Statement
Chapter8: Statement-Level Control Structures April 9, 2019
Program Flow.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Decisions, decisions, decisions
Repetition Statements (Loops) - 2
Building Java Programs
Loops CGS3416 Spring 2019 Lecture 7.
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.
While and Do While Syntax, semantics and examples
Presentation transcript:

The most important decision statement The Java IF Statement The most important decision statement Copyright © 1998-2010 Curt Hill

Flow of Control in Java Recall the flow statements in Java: Decisions If Switch Case Loops for while do Break Here we consider the if Copyright © 1998-2010 Curt Hill

Why? There are too many times when we need a conditional statement One set of statements is executed in one set of circumstances and another otherwise We also want our programs to be robust Unlikely to abnormally terminate regardless of what the user does Copyright © 1998-2010 Curt Hill

Example Suppose the following statement: avg = total/count; If count is zero the program will abnormally terminate with a divide by zero exception The solution is nesting this in an if: if(count > 0) avg = total/count; Copyright © 1998-2010 Curt Hill

Example Revisited The basic form is: if (cond) statement; The if is a reserved word The parentheses are required They show the limit of the condition Only a single statement is allowed A compound statement may be used Copyright © 1998-2010 Curt Hill

A Condition Any expression of type boolean Usually a comparison May include boolean methods May include boolean operators Unlike C/C++ must be a real boolean Copyright © 1998-2010 Curt Hill

Two forms of if statement if (boolean) statement if (boolean) statement else statement Copyright © 1998-2010 Curt Hill

The Else There is no THEN like in Pascal or BASIC The else is optional The statement to do if true immediately follows the parenthesized condition The else is optional Else is a reserved word and signals the start of the statement to execute if the condition is false The if without an else means else do nothing Copyright © 1998-2010 Curt Hill

Compound statements Either the then statement or the else statement may be replaced with a compound statement In any combination Even the compound statement of an if may create local variables Their scope is confined to the compound statement Copyright © 1998-2010 Curt Hill

Example Program Modify a picture by increasing contrast Make the brighter pixels brighter still Dim the pixels already less than average brightness This requires an if because pixel processing is not uniform Copyright © 1998-2010 Curt Hill

Basic Program is Same public static void main(String [] a){ String fileName; FileChooser.setMediaPath (“…/mediasources"); fileName=FileChooser.pickAFile(); Picture p1=new Picture(fileName); p1.explore(); process(p1); } Copyright © 1998-2010 Curt Hill

Process Method Beginning static void process(Picture p){ for(int y=0;y<p.getHeight();y++) for(int x=0;x<p.getWidth();x++){ Pixel px = p.getPixel(x,y); int red = px.getRed(); int green = px.getGreen(); int blue = px.getBlue(); // Now what? Copyright © 1998-2010 Curt Hill

Process Method End if(red + green + blue > 128*3){ // brighten red = red*3/2; green = green*3/2; blue = blue*3/2; } else { // Dim red = red*2/3; green = green*2/3; blue = blue*2/3; px.setRed(red); px.setGreen(green); px.setBlue(blue); } // end of fors Copyright © 1998-2010 Curt Hill

Barbara Copyright © 1998-2010 Curt Hill