Flow of Control. Conditional Statements. Conditional Statements. CS256 Computer Science I Kevin Sahr, PhD. Lecture 15: The If-Statement

Similar documents
More Data Types. The Char Data Type. Variable Declaration. CS200: Computer Science I. Module 14 More Data Types

Class API. Class API. Constructors. CS200: Computer Science I. Module 19 More Objects

Nesting. Abstraction & Nesting. Example. if x is less than y output x is smaller else output y is smaller. CS256 Computer Science I Kevin Sahr, PhD

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Method. Why Write Methods? Overview. CS256 Computer Science I Kevin Sahr, PhD. Lecture 21: Writing Class Methods. ClassName.methodName(arguments)

Algorithms and Conditionals

Decision Making in C

Programming Language. Machine Code. CS200: Computer Science I. Module 3 Programming Languages

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Conditionals and Loops

CS 302: INTRODUCTION TO PROGRAMMING. Lectures 7&8

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Conditional Statements

Comparing Data. Comparing Floating Point Values. Comparing Float Values. CS257 Computer Science I Kevin Sahr, PhD

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

Programming in C++ PART 2

BRANCHING if-else statements

Information Science 1

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Lab 8: IF statement. Conditionals and Loops. Copyright 2012 Pearson Education, Inc.

Chapter 4: Conditionals and Loops

Lecture 5 Tao Wang 1

Language Reference Manual

Boolean Expressions (Conditions)

Introduction to C Programming. What is a C program?

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

CS111: PROGRAMMING LANGUAGE II

Chapter 3. Selections

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Logic & program control part 2: Simple selection structures

Jeliot A powerful Java tutor for beginners

COMP 202 Java in one week

Control flow, conditionals, boolean expressions, block statements, nested statements. Course website:

Java Bytecode (binary file)

CS 1803 Pair Homework 3 Calculator Pair Fun Due: Wednesday, September 15th, before 6 PM Out of 100 points

CS 106 Introduction to Computer Science I

Information Science 1

Example. CS 201 Selection Structures (2) and Repetition. Nested if Statements with More Than One Variable

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Key Differences Between Python and Java

8. Control statements

Introduction to C Programming

Control Structures in Java if-else and switch

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

CIS 130 Exam #2 Review Suggestions

CSCE 121 ENGR 112 List of Topics for Exam 1

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Getting started with C++ (Part 2)

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

CS 139 Practice Midterm Questions #2

CS 2510 Exam 1 Fall 2010

Control Structures in Java if-else and switch

Text Input and Conditionals

If Control Construct

CS313D: ADVANCED PROGRAMMING LANGUAGE

CSc Introduction to Computing

Ch 6. Functions. Example: function calls function

CS-211 Fall 2017 Test 1 Version Practice For Test on Oct. 2, Name:

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

BOOLEAN EXPRESSIONS CONTROL FLOW (IF-ELSE) INPUT/OUTPUT. Problem Solving with Computers-I

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Software and Programming 1

Chapter 1 INTRODUCTION

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

Boolean Expressions and if 9/14/2007

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

CS1 Lecture 3 Jan. 18, 2019

Improved algorithm. Java code. Control flow and conditionals CSC 1051 Villanova University. Dr Papalaskari 1. Java Programè Algorithm

How Do Robots Find Their Way?

Java code. Updated program. Control flow and conditionals CSC 1051 Villanova University. Dr Papalaskari 1. Java Programè Algorithm. Improved algorithm

MEHMET YAYAN - İSMAİL HAKKI ÖZTÜRK CS101/SEC.-2 CLASS NOTES 1. March 28-30, 2007

Programming with Java

CS 106 Introduction to Computer Science I

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

CS1 Lecture 3 Jan. 22, 2018

Get JAVA. I will just tell you what I did (on January 10, 2017). I went to:

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

CSE 114 Computer Science I

Syntax and Variables

Boolean Expressions. So, for example, here are the results of several simple Boolean expressions:

Flow Control. CSC215 Lecture

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

Making Decisions. C# Programming: From Problem Analysis to Program Design 2nd Edition. David McDonald, Ph.D. Director of Emerging Technologies

CS 11 java track: lecture 1

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Decision Structures. Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Computers in Engineering. Moving From Fortran to C Michael A. Hawker

DECISION STRUCTURES: USING IF STATEMENTS IN JAVA

CS 251 Intermediate Programming Java Basics

Intel x86 Jump Instructions. Part 5. JMP address. Operations: Program Flow Control. Operations: Program Flow Control.

Following is the general form of a typical decision making structure found in most of the programming languages:

COMP-202: Foundations of Programming. Lecture 6: Conditionals Jackie Cheung, Winter 2016

Software Design and Analysis for Engineers

Transcription:

CS256 Computer Science I Kevin Sahr, PhD Lecture 15: The If-Statement 1 Flow of Control the order of statement execution default is top-to-bottom 2 Conditional Statements we can change the flow of control using conditional statements conditional statements let us choose which statement to execute next they include: -statements -else statements switch statements (discussed later) for -statements the choice of what to execute next is determined by a boolean expression known as the -statement condition 3 Conditional Statements for -statements the choice of what to execute next is determined by a boolean expression known as the -statement condition 4

answer width The If-Statement syntax: (condition)» statement; boolean expression condition is true, execute statement. condition is false, skip statement. in either case, continue on with the statements that follow the -statement 5 The If-Statement condition evaluated true false statement 6 double width; = Input.readDouble( Enter width: ); (width < 0.0) Output.showMessage( NEGATIVE WIDTH ); Output.showMessage( I am here. ); user enters a negative width, outputs NEGATIVE NUMBER then I am here. user enters a positive width, outputs just I am here 7 int answer; = Input.readInt( Enter the answer: ); (answer == 42) Output.showMessage( YES! ); Output.showMessage( Here I am. ); user enters 42, outputs YES! then Here I am. otherwise outputs just Here I am. 8

length length length double max = 100.0, length; = Input.readDouble( Enter length: ); (length > max) length = max; Output.showValue( length is now, length); user enters a number greater than max, length is assigned the value max otherwise the value of length is unchanged 9 double max = 100.0, length; = Input.readDouble( Enter length: ); (length > max) length = max; Output.showValue( length is now, length); user enters a number greater than max, length is assigned the value max but shouldn t we tell the user we changed it? requires more than one statement inside the 10 Block Statement a block statement is several statements grouped together with curly braces { } can be used anywhere a single statement can be used for example, inside an -statement remember to put a comment on your } s 11 double max = 100.0, length; = Input.readDouble( Enter length: ); (length > max) Block Statement Output.showMessage( Warning: max length exceeded. ); Output.showValue( Length set to, max); length = max; } // { Output.showValue( length is now, length); user enters a number greater than max, user is warned that a change was made length is assigned the value max 12

Indentation Revisited Remember that indentation is for the human reader, and is ignored by the computer (total > MAX) Ouput.showMessage("Error!!"); errorcount = errorcount + 1; Despite what is implied by the indentation, the third line will execute whether the condition is true or not 13 The If-Else Statement the -statement allows our program to decide whether or not to execute a statement we can choose between executing two dferent statements by adding an else-clause to make an else statement 14 The If-Else Statement syntax: (condition)» statement1; else» statement2; the condition is true, execute statement1. the condition is false, execute statement1. one or the other is executed, never both statement1 and/or statement2 could be block statements 15 The If-Else Statement condition evaluated true false statement1 statement2 16

boolean s see Smaller.java and Payroll.java 17 Boolean Input/Output the Input and Output classes do not provide methods for direct boolean input and output can use an -else for output: b =...; b is a boolean expression (b) Output.showMessage( b is true ); else Output.showMessage( b is false ); 18 String Equals Method EX: String quit = new String( QUIT ); String inp = Input.readString(?: ); (inp.equals(quit)) Output.showMessage( bye ); else Output.showMessage( continuing ); output would depend on the String the user input 19 Person Class the Person is another object class example Person class API, compiled class file Person.class, and an example program PersonApp.java are on the class website spend some time looking at this example 20

Output.showMessage(p1.getName() Output.showMessage(p1.getName() Another Boolean Return Type Note in the Person class API the method: public boolean getisalive() This method has a return type of boolean can be invoked anywhere a boolean expression can be used 21 Boolean Return Type s assume we have created a Person object p1 we can invoke the getisalive method on the right hand side of a boolean assignment statement. EX: boolean isalive = p1.getisalive(); we can invoke the getisalive method in the condition of a conditional statement. EX: (p1.getisalive()) + " is alive."); else + " is dead."); 22 Lecture 15 Vocabulary flow of control conditional statement condition block statement -else statement 23