This section gives several examples of if statements. In this first example, if a certain condition holds true, the single line of code immediately following.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
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.
Static Methods Static methods are those methods that are not called on objects. In other words, they don’t have an implicit parameter. Random number generation.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
Chapter 6 Horstmann Programs that make decisions: the IF command.
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Lecture 1 The Basics (Review of Familiar Topics).
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
Logical Operators and Conditional statements
* Calls to methods may have implicit parameters. * Explicit parameters may not be needed because the method has the implicit parameter to work with. *
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
4.1 Instance Variables, Constructors, and Methods.
Computer Science Selection Structures.
EXPRESSIONS AND ASSIGNMENT CITS1001. Scope of this lecture Assignment statements Expressions 2.
CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else.
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
 Java has the logical operators and, or, and not, symbolized by &&, ||, and !, respectively.  Logical or means one or the other or both conditions hold.
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.
Fundamental Programming: Fundamental Programming Introduction to C++
CPS120: Introduction to Computer Science Decision Making in Programs.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
Primitive data Week 3. Lecture outcomes Primitive data – integer – double – string – char – Float – Long – boolean Declaration Initialisation Assignments.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Java Programming Fifth Edition Chapter 5 Making Decisions.
Decision Statements, Short- Circuit Evaluation, Errors.
Controlling Program Flow with Decision Structures.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
USING CONDITIONAL CODE AMIR KHANZADA. Conditional Statement  Conditional statements are the set of commands used to perform different actions based on.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Java Programming Fifth Edition
User-Written Functions
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Chapter 4 C Program Control Part I
Selections Java.
Java Primer 1: Types, Classes and Operators
Lecture 3- Decision Structures
EGR 2261 Unit 4 Control Structures I: Selection
Boolean Expressions and If
Topics The if Statement The if-else Statement Comparing Strings
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
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,
if-else-if Statements
IF if (condition) { Process… }
1) C program development 2) Selection structure
The System.exit() Method
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
Just Enough Java 17-May-19.
Using C++ Arithmetic Operators and Control Structures
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Presentation transcript:

This section gives several examples of if statements. In this first example, if a certain condition holds true, the single line of code immediately following it is executed. If the condition does not hold true, then that line of code is not executed. In either case, any lines of code in the program following that line of code are executed.

First Example if(condition) statement; Notice that the keyword if and the set of parentheses containing the condition do not form an independent line of code. They should not be followed by a semicolon. If the condition is followed immediately by a semicolon, this causes a logic error which is hard to see.

First example, cont. The system tests the truth of the condition. If there is a semicolon immediately following the parentheses, it doesn’t matter whether the result is true or false. The semicolon signals the end of the if. The following line of code would not depend on it and it would simply be executed.

Second Example if(condition) statement1; else statement2; The second example shows an if statement with 2 mutually exclusive alternatives. In the course of execution, one or the other of the two statements has to be executed, but it is impossible for both to be executed.

Second Example, cont. Like the line of code containing the if, the else does not form an independent line of code, and should not be followed by a semicolon. If it is followed by a semicolon then any line of code following it would be executed unconditionally.

Using braces In both of the examples above, the single statements can be replaced by a block of code enclosed in braces. if(condition) { statement1; statement2; … } else { … }

Conditions The conditions inside if statements are frequently comparisons between numeric values. The comparison operators in Java are as follows <less than <=less than or equal to >greater than >=greater than or equal to !=not equal to ==equal to. Notice the difference between this and assignment.

Note for comparing float values In a previous unit the point was made that Java is particular in the way it handles floating point values. It is important to keep this in mind when doing numeric comparisons, especially when mixing integer and floating point type variables. If a variable contains an inexact binary representation of a decimal value, the result of the comparison may not be what you have in mind.

Examples of if statements It would be possible to write any number of fragments of code illustrating the models shown above. Here is the code for the Cup3 class It contains a method, increaseSeedCount(), to increase the number of seeds in a cup.

Examples of if statements, cont. public class Cup3 { private int seedCount; public Cup3() { seedCount = 0; } public Cup3(int initialCount) { seedCount = initialCount; } public int getSeedCount() { return seedCount; } public void setSeedCount(int newCount) { seedCount = newCount; } public void increaseSeedCount(int addedNumber) { seedCount = seedCount + addedNumber; }

Examples of if statements, cont. Observe that in spite of the way things are named using words like increase and added, it would be possible to send a negative parameter to the increaseSeedCount() method. It is possible to use an if statement in the method code to prevent a negative value from being acted on. A simple attempt to handle this might look like the following:

Examples of if statements, cont. public void increaseSeedCount(int addedNumber) { if(addedNumber > 0) { seedCount = seedCount + addedNumber; } This is not a complete solution. It is convenient that the if statement prevents an undesired outcome. It is not convenient that a programmer could call the method and not be warned that the code to increment was not executed. Ways of addressing this problem will be taken up later.