DECISION STRUCTURES: USING IF STATEMENTS IN JAVA
|
|
- Phillip Francis
- 6 years ago
- Views:
Transcription
1 DECISION STRUCTURES: USING IF STATEMENTS IN JAVA S o far all the programs we have created run straight through from start to finish, without making any decisions along the way. Many times, however, you may need to execute a different set of code based on criteria input by the user. Just as you might say to yourself, If she doesn t call me, I am not going to speak to her again, a computer program uses decision structures to decide what actions to take depending on certain conditions. The decision structure Java programmers use to perform this type of conditional logic are called if statements. All if statements use boolean expressions to determine whether to execute or skip a statement (or a block of statements). A boolean experession is a Java expression that, when evaluated, returns a boolean value that is, either true or false. Boolean expressions often compare the value of a variable with the value of some other variable, a literal, or perhaps a simple arithmetic expression through the user of something called a relational operator. RELATIONAL OPERATORS Java evaluates a boolean expression by first evaluating the expression on the left, then evaluating the expression on the right, and finally applying the relational operator to determine if the entire expression evaluates to true or false. The equal sign (==), for example, is a relational operator that compares the value of the expression to the left, with the value of the expression to the right. It always returns a TRUE or FALSE value. if (score == 0) The following are six (6) Java relational operators that are used to evaluate boolean expressions: OPERATOR NAME DESCRIPTION EXAMPLE < Less than Returns true if the expression on the left evaluates to a value that is less than the value of the expression on the right. if (score < 100) <= Less than or equal Returns true if the expression on the left evaluates to a value that is less than or equal to the value of the expression on the right. if (score <= 100) == Equal Returns true if the expression on the left evaluates to a value that is equal to the value of the expression on the right. if (score == 100) MAKING DECISIONS: Using If Statements in Java Page 1 of 8
2 != Not equal Returns true if the expression on the left does not equal the value of the expression on the right. if (score!= 100) >= Greater than or equal Returns true if the expression on the left evaluates to a value that is greater than or equal to the value of the expression on the right. if (score >= 100) > Greater than Returns true if the expression on the left evaluates to a value that is greater than the value of the expression on the right. if (score > 100) Note that the relational operator that tests for equality is two equal signs in a row (==). A single equal sign is the assignment operator. So if you try to use a single equal sign in a boolean expression you will get a compile-time error. THE if STATEMENT In its most basic form, an if statement lets you execute a single statement or a block of statements only if a boolean expression returns true. The following is the basic form of the if statement: if (boolean-expression) statement Note that the boolean expression must be enclosed in parentheses. Also, if you use only a single statement, it must end with a semicolon. But the statement can also be a statement enclosed by braces. In that case, each statement within the block needs a semicolon, but the block itself doesn t. Here s an example of a typical if statement: double hourlyrate = 10; In this example, a variable named hourlyrate is initialized to 8.00, and then set to if hoursworked is greater than 40. So what will hourlyrate be equal to if hoursworked is not greater than 40. If you answered 8.00, you re correct! Indenting the statement under the if statement is customary to make the structure of your code more obvious. It isn t necessary, but always a good idea. Here s an example that uses a block rather than a single statement: double MAKING DECISIONS: Using If Statements in Java Page 2 of 8
3 hourlyrate = 10; totalpay = hoursworked * hourlyrate; In the above example, the two statements within the braces are executed if hoursworked is greater than 40. Otherwise, neither statement is executed. Keep in mind that indentation by itself doesn t create a block. For example, consider this example: double hourlyrate = 10; totalpay = hoursworked * hourlyrate; In the above example, I didn t use the braces to mark a block, but indented the last statement as if it were part of the if statement. This will result in the last statement being executed whether or not the expression in the if statement returns true. It might be a good idea to always code if statements using statement blocks regardless of the number of statements that conditionally execute, as the following example illustrates: hourlyrate = 10; The reason why this would not be a bad idea is because it makes the structure of your code a little more obvious by adding extra white space around the statement. Also, if you later decide you need to add a few statements to the block, the braces are already there. THE if- STATEMENT Unlike an if statement which executes an instruction(s) if the conditional expression returns true, an if statement includes an optional clause that is executed if the conditional expression returns false. The basic format of an if- statement is: if (boolean-expression) statement statement Here s an example that uses an if- statement to determine one s hourly rate: double hourlyrate; MAKING DECISIONS: Using If Statements in Java Page 3 of 8
4 In this example, the hourlyrate is set to if the hoursworked is greater than 40. If the hoursworked is not greater than 40, then the hourlyrate is set to You can, of course, write the above statement using blocks as follows: double hourlyrate; NESTED if STATEMENTS The statement that goes in the if or part of an if- statement can be any kind of Java statement, including another if or -if statement. This is called NESTING, and an if or if- statement that includes another if or -if statement is called a NESTED IF STATEMENT. The general form of a nested if statement is as follows: if (expression1) if (expression2) statement1 statement2 if (expression3) statement3 statement4 In this example, expression1 is first evaluated. If it returns true, expression2 is evaluated. If that expression is true, statement1 is executed; otherwise statement2 is executed. But if expression1 is false, then expression3 is evaluated. If expression3 is true, statement3 is executed; otherwise statement4 is executed. Suppose I have two classes of employees: part-timers and full-timers and they get a different hourly rate based on the number of hours they work. I could implement a pay structure with a nested if statement as follows: MAKING DECISIONS: Using If Statements in Java Page 4 of 8
5 if (parttime == true) hourlyrate = 12.00; OR if (parttime == true) hourlyrate = 12.00; This example assumes that if the parttime variable isn t true, then the employee must be a full-time employee. In the above example, what would be my hourly rate if I was a full-time employee who worked a total of 40 hours? If you said 12.00, you are correct! THE if- if STATEMENT A common pattern for nested if statements is to have a series of if- if statements: The general format of an if- if statement is as follows: MAKING DECISIONS: Using If Statements in Java Page 5 of 8
6 if (expression1) statement1 if (expression2) statement2 if (expression3) statement3 For example, suppose I want to assign four different hourly rates based on the amount of hours worked. I can easily implement a series of if- if statements to assign different rates by doing the following: if (hoursworked > 30) hourlyrate = 12.00; if (hoursworked > 20) It is important to keep in mind that your boolean expression must be properly ordered because statements are executed for the first true condition only, and skips all the other conditions. For example, if I had changed the sequence of my statements to look like this, what would the hourly rate be for an employee who works 60 hours? if (hoursworked > 0) if (hoursworked > 20) if (hoursworked > 30) hourlyrate = 12.00; If you said 8.00, you re correct! These if statements sets the hourlyrate to 8.00 because the boolean expression in the first statement always returns true (assuming the hoursworked variable isn t zero or negative), thus resulting in the other if statements never being evaluated. THE switch STATEMENT The switch statement is sometimes useful when you need to select one of several alternatives based on the value of an integer or character type variable. The following is the basic form of a switch statement: switch (expression) case constant1: statements; break; MAKING DECISIONS: Using If Statements in Java Page 6 of 8
7 case constant2: statements; break; case constant3: statements; break; default: statements; break; You can code as many case groups as you want or need. Each begins with the word case followed by a constant (usually a simple numeric literal) and a colon. Then you code one or more statements that you want executed if the value of the switch expression equals the constant. The last line of each case group is a break statement, which causes the entire switch statement to end. The default group, which is optional, is like a catch-all case group. Its statements are executed only if none of the previous case constants match the switch expression (very similar to an statement). BOOLEAN OPERATORS Sometimes we may want our program to perform an action if more than one condition (logical AND) or one of multiple conditions is true (logical OR). For example, I may want to set an employee s hourly rate to if he/she is a full-time employee and works more than 40 hours a week. In this case we need to use a BOOLEAN OPERATOR. Boolean operators let you combine two or more conditions in the same expression. if (fulltime == true && hoursworked > 40) The following are Boolean operators that can be used to form more complex Boolean expressions: OPERATOR LOGIC BOOLEAN EXAMPLE && Both conditions must be true Either condition must be true! Result of the condition is reversed AND if (fulltime == true && hoursworked > 40) OR if (fulltime == true hoursworked > 40) NOT if!(hoursworked > 40) MAKING DECISIONS: Using If Statements in Java Page 7 of 8
8 ^ One and only one of the conditions is true XOR if (fulltime == true ^ hoursworked > 40) ILLUSTRATING BOOLEAN EXPRESSIONS WITH TRUTH TABLES How a compound Boolean expression evaluates with && and operators can be illustrated with TRUTH TABLES. Truth tables show the possible outcomes of compound Boolean expressions. AND (&&) A B RESULT True True True True False False False True False False False False OR ( ) A B RESULT True True True True False True False True True False False False XOR (^) A B RESULT True True False True False True False True True False False False A True False NOT (!) RESULT False True MAKING DECISIONS: Using If Statements in Java Page 8 of 8
Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal
Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program
More informationText Input and Conditionals
Text Input and Conditionals Text Input Many programs allow the user to enter information, like a username and password. Python makes taking input from the user seamless with a single line of code: input()
More informationSelec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal
Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal Lesson Goals Understand Control Structures Understand how to control the flow of a program
More informationLecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan
Lecture 08-1 Programming in C++ PART 1 By Assistant Professor Dr. Ali Kattan 1 The Conditional Operator The conditional operator is similar to the if..else statement but has a shorter format. This is useful
More informationChapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14
Chapter 4: Making Decisions 4.1 Relational Operators Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than or equal to
More informationDecision Making in C
Decision Making in C Decision making structures require that the programmer specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed
More informationThe following expression causes a divide by zero error:
Chapter 2 - Test Questions These test questions are true-false, fill in the blank, multiple choice, and free form questions that may require code. The multiple choice questions may have more than one correct
More information(I m not printing out these notes! Take your own.)
PT1420 Week 2: Software Program Design I (I m not printing out these notes! Take your own.) Today we'll be discussing designing programs: Algorithms and errors Flowcharts and pseudocode Sequence structures
More informationProblem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur
Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur Lecture 15 Branching : IF ELSE Statement We are looking
More informationChapter 4: Making Decisions
Chapter 4: Making Decisions 4.1 Relational Operators Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than or equal to
More informationChapter 4: Making Decisions
Chapter 4: Making Decisions CSE 142 - Computer Programming I 1 4.1 Relational Operators Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >=
More informationPython for Informatics
Python for Informatics Exploring Information Version 0.0.6 Charles Severance Chapter 3 Conditional execution 3.1 Boolean expressions A boolean expression is an expression that is either true or false.
More information5. Control Statements
5. Control Statements This section of the course will introduce you to the major control statements in C++. These control statements are used to specify the branching in an algorithm/recipe. Control statements
More informationConditional Programming
COMP-202 Conditional Programming Chapter Outline Control Flow of a Program The if statement The if - else statement Logical Operators The switch statement The conditional operator 2 Introduction So far,
More information3 The L oop Control Structure
3 The L oop Control Structure Loops The while Loop Tips and Traps More Operators The for Loop Nesting of Loops Multiple Initialisations in the for Loop The Odd Loop The break Statement The continue Statement
More informationRelational Operators and if. Class 10
Relational Operators and if Class 10 Data Type a data type consists of two things: Data Type a data type consists of two things: a set of values Data Type a data type consists of two things: a set of values
More information5. Selection: If and Switch Controls
Computer Science I CS 135 5. Selection: If and Switch Controls René Doursat Department of Computer Science & Engineering University of Nevada, Reno Fall 2005 Computer Science I CS 135 0. Course Presentation
More informationFlow of Control. Flow of control The order in which statements are executed. Transfer of control
1 Programming in C Flow of Control Flow of control The order in which statements are executed Transfer of control When the next statement executed is not the next one in sequence 2 Flow of Control Control
More informationV2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed
Programming in C 1 Flow of Control Flow of control The order in which statements are executed Transfer of control When the next statement executed is not the next one in sequence 2 Flow of Control Control
More informationSoftware Design & Programming I
Software Design & Programming I Starting Out with C++ (From Control Structures through Objects) 7th Edition Written by: Tony Gaddis Pearson - Addison Wesley ISBN: 13-978-0-132-57625-3 Chapter 4 Making
More informationLESSON 3. In this lesson you will learn about the conditional and looping constructs that allow you to control the flow of a PHP script.
LESSON 3 Flow Control In this lesson you will learn about the conditional and looping constructs that allow you to control the flow of a PHP script. In this chapter we ll look at two types of flow control:
More informationPrimitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long
Primitive Types Four integer types: byte short int (most common) long Two floating-point types: float double (most common) One character type: char One boolean type: boolean 1 2 Primitive Types, cont.
More informationWEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS
WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS OPERATORS Review: Data values can appear as literals or be stored in variables/constants Data values can be returned by method calls Operators: special symbols
More informationPIC 10A Flow control. Ernest Ryu UCLA Mathematics
PIC 10A Flow control Ernest Ryu UCLA Mathematics If statement An if statement conditionally executes a block of code. # include < iostream > using namespace std ; int main () { double d1; cin >> d1; if
More informationDecision Structures. Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz
Decision Structures Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Chapter Topics o Relational Operators o The if Statement o The if-else Statement o Nested if statements o The if-else-if
More informationSELECTION. (Chapter 2)
SELECTION (Chapter 2) Selection Very often you will want your programs to make choices among different groups of instructions For example, a program processing requests for airline tickets could have the
More informationCSE 142 Su 04 Computer Programming 1 - Java. Objects
Objects Objects have state and behavior. State is maintained in instance variables which live as long as the object does. Behavior is implemented in methods, which can be called by other objects to request
More informationControl Structures. Lecture 4 COP 3014 Fall September 18, 2017
Control Structures Lecture 4 COP 3014 Fall 2017 September 18, 2017 Control Flow Control flow refers to the specification of the order in which the individual statements, instructions or function calls
More informationSlide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto
CS 170 Java Programming 1 The Switch Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Menu-Style Code With ladder-style if-else else-if, you might sometimes find yourself writing menu-style
More informationBoolean Expressions. Is Equal and Is Not Equal
3 MAKING CHOICES Now that we ve covered how to create constants and variables, you re ready to learn how to tell your computer to make choices. This chapter is about controlling the flow of a computer
More informationLECTURE 04 MAKING DECISIONS
PowerPoint Slides adapted from *Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* Copyright 2012 Pearson Education Inc. COMPUTER PROGRAMMING LECTURE 04 MAKING DECISIONS
More informationBoolean Expressions. Is Equal and Is Not Equal
3 MAKING CHOICES ow that we ve covered how to create constants and variables, you re ready to learn how to tell your computer to make choices. This chapter is about controlling the flow of a computer program
More informationMr G s Java Jive. #11: Formatting Numbers
Mr G s Java Jive #11: Formatting Numbers Now that we ve started using double values, we re bound to run into the question of just how many decimal places we want to show. This where we get to deal with
More informationPace University. Fundamental Concepts of CS121 1
Pace University Fundamental Concepts of CS121 1 Dr. Lixin Tao http://csis.pace.edu/~lixin Computer Science Department Pace University October 12, 2005 This document complements my tutorial Introduction
More information4.1. Chapter 4: Simple Program Scheme. Simple Program Scheme. Relational Operators. So far our programs follow a simple scheme
Chapter 4: 4.1 Making Decisions Relational Operators Simple Program Scheme Simple Program Scheme So far our programs follow a simple scheme Gather input from the user Perform one or more calculations Display
More informationLecture 05 I/O statements Printf, Scanf Simple statements, Compound statements
Programming, Data Structures and Algorithms Prof. Shankar Balachandran Department of Computer Science and Engineering Indian Institute of Technology, Madras Lecture 05 I/O statements Printf, Scanf Simple
More informationRelational Operators EXAMPLE. C++ By
C++ By 9 EXAMPLE Relational Operators Sometimes you won t want every statement in your C++ program to execute every time the program runs. So far, every program in this book has executed from the top and
More informationFlow Control. CSC215 Lecture
Flow Control CSC215 Lecture Outline Blocks and compound statements Conditional statements if - statement if-else - statement switch - statement? : opertator Nested conditional statements Repetitive statements
More informationProgramming Lecture 4
Five-Minute Review 1. What are classes and objects? What is a class hierarchy? 2. What is an expression? A term? 3. What is a variable declaration? 4. What is an assignment? What is precedence? 5. What
More informationChapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Chapter 3 More Flow of Control Overview 3.1 Using Boolean Expressions 3.2 Multiway Branches 3.3 More about C++ Loop Statements 3.4 Designing Loops Slide 3-3 Flow Of Control Flow of control refers to the
More informationDecision Structures. Lesson 03 MIT 11053, Fundamentals of Programming
Decision Structures Lesson 03 MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz M.Sc. in IS (SLIIT), PGD in IS (SLIIT), BBA (Hons.) Spl. In IS (SEUSL), MIEEE, Microsoft Certified Professional
More informationIT 1033: Fundamentals of Programming Loops
IT 1033: Fundamentals of Programming Loops Budditha Hettige Department of Computer Science Repetitions: Loops A loop is a sequence of instruction s that is continually repeated until a certain condition
More informationCISC-124. Casting. // this would fail because we can t assign a double value to an int // variable
CISC-124 20180122 Today we looked at casting, conditionals and loops. Casting Casting is a simple method for converting one type of number to another, when the original type cannot be simply assigned to
More informationCS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG
CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG 1 Notice Prepare the Weekly Quiz The weekly quiz is for the knowledge we learned in the previous week (both the
More informationCS112 Lecture: Repetition Statements
CS112 Lecture: Repetition Statements Objectives: Last revised 2/18/05 1. To explain the general form of the java while loop 2. To introduce and motivate the java do.. while loop 3. To explain the general
More informationRepe$$on CSC 121 Fall 2015 Howard Rosenthal
Repe$$on CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Learn the following three repetition methods, their similarities and differences, and how to avoid common errors when using them: while do-while
More informationProgramming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029
Programming Basics and Practice GEDB029 Decision Making, Branching and Looping Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029 Decision Making and Branching C language possesses such decision-making capabilities
More informationKey Differences Between Python and Java
Python Python supports many (but not all) aspects of object-oriented programming; but it is possible to write a Python program without making any use of OO concepts. Python is designed to be used interpretively.
More informationCS125 : Introduction to Computer Science. Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals
CS125 : Introduction to Computer Science Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals c 2005, 2004, 2003, 2002, 2001, 2000 Jason Zych 1 Lecture 6 : Compound Statements, Scope,
More informationIntroduction to Bioinformatics
Introduction to Bioinformatics Variables, Data Types, Data Structures, Control Structures Janyl Jumadinova February 3, 2016 Data Type Data types are the basic unit of information storage. Instances of
More informationFollowing is the general form of a typical decision making structure found in most of the programming languages:
Decision Making Decision making structures have one or more conditions to be evaluated or tested by the program, along with a statement or statements that are to be executed if the condition is determined
More information\n is used in a string to indicate the newline character. An expression produces data. The simplest expression
Chapter 1 Summary Comments are indicated by a hash sign # (also known as the pound or number sign). Text to the right of the hash sign is ignored. (But, hash loses its special meaning if it is part of
More informationEECS 183. Week 3 - Diana Gage. www-personal.umich.edu/ ~drgage
EECS 183 Week 3 - Diana Gage www-personal.umich.edu/ ~drgage Upcoming Deadlines Lab 3 and Assignment 3 due October 2 nd (this Friday) Project 2 will be due October 6 th (a week from Friday) Get started
More informationModule 2: Choice and Iteration
Module 2: Choice and Iteration Ron K. Cytron * Department of Computer Science and Engineering * Washington University in Saint Louis Thanks to Alan Waldman for comments that improved these slides Prepared
More informationControl Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003
Control Flow COMS W1007 Introduction to Computer Science Christopher Conway 3 June 2003 Overflow from Last Time: Why Types? Assembly code is typeless. You can take any 32 bits in memory, say this is an
More informationProgramming Lecture 4
Five-Minute Review 1. What is a class hierarchy? 2. Which graphical coordinate system is used by Java (and most other languages)? 3. Why is a collage a good methapher for GObjects? 4. What is a CFG? What
More informationCS 106 Introduction to Computer Science I
CS 106 Introduction to Computer Science I 06 / 04 / 2015 Instructor: Michael Eckmann Today s Topics Questions / comments? Calling methods (noting parameter(s) and their types, as well as the return type)
More informationChapter 2, Part III Arithmetic Operators and Decision Making
Chapter 2, Part III Arithmetic Operators and Decision Making C How to Program, 8/e, GE 2016 Pearson Education, Ltd. All rights reserved. 1 2016 Pearson Education, Ltd. All rights reserved. 2 2016 Pearson
More informationCPS122 Lecture: From Python to Java
Objectives: CPS122 Lecture: From Python to Java last revised January 7, 2013 1. To introduce the notion of a compiled language 2. To introduce the notions of data type and a statically typed language 3.
More informationThe name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.
Mr G s Java Jive #2: Yo! Our First Program With this handout you ll write your first program, which we ll call Yo. Programs, Classes, and Objects, Oh My! People regularly refer to Java as a language that
More informationCOMP 202 Java in one week
COMP 202 Java in one week... Continued CONTENTS: Return to material from previous lecture At-home programming exercises Please Do Ask Questions It's perfectly normal not to understand everything Most of
More informationDECISION MAKING STATEMENTS
DECISION MAKING STATEMENTS If, else if, switch case These statements allow the execution of selective statements based on certain decision criteria. C language provides the following statements: if statement
More informationChapter 8. Statement-Level Control Structures
Chapter 8 Statement-Level Control Structures Levels of Control Flow Within expressions Among program units Among program statements Copyright 2012 Addison-Wesley. All rights reserved. 1-2 Control Structure
More informationc) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.
2.1 Introduction (No questions.) 2.2 A Simple Program: Printing a Line of Text 2.1 Which of the following must every C program have? (a) main (b) #include (c) /* (d) 2.2 Every statement in C
More informationFull file at
Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class
More informationBoolean Logic & Branching Lab Conditional Tests
I. Boolean (Logical) Operations Boolean Logic & Branching Lab Conditional Tests 1. Review of Binary logic Three basic logical operations are commonly used in binary logic: and, or, and not. Table 1 lists
More informationProgramming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.
Plan for the rest of the semester: Programming We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems. We saw earlier that computers
More informationIntroduction. C provides two styles of flow control:
Introduction C provides two styles of flow control: Branching Looping Branching is deciding what actions to take and looping is deciding how many times to take a certain action. Branching constructs: if
More informationCS112 Lecture: Making Choices
CS112 Lecture: Making Choices Objectives: Last revised 1/19/06 1. To review the Java if and if... statements 2. To introduce relational expressions and boolean operators 3. To discuss nested if statements
More informationRule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.
Chapter 9: Rules Chapter 1:Style and Program Organization Rule 1-1: Organize programs for readability, just as you would expect an author to organize a book. Rule 1-2: Divide each module up into a public
More informationMaking Decisions In Python
Making Decisions In Python In this section of notes you will learn how to have your programs choose between alternative courses of action. Decision Making Is All About Choices My next vacation? Images:
More informationChapter 3. Selections
Chapter 3 Selections 1 Outline 1. Flow of Control 2. Conditional Statements 3. The if Statement 4. The if-else Statement 5. The Conditional operator 6. The Switch Statement 7. Useful Hints 2 1. Flow of
More informationSchool of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn
CPS109 Course Notes 6 Alexander Ferworn Unrelated Facts Worth Remembering Use metaphors to understand issues and explain them to others. Look up what metaphor means. Table of Contents Contents 1 ITERATION...
More informationCPS122 Lecture: From Python to Java last revised January 4, Objectives:
Objectives: CPS122 Lecture: From Python to Java last revised January 4, 2017 1. To introduce the notion of a compiled language 2. To introduce the notions of data type and a statically typed language 3.
More informationThere are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.
Introduction In the programs that we have dealt with so far, all statements inside the main function were executed in sequence as they appeared, one after the other. This type of sequencing is adequate
More informationFlow Control: Branches and loops
Flow Control: Branches and loops In this context flow control refers to controlling the flow of the execution of your program that is, which instructions will get carried out and in what order. In the
More informationSelection Control Structure CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Selection Control Structure CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING MULTIPLE SELECTION To solve a problem that has several selection, use either of the following method: Multiple selection nested
More informationSelection Statements. Pseudocode
Selection Statements Pseudocode Natural language mixed with programming code Ex: if the radius is negative the program display a message indicating wrong input; the program compute the area and display
More informationExpressions and Data Types CSC 121 Spring 2015 Howard Rosenthal
Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types
More informationCreate your first workbook
Create your first workbook You've been asked to enter data in Excel, but you've never worked with Excel. Where do you begin? Or perhaps you have worked in Excel a time or two, but you still wonder how
More informationBRANCHING if-else statements
BRANCHING if-else statements Conditional Statements A conditional statement lets us choose which statement t t will be executed next Therefore they are sometimes called selection statements Conditional
More informationPlease answer the following questions. Do not re-code the enclosed codes if you have already completed them.
Dec. 9 Loops Please answer the following questions. Do not re-code the enclosed codes if you have already completed them. What is a loop? What are the three loops in Java? What do control structures do?
More information(Refer Slide Time 6:48)
Digital Circuits and Systems Prof. S. Srinivasan Department of Electrical Engineering Indian Institute of Technology Madras Lecture - 8 Karnaugh Map Minimization using Maxterms We have been taking about
More informationDecision Making -Branching. Class Incharge: S. Sasirekha
Decision Making -Branching Class Incharge: S. Sasirekha Branching The C language programs presented until now follows a sequential form of execution of statements. Many times it is required to alter the
More informationFlow Control. So Far: Writing simple statements that get executed one after another.
Flow Control So Far: Writing simple statements that get executed one after another. Flow Control So Far: Writing simple statements that get executed one after another. Flow control allows the programmer
More informationCS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University
CS 112 Introduction to Computing II Wayne Snyder Department Boston University Today: Java expressions and operators concluded Java Statements: Conditionals: if/then, if/then/else Loops: while, for Next
More informationControl Structures: if and while A C S L E C T U R E 4
Control Structures: if and while A C S - 1903 L E C T U R E 4 Control structures 3 constructs are essential building blocks for programs Sequences compound statement Decisions if, switch, conditional operator
More informationTable of Contents. Lesson Topics Page Lab Summary Page
Table of Contents Lesson Topics Page Lab Summary Page Lesson 1 Programming Fundamentals Review---From Java to C++ 1 14 16 Lesson 2 Algorithms, Data Types and Control Instructions in C++ 18 33 40 Lesson
More informationLecture 10: for, do, and switch
Lecture 10: for, do, and switch Jiajia Liu Recall the while Loop The while loop has the general form while ( boolean condition ) { The while loop is like a repeated if statement. It will repeat the statements
More information8. Control statements
8. Control statements A simple C++ statement is each of the individual instructions of a program, like the variable declarations and expressions seen in previous sections. They always end with a semicolon
More informationRepe$$on CSC 121 Spring 2017 Howard Rosenthal
Repe$$on CSC 121 Spring 2017 Howard Rosenthal Lesson Goals Learn the following three repetition structures in Java, their syntax, their similarities and differences, and how to avoid common errors when
More informationConditionals & Loops /
Conditionals & Loops 02-201 / 02-601 Conditionals If Statement if statements let you execute statements conditionally. true "then" part condition a > b false "else" part func max(a int, b int) int { var
More informationChapter 4 - Notes Control Structures I (Selection)
Chapter 4 - Notes Control Structures I (Selection) I. Control Structures A. Three Ways to Process a Program 1. In Sequence: Starts at the beginning and follows the statements in order 2. Selectively (by
More informationSTUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode
STUDENT OUTLINE Lesson 8: Structured Programming, Control Structures, if- Statements, Pseudocode INTRODUCTION: This lesson is the first of four covering the standard control structures of a high-level
More informationFull file at C How to Program, 6/e Multiple Choice Test Bank
2.1 Introduction 2.2 A Simple Program: Printing a Line of Text 2.1 Lines beginning with let the computer know that the rest of the line is a comment. (a) /* (b) ** (c) REM (d)
More informationCS 115 Lecture 8. Selection: the if statement. Neil Moore
CS 115 Lecture 8 Selection: the if statement Neil Moore Department of Computer Science University of Kentucky Lexington, Kentucky 40506 neil@cs.uky.edu 24 September 2015 Selection Sometime we want to execute
More informationJava provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:
JAVA OPERATORS GENERAL Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups: Arithmetic Operators Relational Operators Bitwise Operators
More informationOverview: Programming Concepts. Programming Concepts. Names, Values, And Variables
Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript Fluency with Information Technology Third Edition by Lawrence Snyder Overview: Programming Concepts Programming: Act of formulating
More informationOverview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript
Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript Fluency with Information Technology Third Edition by Lawrence Snyder Overview: Programming Concepts Programming: Act of formulating
More informationLecture 6. Statements
Lecture 6 Statements 1 Statements This chapter introduces the various forms of C++ statements for composing programs You will learn about Expressions Composed instructions Decision instructions Loop instructions
More information