Decision Structures. Lesson 03 MIT 11053, Fundamentals of Programming
|
|
- Dale Cameron
- 6 years ago
- Views:
Transcription
1 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 Senior Lecturer in MIT
2 Lesson Topics o Relational Operators o The if Statement o The if-else Statement o The if-else-if Statement o Nested if statements o The switch Statement MIT 12043, By S. Sabraz Nawaz 2
3 Relational Operators How do you compare two values, such as whether a radius is greater than 0, equal to 0, or less than 0? Java provides six comparison operators (also known as relational operators), Java provides six relational operators as shown below: Equal == Greater than > Less than < Greater than or equal to >= Less than or equal to <= Unequal!= The result of any relational operator is true or false MIT 12043, By S. Sabraz Nawaz 3
4 Boolean Expressions int a = 3; b = 4; a == b will evaluate to false a!= b will evaluate to true a > b a < b will evaluate to false will evaluate to true a >= b will evaluate to false a <= b will evaluate to true MIT 12043, By S. Sabraz Nawaz 4
5 Sequence Structure MIT 12043, By S. Sabraz Nawaz 5
6 Decision Structure Programs often need more than one path of execution Many algorithms require a program to execute some statements only under certain circumstances. This can be accomplished with a decision structure In a decision structure s simplest form, a specific action is taken only when a condition exists If the condition does not exist, the action is not performed MIT 12043, By S. Sabraz Nawaz 6
7 Decision Structure Simple decision structure logic MIT 12043, By S. Sabraz Nawaz 7
8 Decision Structure Three-action decision structure logic MIT 12043, By S. Sabraz Nawaz 8
9 Selection Statements The control statement that allows alternative actions based upon conditions that are evaluated at run time are generally called selection statements One way to code a decision structure in Java is with the if statement MIT 12043, By S. Sabraz Nawaz 9
10 The if Statement The if statement is used to create a decision structure, which allows a program to have more than one path of execution o The if statement causes one or more statements to execute only when a boolean expression is true. The if statement decides whether a section executes or not if (boolean expression is true) execute this statement; MIT 12043, By S. Sabraz Nawaz 3-10
11 if Statements and Boolean Expressions if (x > y) System.out.println("X is greater than Y"); if(x == y) System.out.println("X is equal to Y"); if(x!= y) { System.out.println("X is not equal to Y"); x = y; System.out.println("However, now it is."); } MIT 12043, By S. Sabraz Nawaz 3-11
12 Programming Style and if Statements An if statement can span more than one line; however, it is still one statement. if (average > 85) grade = A ; is functionally equivalent to if(average > 95) grade = A ; MIT 12043, By S. Sabraz Nawaz 3-12
13 Block if Statements Conditionally executed statements can be grouped into a block by using curly braces {} to enclose them. If curly braces are used to group conditionally executed statements, the if statement is ended by the closing curly brace. if (expression) { statement1; statement2; } Curly brace ends the statement. MIT 12043, By S. Sabraz Nawaz 3-13
14 The if Statement A block if statement may be modeled as: Note the use of curly braces to block several statements together. if (coldoutside) { wearcoat; wearhat; weargloves; } MIT 12043, By S. Sabraz Nawaz 3-14
15 Block if Statements Remember that when the curly braces are not used, then only the next statement after the if condition will be executed conditionally if (expression) statement1; statement2; statement3; Only this statement is conditionally executed. MIT 12043, By S. Sabraz Nawaz 3-15
16 Example 01 Write a Java program that takes a person s age as input and decides whether that person is eligible to vote based on the following o If the person s age is above 18, he or she is eligible to vote o The program should display a message whether he or she is eligible to vote MIT 12043, By S. Sabraz Nawaz 16
17 Example 02 Write a Java program that takes a person s age and his or her name as inputs and decides whether that person is eligible to vote o If the person s age is above 18, he or she is eligible to vote o The program should display a message with the Name of the person and his or eligibility to vote MIT 12043, By S. Sabraz Nawaz 17
18 if-else Statements The if-else statement adds the ability to conditionally execute code when the if condition is false. if (expression) statementorblockiftrue; else statementorblockiffalse; It works the same way as the if statement except that, when the condition is false, the statement within the else clause executes MIT 12043, By S. Sabraz Nawaz 3-18
19 if-else Statement Flowcharts No Wear a T-Shirt Is it cold outside? Yes Wear a coat. MIT 12043, By S. Sabraz Nawaz 3-19
20 Example 03 Write a Java program that takes a person s age and his or her name as inputs and decides whether that person is eligible to vote or not o If the person s age is above 18, he or she is eligible to vote o The program should display a message with the Name of the person and his or eligibility to vote MIT 12043, By S. Sabraz Nawaz 20
21 If Else Exercise A company s payment structure to its employees is as follows o If the Basic salary exceeds 30,000/=, the Dearness Allowance 40% of the BS, otherwise 50% of the BS is given. o If the BS exceeds 25,000/=, the House Rent Allowance is 20% of the BS is given, otherwise 3,000/= is given. o Income Tax is calculated at the rate of 12% if the Total Salary exceeds 50,000/= Write a Java program that takes BS as input, calculate DA, HRA, IT, Total Salary, Tax Amount and Net Salary. Display the output on the screen. MIT 12043, By S. Sabraz Nawaz 21
22 Nested if Statements To test more than one condition, an if statement can be nested inside another if statement If an if statement appears inside another if statement (single or block) it is called a nested if statement The nested if is executed only if the outer if statement results in a true condition MIT 12043, By S. Sabraz Nawaz 3-22
23 Alignment and Nested if Statements This if and else go together. This if and else go together. if (expression) { if (expression) { statement; } else { statement; } } else { statement; } MIT 12043, By S. Sabraz Nawaz 3-23
24 NestedIfExample Modify the Example 03 to test the age range. If age greater than 130, display a message that age cannot be beyond 130. If age less than or equal to 130, allow the calculation MIT 12043, By S. Sabraz Nawaz 24
25 Exercise Consider a banking program that determines whether a bank customer qualifies for a special, low interest rate on a loan To qualify, two conditions must exist: 1. The customer s salary must be at least Rs.30, The customer must have held his or her current job for at least 02 years MIT 12043, By S. Sabraz Nawaz 25
26 Answer MIT 12043, By S. Sabraz Nawaz 26
27 if-else-if Statements The if-else-if statement tests a series of conditions It is often simpler to test a series of conditions with the if-elseif statement than with a set of nested if-else statements MIT 12043, By S. Sabraz Nawaz 27
28 if-else-if Syntax if (expression_1) { statement; statement; etc. } else if (expression_2) { } statement; statement; etc. Insert as many else if clauses as necessary If expression_1 is true these statements are executed, and the rest of the structure is ignored. Otherwise, if expression_2 is true these statements are executed, and the rest of the structure is ignored. else { statement; statement; etc. } These statements are executed if none of the expressions above are true. MIT 12043, By S. Sabraz Nawaz 3-28
29 Example 05 Your lecturer has asked you to write a program that will allow a student to enter a test score and then display the grade for that score MIT 12043, By S. Sabraz Nawaz 29
30 MIT 12043, By S. Sabraz Nawaz 30
31 Logical Operators MIT 12043, By S. Sabraz Nawaz 31
32 Logical Operators Logical operators connect two or more relational expressions into one or reverse the logic of an expression. Java provides two binary logical operators, && and used to combine two boolean expressions into a single expression It also provides the unary! Operator o reverses the truth of a boolean expression MIT 12043, By S. Sabraz Nawaz 32
33 Logical Operators Operator Meaning Effect && AND Connects two boolean expressions into one. Both expression must be true for the overall expression to be true OR Connects two boolean expressions into one. One or both expressions must be true for the overall expression to be true. It is only necessary for one to be true, and it does not matter which one.! NOT Reverses the truth of a boolean expression. If it is applied to an expression that is true, the operator returns false. If it is applied to an expression that is false, the operator returns true MIT 12043, By S. Sabraz Nawaz 33
34 Logical Operators Expression x > y && a < b Meaning x greater than y AND is a less than b x == y x == z x equal to y OR is x equal to z!(x > y) the expression x > y NOT true MIT 12043, By S. Sabraz Nawaz 34
35 Truth tables Expression true && false false && true false && false true && true Expression true false false true false false true true Expression!true!false Value of the Expression false true Value of the Expression false false false true Value of the Expression true true false true MIT 12043, By S. Sabraz Nawaz 35
36 Example 06 Improved Ex 05 MIT 12043, By S. Sabraz Nawaz 36
37 Comparing String Objects You cannot use relational operators to compare String objects To compare, you should use the String class s equals method. The general form of the method is as follows: StringReference1.equals(StringReference2) o StringReference1 is a variable that references a String object, and StringReference2 is another variable that references a String object The method returns true if the two strings are equal, or false if they are not equal. Here is an example: if (name1.equals(name2)) MIT 12043, By S. Sabraz Nawaz 37
38 Example 07 Note: the equal method is case sensitive; if you want to ignore the case case, use string. equalsignorecase() method MIT 12043, By S. Sabraz Nawaz 38
39 Classroom Exercise A car rental company has three types of cars; Premio, Alien, and Civic. The rental scheme is as follows. Type Premio Alien Civic Fixed Rent per day 1,500/- 2,000/- 2,500/- Rate per KM 20/- 30/- 40/- Write a Java program to input Vehicle Type, Number of Days Rented, and Kilometers run to calculate the Total Rent Payable and display the output on the screen. MIT 12043, By S. Sabraz Nawaz 39
40 The switch Statement For decisions involving many possible paths and values we use the switch statement The switch statement can have a number of possible execution paths At the end of each sequence we insert a break statement that means go to the end of the switch A switch works with the byte, short, char, and int primitive data types. It works with the String class as well. 41 MIT 12043, By S. Sabraz Nawaz
41 The switch Statement switch case default break Reserved words 42 MIT 12043, By S. Sabraz Nawaz switch (variable) { case value1: statement; statement; break; } case value2: statement; statement; break; default: statement; break; Don t forget breaks!
42 Rules apply to a switch statement You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon The value for a case must be the same data type as the variable in the switch, and it must be a constant or a literal (character) When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached 43 MIT 12043, By S. Sabraz Nawaz
43 Rules apply to a switch statement When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case 44 MIT 12043, By S. Sabraz Nawaz
44 switch - Example The same case can have two or more labels. For example: switch (num) { case 1: case 2:System.out.println ( Stand up and sing a song"); break; case 3:... } 45 MIT 12043, By S. Sabraz Nawaz
45 Month Finder MIT 12043, By S. Sabraz Nawaz 46
46 Switch with Multiple case labels MIT 12043, By S. Sabraz Nawaz 47
47 End MIT 12043, By S. Sabraz Nawaz 48
Decision 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 informationChapter 3: Decision Structures
Chapter 3: Decision Structures Chapter Topics Chapter 3 discusses the following main topics: The if Statement The if-else Statement Nested if statements The if-else-if Statement Logical Operators Comparing
More informationChapter 3: Decision Structures
Chapter 3: Decision Structures Starting Out with Java: From Control Structures through Objects Fourth Edition by Tony Gaddis Addison Wesley is an imprint of 2010 Pearson Addison-Wesley. All rights reserved.
More informationChapter 3: Decision Structures
Chapter 3: Decision Structures Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter Topics Chapter 3 discusses the following main topics: The if Statement
More informationLesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL
Lesson 06 Arrays MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL Array An array is a group of variables (called elements or components) containing
More informationLesson 04. Control Structures I : Decision Making. MIT 31043, VISUAL PROGRAMMING By: S. Sabraz Nawaz
Lesson 04 Control Structures I : Decision Making MIT 31043, VISUAL PROGRAMMING Senior Lecturer in MIT Department of MIT Faculty of Management and Commerce South Eastern University of Sri Lanka Decision
More informationLesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL
Lesson 02 Data Types and Statements MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL Topics Covered Statements Variables Data Types Arithmetic
More informationLoops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz
Loops and Files Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Chapter Topics o The Increment and Decrement Operators o The while Loop o Shorthand Assignment Operators o The do-while
More informationLesson 01 Introduction
Lesson 01 Introduction MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Lecturer in Management & IT M.Sc. In IS (SLIIT), PGD in IS (SLIIT), BBA (Hons.) Spl. in IS (SEUSL), MCP Programs Computer
More informationTopics. Chapter 5. Equality Operators
Topics Chapter 5 Flow of Control Part 1: Selection Forming Conditions if/ Statements Comparing Floating-Point Numbers Comparing Objects The equals Method String Comparison Methods The Conditional Operator
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 informationLesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL
Lesson 02 Data Types and Statements MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL Topics Covered Statements Variables Constants Data Types
More informationAdministration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP
Administration Conditional Statements CS 99 Summer 2000 Michael Clarkson Lecture 4 Lab 2 due now on floppy Lab 3 due tomorrow via FTP need Instruct account password Lab 4 posted this afternoon Prelim 1
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 informationControl Structures in Java if-else and switch
Control Structures in Java if-else and switch Lecture 4 CGS 3416 Spring 2017 January 23, 2017 Lecture 4CGS 3416 Spring 2017 Selection January 23, 2017 1 / 26 Control Flow Control flow refers to the specification
More informationControl Structure: Selection
Control Structure: Selection Knowledge: Understand various concepts of selection control structure Skill: Be able to develop a program containing selection control structure Selection Structure Single
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 informationMotivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018
Motivating Examples (1.1) Selections EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG 1 import java.util.scanner; 2 public class ComputeArea { 3 public static void main(string[] args)
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 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 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 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 informationDECISION STRUCTURES: USING IF STATEMENTS IN JAVA
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
More informationLecture 5 Tao Wang 1
Lecture 5 Tao Wang 1 Objectives In this chapter, you will learn about: Selection criteria Relational operators Logical operators The if-else statement Nested if statements C++ for Engineers and Scientists,
More informationLogical Operators and switch
Lecture 5 Relational and Equivalence Operators SYS-1S22 / MTH-1A66 Logical Operators and switch Stuart Gibson sg@sys.uea.ac.uk S01.09A 1 Relational Operator Meaning < Less than > Greater than
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 informationChapter 3 Selection Statements
Chapter 3 Selection Statements 3.1 Introduction Java provides selection statements that let you choose actions with two or more alternative courses. Selection statements use conditions. Conditions are
More informationRecognize the correct ordering of decisions in multiple branches Program simple and complex decision
Lesson Outcomes At the end of this chapter, student should be able to: Use the relational operator (>, >=,
More informationSelec%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 informationC++ Programming: From Problem Analysis to Program Design, Third Edition
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection) Control Structures A computer can proceed: In sequence Selectively (branch) - making
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 informationLesson 7 Part 2 Flags
Lesson 7 Part 2 Flags A Flag is a boolean variable that signals when some condition exists in a program. When a flag is set to true, it means some condition exists When a flag is set to false, it means
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 informationSelections. CSE 114, Computer Science 1 Stony Brook University
Selections CSE 114, Computer Science 1 Stony Brook University http://www.cs.stonybrook.edu/~cse114 1 Motivation If you assigned a negative value for radius in ComputeArea.java, then you don't want the
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 informationModule 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1
Module 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1 Motivation In the programs we have written thus far, statements are executed one after the other, in the order in which they appear. Programs often
More informationSelections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG
Selections EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG Learning Outcomes The Boolean Data Type if Statement Compound vs. Primitive Statement Common Errors
More informationUniversity of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Conditionals II Lecture 11, Thu Feb 9 2006 based on slides by Kurt Eiselt http://www.cs.ubc.ca/~tmm/courses/cpsc111-06-spr
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 informationHandout 4 Conditionals. Boolean Expressions.
Handout 4 cs180 - Programming Fundamentals Fall 17 Page 1 of 8 Handout 4 Conditionals. Boolean Expressions. Example Problem. Write a program that will calculate and print bills for the city power company.
More informationBasic computer skills such as using Windows, Internet Explorer, and Microsoft Word. and Java. Chapter 2 Primitive Data Types and Operations
Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word Chapter p 1 Introduction to Computers, p, Programs, g, and Java Chapter 2 Primitive Data Types and Operations Chapter
More informationIntroduction to Object-Oriented Programming
Introduction to Object-Oriented Programming Conditional Execution Christopher Simpkins chris.simpkins@gatech.edu CS 1331 (Georgia Tech) Conditional Execution 1 / 14 Structured Programming In reasoning
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 informationAccelerating Information Technology Innovation
Accelerating Information Technology Innovation http://aiti.mit.edu Cali, Colombia Summer 2012 Lección 03 Control Structures Agenda 1. Block Statements 2. Decision Statements 3. Loops 2 What are Control
More informationswitch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++
Comp Sci 1570 Introduction to C++ Outline 1 Outline 1 Outline 1 switch ( e x p r e s s i o n ) { case c o n s t a n t 1 : group of statements 1; break ; case c o n s t a n t 2 : group of statements 2;
More informationIn this lab, you will learn more about selection statements. You will get familiar to
Objective: In this lab, you will learn more about selection statements. You will get familiar to nested if and switch statements. Nested if Statements: When you use if or if...else statement, you can write
More informationCCHAPTER SELECTION STATEMENTS HAPTER 3. Objectives
LIANMC03v3_0132221586.QXD 5/15/06 7:41 PM Page 67 CCHAPTER HAPTER 3 1 SELECTION STATEMENTS Objectives To declare boolean type and write Boolean expressions ( 3.2). To distinguish between conditional and
More informationCSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York
CSc 10200! Introduction to Computing Lecture 2-3 Edgardo Molina Fall 2013 City College of New York 1 C++ for Engineers and Scientists Third Edition Chapter 2 Problem Solving Using C++ 2 Objectives In this
More informationMotivations. Chapter 3: Selections and Conditionals. Relational Operators 8/31/18. Objectives. Problem: A Simple Math Learning Tool
Chapter 3: Selections and Conditionals CS1: Java Programming Colorado State University Motivations If you assigned a negative value for radius in Listing 2.2, ComputeAreaWithConsoleInput.java, the program
More informationChapter Goals. 3.1 The if Statement. Contents 1/30/2013 DECISIONS
CHAPTER DECISIONS 3 Chapter Goals To implement decisions using the if statement To compare integers, floating-point numbers, and Strings To write statements using the Boolean data type To develop strategies
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 informationLecture 5: Making Decisions
Lecture 5: Making Decisions Topics 5.1 Relational Operators 5.2 The if Statement 5.3 The if/else Statement 5.4 Logical Operators 5.5 Examples 4-2 3 5.1 Relational Operators Used to compare numbers to determine
More informationIntroduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017
Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017 Lecture 03: Control Flow Statements: Selection Instructor: AbuKhleif, Mohammad Noor Sep 2017 Instructor AbuKhleif, Mohammad Noor
More informationLesson 05 Methods. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL
Lesson 05 Methods MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL Why Write Methods? Methods are commonly used to break a problem down into
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 informationPROGRAMMING FUNDAMENTALS
PROGRAMMING FUNDAMENTALS Q1. Name any two Object Oriented Programming languages? Q2. Why is java called a platform independent language? Q3. Elaborate the java Compilation process. Q4. Why do we write
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 informationprintf( Please enter another number: ); scanf( %d, &num2);
CIT 593 Intro to Computer Systems Lecture #13 (11/1/12) Now that we've looked at how an assembly language program runs on a computer, we're ready to move up a level and start working with more powerful
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 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 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 informationLecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4
Assignments Lecture 6 Complete for Project 1 Reading: 3.1, 3.2, 3.3, 3.4 Summary Program Parts Summary - Variables Class Header (class name matches the file name prefix) Class Body Because this is a program,
More informationCHAPTER 5 FLOW OF CONTROL
CHAPTER 5 FLOW OF CONTROL PROGRAMMING CONSTRUCTS - In a program, statements may be executed sequentially, selectively or iteratively. - Every programming language provides constructs to support sequence,
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 informationMIT Database Management Systems Lesson 03: Entity Relationship Diagrams
MIT 22033 Database Management Systems Lesson 03: Entity Relationship Diagrams By S. Sabraz Nawaz Senior Lecturer in MIT, FMC, SEUSL & A.J.M.Hasmy FMC, SEUSL ER - Model The entity-relationship (ER) data
More informationif Statement Numeric Rela5onal Operators The if Statement Flow of Control: Branching (Savitch, Chapter 3)
if Statement Flow of Control: Branching (Savitch, Chapter 3) TOPICS Conditional Execution if and Statement Boolean Data switch Statement Ensures that a statement is executed only when some condi5on is
More informationSTUDENT LESSON A12 Iterations
STUDENT LESSON A12 Iterations Java Curriculum for AP Computer Science, Student Lesson A12 1 STUDENT LESSON A12 Iterations INTRODUCTION: Solving problems on a computer very often requires a repetition of
More informationif Statement Numeric Rela7onal Operators The if Statement Flow of Control: Branching (Savitch, Chapter 3)
if Statement Flow of Control: Branching (Savitch, Chapter 3) TOPICS Conditional Execution if,, and if boolean data switch statements CS 160, Fall Semester 2015 1 Programs o-en contain statements that may
More informationLesson 39: Conditionals #3 (W11D4)
Lesson 39: Conditionals #3 (W11D4) Balboa High School Michael Ferraro October 29, 2015 1 / 29 Do Now In order to qualify for a $50k loan, the following conditions must be met: Your annual income must be
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 informationAlgorithms and Conditionals
Algorithms and Conditionals CSC 1051 Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/
More informationChapter 4: Control Structures I
Chapter 4: Control Structures I Java Programming: From Problem Analysis to Program Design, Second Edition Chapter Objectives Learn about control structures. Examine relational and logical operators. Explore
More informationECE 122. Engineering Problem Solving with Java
ECE 122 Engineering Problem Solving with Java Lecture 8 More Conditional Statements Outline Problem: How do I make choices in my Java program? Understanding conditional statements Remember: Boolean logic
More informationECE 122. Engineering Problem Solving with Java
ECE 122 Engineering Problem Solving with Java Lecture 8 More Conditional Statements Outline Problem: How do I make choices in my Java program? Understanding conditional statements Remember: Boolean logic
More informationDecisions. Arizona State University 1
Decisions CSE100 Principles of Programming with C++, Fall 2018 (based off Chapter 4 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State University
More informationconditional statements
L E S S O N S E T 4 Conditional Statements PU RPOSE PROCE DU RE 1. To work with relational operators 2. To work with conditional statements 3. To learn and use nested if statements 4. To learn and use
More informationConditionals and Loops
Conditionals and Loops Conditionals and Loops Now we will examine programming statements that allow us to: make decisions repeat processing steps in a loop Chapter 5 focuses on: boolean expressions conditional
More informationPractice with variables and types
Practice with variables and types 1. Types. For each literal or expression, state its type (String, int, double, or boolean). Expression Type Expression Type 387 "pancakes" true 45.0 "14" 87.98515 "false"
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 informationCIS 110: Introduction to Computer Programming
CIS 110: Introduction to Computer Programming Lecture 3 Express Yourself ( 2.1) 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 1 Outline 1. Data representation and types 2. Expressions 9/16/2011
More informationCONDITIONAL EXECUTION
CONDITIONAL EXECUTION yes x > y? no max = x; max = y; logical AND logical OR logical NOT &&! Fundamentals of Computer Science I Outline Conditional Execution if then if then Nested if then statements Comparisons
More information1. Wap in JAVA to accept two values and calculate highest value and lowest value.
Application based on if..else and nested if else structure : 1. Wap in JAVA to accept two values and calculate highest value and lowest value. 2. Wap in JAVA language to accept a number, check and display
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 informationObjectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators
Objectives Chapter 4: Control Structures I (Selection) In this chapter, you will: Learn about control structures Examine relational and logical operators Explore how to form and evaluate logical (Boolean)
More information8. Decision-Making Statements. Java. Summer 2008 Instructor: Dr. Masoud Yaghini
8. Decision-Making Statements Java Summer 2008 Instructor: Dr. Masoud Yaghini Decision-Making Statements Outline Introduction The if-then Statement The if-then-else Statement The switch Statement References
More informationMobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02
Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02 Hello, in this lecture we will learn about some fundamentals concepts of java.
More informationLanguage Reference Manual
ALACS Language Reference Manual Manager: Gabriel Lopez (gal2129) Language Guru: Gabriel Kramer-Garcia (glk2110) System Architect: Candace Johnson (crj2121) Tester: Terence Jacobs (tj2316) Table of Contents
More informationCOMP 111. Introduction to Computer Science and Object-Oriented Programming
COMP 111 Introduction to Computer Science and Object-Oriented Programming 5 Decisions, Decisions! Methods - So Far Execute a sequence of statements Declarations Assignment statements Return Maybe an output
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 informationComputer Science & Engineering 150A Problem Solving Using Computers
Computer Science & Engineering 150A Problem Solving Using Computers Lecture 04 - Conditionals Stephen Scott (Adapted from Christopher M. Bourke) Fall 2009 1 / 1 cbourke@cse.unl.edu Control Structure Conditions
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 informationProgramming with Java
Programming with Java String & Making Decision Lecture 05 First stage Software Engineering Dep. Saman M. Omer 2017-2018 Objectives By the end of this lecture you should be able to : Understand another
More informationDecision Making and Branching
INTRODUCTION Decision Making and Branching Unit 4 In the previous lessons we have learned about the programming structure, data types, declaration of variables, tokens, constants, keywords and operators
More informationBuilding Java Programs
Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading: 2.1-2.2 1 2 Data and expressions reading: 2.1 3 The computer s view Internally, computers store everything as 1 s and 0
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 informationUEE1302(1102) F10: Introduction to Computers and Programming
Computational Intelligence on Automation Lab @ NCTU UEE1302(1102) F10: Introduction to Computers and Programming Programming Lecture 02 Flow of Control (I): Boolean Expression and Selection Learning Objectives
More informationCOMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015
COMP-202: Foundations of Programming Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015 Announcements Slides will be posted before the class. There might be few
More informationChapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.
Chapter 4: Control Structures I (Selection) In this chapter, you will: Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate logical (Boolean)
More informationIn Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.
CS101, Mock Boolean Conditions, If-Then Boolean Expressions and Conditions The physical order of a program is the order in which the statements are listed. The logical order of a program is the order in
More informationImportant Java terminology
1 Important Java terminology The information we manage in a Java program is either represented as primitive data or as objects. Primitive data פרימיטיביים) (נתונים include common, fundamental values as
More information