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

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

CS0007: Introduction to Computer Programming
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
The if Statement The code in methods executes sequentially.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Chapter 3: Decision Structures Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis.
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
Selection in C.
© 2012 Pearson Education, Inc. All rights reserved. Chapter 3: Decision Structures Starting Out with Java: From Control Structures through Data Structures.
Lecture 3 Decision Structures. 4-2 Topics – The if Statement – The if - else Statement – The PayRoll class – Nested if Statements – The if - else - if.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Chapter 3 Decision Structures Starting Out with Java: From Control Structures through Objects.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA Prof. Dr. Paulo Brasko Ferreira Fall 2013.
Control Stuctures Module 4 Copyright © Copyright © Slide #2 Decision Structures Chapter Objectives To understand: how to write boolean expressions.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 3: Decision Structures
CHAPTER 3 Decision Structures Copyright © 2016 Pearson Education, Inc., Hoboken NJ.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC INTRO TO COMPUTING - PROGRAMMING If Statement.
Starting Out With Java 5 (Control Structures to Objects) Chapter 3 By Tony Gaddis Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
CHAPTER 3 Decision Structures Copyright © 2016 Pearson Education, Ltd.
CS0007: Introduction to Computer Programming
Introduction to Decision Structures and Boolean Variables
INTERMEDIATE PROGRAMMING USING JAVA
More on the Selection Structure
Decision Structure ISYS 350.
CS0007: Introduction to Computer Programming
Chapter 3: Decision Structures by Tony Gaddis and Godfrey Muganda
Chapter 4: Making Decisions.
Chapter 3: Decision Structures
Chapter 4: Making Decisions.
CHAPTER 4 Selection CSEG1003 Introduction to Computing
Lecture 3- Decision Structures
Relational Operators A relational operator compares two values. The values can be any built-in C++ data type, such as Character Integer Floating point.
Boolean Expressions and If
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Decision Structures and Boolean Logic
SELECTION STATEMENTS (1)
if-else-if Statements
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA
Chapter 3: Decision Structures
Topics The if Statement The if-else Statement Comparing Strings
Lesson 04 Control Structures I : Decision Making
Starting Out With Java 5 (Control Structures to Objects) Chapter 3
Chapter 3:Decision Structures
Chapter 4: Decision Structures and Boolean Logic
Selection Control Structure
Comparing Strings Strings can be compared using the == and != operators String comparisons are case sensitive Strings can be compared using >, =, and.
Chapter 5: Control Structure
Chapter 4: Control Structures I (Selection)
Topics The if Statement The if-else Statement Comparing Strings
The System.exit() Method
The if Statement Control structure: logical design that controls order in which set of statements execute Sequence structure: set of statements that execute.
Boolean Expressions to Make Comparisons
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Chapter 4: Decision Structures and Boolean Logic
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Presentation transcript:

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 Relational Operators The if Statement The if-else Statement Nested if statements The if-else-if Statement MIT 12043, By S. Sabraz Nawaz

Relational Operators == > < != Relational operators in Java are used to compare two variables. Java provides six relational operators: The result of any relational operator is “true” or “false” Equal == Greater than > Less than < Greater than or equal to >= Less than or equal to <= Unequal != MIT 12043, By S. Sabraz Nawaz

Boolean Expressions int a = 3; b = 4; a == b will evaluate to false a != b will evaluate to true a > b will evaluate to false a < b will evaluate to true a >= b will evaluate to false a <= b will evaluate to true MIT 12043, By S. Sabraz Nawaz

Sequence Structure MIT 12043, By S. Sabraz Nawaz

Decision Structure Programs often need more than one path of execution, however. 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

Decision Structure… Simple decision structure logic MIT 12043, By S. Sabraz Nawaz

Decision Structure… Three-action decision structure logic MIT 12043, By S. Sabraz Nawaz

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

The if Statement if (boolean expression is true) The if statement is used to create a decision structure, which allows a program to have more than one path of execution. The if statement causes one or more statements to execute only when a boolean expression is true. The if statement decides whether a section of executes or not. if (boolean expression is true) execute this statement; MIT 12043, By S. Sabraz Nawaz

The if Statement… if (coldOutside) { wearCoat; wearHat; wearGloves; } A block if statement may be modeled as: if (coldOutside) { wearCoat; wearHat; wearGloves; } Note the use of curly braces to block several statements together. MIT 12043, By S. Sabraz Nawaz

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

Programming Style and if Statements An if statement can span more than one line; however, it is still one statement. if (average > 95) grade = ′A′; is functionally equivalent to if(average > 95) grade = ′A′; MIT 12043, By S. Sabraz Nawaz

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

Only this statement is conditionally executed. 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

Example 01 Write a Java program that takes a person’s age as input and decides whether that person is eligible to vote If the person’s age is above 18, he or she is eligible to vote The program should display a message whether he or she is eligible to vote MIT 12043, By S. Sabraz Nawaz

Example 01… MIT 12043, By S. Sabraz Nawaz

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 If the person’s age is above 18, he or she is eligible to vote The program should display a message with the Name of the person and his or eligibility to vote MIT 12043, By S. Sabraz Nawaz

Example 02 MIT 12043, By S. Sabraz Nawaz

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

if-else Statement Flowcharts Wear a coat. Yes Is it cold outside? Wear shorts. No MIT 12043, By S. Sabraz Nawaz

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 If the person’s age is above 18, he or she is eligible to vote The program should display a message with the Name of the person and his or eligibility to vote MIT 12043, By S. Sabraz Nawaz

Example 03… MIT 12043, By S. Sabraz Nawaz

Exercise A company’s payment structure to its employees is as follows If the Basic salary exceeds 30,000/=, the Dearness Allowance 40% the BS, otherwise 50% of the BS If the BS exceeds 25,000/=, the House Rent Allowance is 20% of the BS, otherwise 3,000 of the BS 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, Net Salary, and display them all on screen MIT 12043, By S. Sabraz Nawaz

Solution MIT 12043, By S. Sabraz Nawaz

Solution… MIT 12043, By S. Sabraz Nawaz

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

Alignment and Nested if Statements if (expression) { statement; } else This if and else go together. This if and else go together. MIT 12043, By S. Sabraz Nawaz

Example 04 For example, 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: The customer’s salary must be at least Rs.30,000 The customer must have held his or her current job for at least 02 years MIT 12043, By S. Sabraz Nawaz

Example 04… MIT 12043, By S. Sabraz Nawaz

if-else-if The if-else-if statement tests a series of conditions It is often simpler to test a series of conditions with the if-else-if statement than with a set of nested if-else statements MIT 12043, By S. Sabraz Nawaz

if-else-if Syntax if (expression_1) { statement; etc. } else if (expression_2)   Insert as many else if clauses as necessary else 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. These statements are executed if none of the expressions above are true. MIT 12043, By S. Sabraz Nawaz

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

Logical Operators MIT 12043, By S. Sabraz Nawaz

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 reverses the truth of a boolean expression MIT 12043, By S. Sabraz Nawaz

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

Logical Operators… Expression Meaning x > y && a < b Is x greater than y AND is a less than b? x == y || x == z Is x equal to y OR is x equal to z? !(x > y) Is the expression x > y NOT true? MIT 12043, By S. Sabraz Nawaz

Truth table for the && operator Expression Value of the Expression true && false false false && true false && false true && true true MIT 12043, By S. Sabraz Nawaz

Truth table for the ||operator Expression Value of the Expression true || false true false || true false || false false true || true MIT 12043, By S. Sabraz Nawaz

Truth table for the ! operator Expression Value of the Expression !true false !false true MIT 12043, By S. Sabraz Nawaz

Example 06 – Improved Ex 05

Will be continue in next lecture… MIT 12043, By S. Sabraz Nawaz