SlideShare a Scribd company logo
Chapter 4: Control
Structures I
JAVA PROGRAMMING:
FROM PROBLEM ANALYSIS TO PROGRAM DESIGN,
SECOND EDITION
2
Chapter Objectives
 Learn about control structures.
 Examine relational and logical operators.
 Explore how to form and evaluate logical
(Boolean) expressions.
 Learn how to use the selection control
structures if, if…else, and switch in
a program.
3
Control Structures
 Three methods of processing a program:
 In sequence
 Branching
 Looping
 Branch: Altering the flow of program
execution by making a selection or choice.
 Loop: Altering the flow of program
execution by repeating statements.
4
Control Structures
5
Relational Operators
 Relational operator:
 Allows you to make comparisons in a program.
 Binary operator.
 Condition is represented by a logical
expression in Java.
 Logical expression: An expression that has
a value of either true or false.
6
Relational Operators
7
Relational Operators and
Primitive Data Types
 Can be used with integral and floating-point
data types.
 Can be used with the char data type.
 Unicode Collating Sequence.
8
Relational Operators and Primitive Data Types
9
Comparing Strings
 class String
 Method compareTo
 Method equals
 Given string str1 and str2





=
>>
<<
str2str10
str2str10
str2str10
reTo(str2)str1.compa
stringifintegeran
stringtoequalisstringif
stringifintegeran
10
Comparing Strings
String str1 = "Hello";
String str2 = "Hi";
String str3 = "Air";
String str4 = "Bill";
String str5 = "Bigger";
11
Comparing Strings
12
Comparing Strings
13
Comparing Strings
14
Comparing Strings
15
Short-Circuit Evaluation
 A process in which the computer evaluates
a logical expression from left to right and
stops as soon as the value of the expression
is known.
16
Selection
 One-way selection
 Two-way selection
 Compound (block of) statements
 Multiple selections (nested if)
 Conditional operator
 switch structures
17
One-Way Selection
 Syntax:
if (expression)
statement
 Expression referred to as decision maker.
 Statement referred to as action statement.
18
Example 4-11
//Determine the absolute value of an integer
import javax.swing.JOptionPane;
public class AbsoluteValue
{
public static void main(String[] args)
{
int number;
int temp;
String numString;
numString =
JOptionPane.showInputDialog
("Enter an integer:"); //Line 1
number = Integer.parseInt(numString); //Line 2
temp = number; //Line 3
One-Way Selection
19
if (number < 0) //Line 4
number = -number; //Line 5
JOptionPane.showMessageDialog(null,
"The absolute value of " + temp
+ " is " + number,
"Absolute Value",
JOptionPane.INFORMATION_MESSAGE); //Line 6
System.exit(0);
}
One-Way Selection
20
Two-Way Selection
 Syntax:
if (expression)
statement1
else
statement2
 else statement must be paired with an if.
21
Two-Way Selection
22
Two-Way Selection
Example 4-14
if (hours > 40.0)
wages = 40.0 * rate +
1.5 * rate * (hours - 40.0);
else
wages = hours * rate;
23
Example 4-15
if (hours > 40.0); //Line 1
wages = 40.0 * rate +
1.5 * rate * (hours - 40.0); //Line 2
else //Line 3
wages = hours * rate; //Line 4
Because a semicolon follows the closing parenthesis of the if
statement (Line 1), the else statement stands alone. The
semicolon at the end of the if statement (see Line 1) ends the
if statement, so the statement at Line 2 separates the else
clause from the if statement. That is, else is by itself.
Because there is no separate else statement in Java, this code
generates a syntax error.
Two-Way Selection
24
Compound (Block of) Statements
Syntax:
{
statement1
statement2
.
.
.
statementn
}
25
Compound (Block of) Statements
if (age > 18)
{
System.out.println("Eligible to vote.");
System.out.println("No longer a minor.");
}
else
{
System.out.println("Not eligible to vote.");
System.out.println("Still a minor.");
}
26
Multiple Selection: Nested if
 Syntax:
if (expression1)
statement1
else
if (expression2)
statement2
else
statement3
 Else is associated with the
most recent incomplete if.
 Multiple if statements can
be used in place of if…
else statements.
 May take longer to
evaluate.
27
Conditional (? :) Operator
 Ternary operator
 Syntax:
expression1 ? expression2 :
expression3
 If expression1 = true, then the result of the
condition is expression2.
Otherwise, the result of the condition is
expression3.
28
switch Structures
 Expression is also
known as selector.
 Expression can be an
identifier.
 Value can only be
integral.
switch (expression)
{
case value1: statements1
break;
case value2: statements2
break;
...
case valuen: statementsn
break;
default: statements
}
29
switch Structures
30
Example 4-24
switch (grade)
{
case 'A': System.out.println("The grade is A.");
break;
case 'B': System.out.println("The grade is B.");
break;
case 'C': System.out.println("The grade is C.");
break;
case 'D': System.out.println("The grade is D.");
break;
case 'F': System.out.println("The grade is F.");
break;
default: System.out.println("The grade is
invalid.");
}
switch Structures
31
Programming Example:
Cable Company Billing
 Input: Customer’s account number,
customer code, number of premium
channels to which customer subscribes,
number of basic service connections (in the
case of business customers).
 Output: Customer’s account number and the
billing amount.
32
Programming Example:
Cable Company Billing
Solution:
1. Prompt user for information.
2. Use switch statements based on customer’s
type.
3. Use an if statement nested within a switch
statement to determine the amount due by
each customer.
33
Chapter Summary
 Control structures are used to process programs.
 Logical expressions and order of precedence of
operators are used in expressions.
 Compare strings.
 If statements.
 if…else statements.
 switch structures.
 Proper syntax for using control statements.
Ad

More Related Content

What's hot (20)

Functions in python
Functions in python Functions in python
Functions in python
baabtra.com - No. 1 supplier of quality freshers
 
What is Multithreading In Python | Python Multithreading Tutorial | Edureka
What is Multithreading In Python | Python Multithreading Tutorial | EdurekaWhat is Multithreading In Python | Python Multithreading Tutorial | Edureka
What is Multithreading In Python | Python Multithreading Tutorial | Edureka
Edureka!
 
Control and conditional statements
Control and conditional statementsControl and conditional statements
Control and conditional statements
rajshreemuthiah
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
Ahmad Idrees
 
C Basics
C BasicsC Basics
C Basics
Sunil OS
 
The string class
The string classThe string class
The string class
Syed Zaid Irshad
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
VINOTH R
 
Introduction to Python - Training for Kids
Introduction to Python - Training for KidsIntroduction to Python - Training for Kids
Introduction to Python - Training for Kids
Aimee Maree Forsstrom
 
Type casting
Type castingType casting
Type casting
Ruchika Sinha
 
Python Programming Essentials - M9 - String Formatting
Python Programming Essentials - M9 - String FormattingPython Programming Essentials - M9 - String Formatting
Python Programming Essentials - M9 - String Formatting
P3 InfoTech Solutions Pvt. Ltd.
 
Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)
Paige Bailey
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in Java
Ravi_Kant_Sahu
 
Python slide.1
Python slide.1Python slide.1
Python slide.1
Aswin Krishnamoorthy
 
Loops in c programming
Loops in c programmingLoops in c programming
Loops in c programming
CHANDAN KUMAR
 
Loop(for, while, do while) condition Presentation
Loop(for, while, do while) condition PresentationLoop(for, while, do while) condition Presentation
Loop(for, while, do while) condition Presentation
Badrul Alam
 
Expression and Operartor In C Programming
Expression and Operartor In C Programming Expression and Operartor In C Programming
Expression and Operartor In C Programming
Kamal Acharya
 
Control structure C++
Control structure C++Control structure C++
Control structure C++
Anil Kumar
 
C++ decision making
C++ decision makingC++ decision making
C++ decision making
Zohaib Ahmed
 
Functions in C
Functions in CFunctions in C
Functions in C
Kamal Acharya
 
Types of loops in c language
Types of loops in c languageTypes of loops in c language
Types of loops in c language
sneha2494
 
What is Multithreading In Python | Python Multithreading Tutorial | Edureka
What is Multithreading In Python | Python Multithreading Tutorial | EdurekaWhat is Multithreading In Python | Python Multithreading Tutorial | Edureka
What is Multithreading In Python | Python Multithreading Tutorial | Edureka
Edureka!
 
Control and conditional statements
Control and conditional statementsControl and conditional statements
Control and conditional statements
rajshreemuthiah
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
Ahmad Idrees
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
VINOTH R
 
Introduction to Python - Training for Kids
Introduction to Python - Training for KidsIntroduction to Python - Training for Kids
Introduction to Python - Training for Kids
Aimee Maree Forsstrom
 
Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)
Paige Bailey
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in Java
Ravi_Kant_Sahu
 
Loops in c programming
Loops in c programmingLoops in c programming
Loops in c programming
CHANDAN KUMAR
 
Loop(for, while, do while) condition Presentation
Loop(for, while, do while) condition PresentationLoop(for, while, do while) condition Presentation
Loop(for, while, do while) condition Presentation
Badrul Alam
 
Expression and Operartor In C Programming
Expression and Operartor In C Programming Expression and Operartor In C Programming
Expression and Operartor In C Programming
Kamal Acharya
 
Control structure C++
Control structure C++Control structure C++
Control structure C++
Anil Kumar
 
C++ decision making
C++ decision makingC++ decision making
C++ decision making
Zohaib Ahmed
 
Types of loops in c language
Types of loops in c languageTypes of loops in c language
Types of loops in c language
sneha2494
 

Viewers also liked (20)

Control statements
Control statementsControl statements
Control statements
Kanwalpreet Kaur
 
Control statements in Java
Control statements  in JavaControl statements  in Java
Control statements in Java
Jin Castor
 
Control Structures
Control StructuresControl Structures
Control Structures
Ghaffar Khan
 
Flowchart Diagram Templates by Creately
Flowchart Diagram Templates by CreatelyFlowchart Diagram Templates by Creately
Flowchart Diagram Templates by Creately
Creately
 
Basic c++ programs
Basic c++ programsBasic c++ programs
Basic c++ programs
harman kaur
 
Principle of marketing
Principle of marketing Principle of marketing
Principle of marketing
Ahmad Idrees
 
Python in Computer Vision
Python in Computer VisionPython in Computer Vision
Python in Computer Vision
Brian Thorne
 
C++ ppt
C++ pptC++ ppt
C++ ppt
Aneesh Gupta
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language
Mohamed Loey
 
Effective writing, tips for Bloggers
Effective writing, tips for BloggersEffective writing, tips for Bloggers
Effective writing, tips for Bloggers
Ahmad Idrees
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
Ahmad Idrees
 
What is business
What is businessWhat is business
What is business
Ahmad Idrees
 
System outputs - Computer System
System outputs - Computer SystemSystem outputs - Computer System
System outputs - Computer System
Ahmad Idrees
 
Basic characteristics of business
Basic characteristics of businessBasic characteristics of business
Basic characteristics of business
Ahmad Idrees
 
What is computer Introduction to Computing
What is computer Introduction  to Computing What is computer Introduction  to Computing
What is computer Introduction to Computing
Ahmad Idrees
 
Strategic planning and mission statement
Strategic planning and mission statement Strategic planning and mission statement
Strategic planning and mission statement
Ahmad Idrees
 
Basic elements of java
Basic elements of java Basic elements of java
Basic elements of java
Ahmad Idrees
 
Introduction to objects and inputoutput
Introduction to objects and inputoutput Introduction to objects and inputoutput
Introduction to objects and inputoutput
Ahmad Idrees
 
Control structures ii
Control structures ii Control structures ii
Control structures ii
Ahmad Idrees
 
IBM MQ V9 Overview
IBM MQ V9 OverviewIBM MQ V9 Overview
IBM MQ V9 Overview
MarkTaylorIBM
 
Control statements in Java
Control statements  in JavaControl statements  in Java
Control statements in Java
Jin Castor
 
Control Structures
Control StructuresControl Structures
Control Structures
Ghaffar Khan
 
Flowchart Diagram Templates by Creately
Flowchart Diagram Templates by CreatelyFlowchart Diagram Templates by Creately
Flowchart Diagram Templates by Creately
Creately
 
Basic c++ programs
Basic c++ programsBasic c++ programs
Basic c++ programs
harman kaur
 
Principle of marketing
Principle of marketing Principle of marketing
Principle of marketing
Ahmad Idrees
 
Python in Computer Vision
Python in Computer VisionPython in Computer Vision
Python in Computer Vision
Brian Thorne
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language
Mohamed Loey
 
Effective writing, tips for Bloggers
Effective writing, tips for BloggersEffective writing, tips for Bloggers
Effective writing, tips for Bloggers
Ahmad Idrees
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
Ahmad Idrees
 
System outputs - Computer System
System outputs - Computer SystemSystem outputs - Computer System
System outputs - Computer System
Ahmad Idrees
 
Basic characteristics of business
Basic characteristics of businessBasic characteristics of business
Basic characteristics of business
Ahmad Idrees
 
What is computer Introduction to Computing
What is computer Introduction  to Computing What is computer Introduction  to Computing
What is computer Introduction to Computing
Ahmad Idrees
 
Strategic planning and mission statement
Strategic planning and mission statement Strategic planning and mission statement
Strategic planning and mission statement
Ahmad Idrees
 
Basic elements of java
Basic elements of java Basic elements of java
Basic elements of java
Ahmad Idrees
 
Introduction to objects and inputoutput
Introduction to objects and inputoutput Introduction to objects and inputoutput
Introduction to objects and inputoutput
Ahmad Idrees
 
Control structures ii
Control structures ii Control structures ii
Control structures ii
Ahmad Idrees
 
Ad

Similar to Control structures i (20)

Basic_C++ Notes with problema from Preethi arora and suneetha arora.pdf
Basic_C++ Notes with problema from Preethi arora and suneetha arora.pdfBasic_C++ Notes with problema from Preethi arora and suneetha arora.pdf
Basic_C++ Notes with problema from Preethi arora and suneetha arora.pdf
Computer Programmer
 
Lecture 3 Conditionals, expressions and Variables
Lecture 3   Conditionals, expressions and VariablesLecture 3   Conditionals, expressions and Variables
Lecture 3 Conditionals, expressions and Variables
Syed Afaq Shah MACS CP
 
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysUnit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
DevaKumari Vijay
 
Ch05.pdf
Ch05.pdfCh05.pdf
Ch05.pdf
ShivamChaturvedi67
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
Soran University
 
Ch04
Ch04Ch04
Ch04
Arriz San Juan
 
Java input Scanner
Java input Scanner Java input Scanner
Java input Scanner
Huda Alameen
 
Ch05-converted.pptx
Ch05-converted.pptxCh05-converted.pptx
Ch05-converted.pptx
ShivamChaturvedi67
 
03a control structures
03a   control structures03a   control structures
03a control structures
Manzoor ALam
 
Operators
OperatorsOperators
Operators
loidasacueza
 
LESSON 4 Control Flow Statements in JAVA.pptx
LESSON 4 Control Flow Statements in JAVA.pptxLESSON 4 Control Flow Statements in JAVA.pptx
LESSON 4 Control Flow Statements in JAVA.pptx
KiRe6
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
HCMUTE
 
Lewis_Cocking_AP_Decision_Making_For_Coding
Lewis_Cocking_AP_Decision_Making_For_CodingLewis_Cocking_AP_Decision_Making_For_Coding
Lewis_Cocking_AP_Decision_Making_For_Coding
GeorgeTsak
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
Saad Sheikh
 
control statements of clangauge (ii unit)
control statements of clangauge (ii unit)control statements of clangauge (ii unit)
control statements of clangauge (ii unit)
Prashant Sharma
 
PE1 Module 3.ppt
PE1 Module 3.pptPE1 Module 3.ppt
PE1 Module 3.ppt
balewayalew
 
C# language basics (Visual Studio)
C# language basics (Visual Studio) C# language basics (Visual Studio)
C# language basics (Visual Studio)
rnkhan
 
C# language basics (Visual studio)
C# language basics (Visual studio)C# language basics (Visual studio)
C# language basics (Visual studio)
rnkhan
 
slides03.ppt
slides03.pptslides03.ppt
slides03.ppt
Anjali127411
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to Java
Ashita Agrawal
 
Basic_C++ Notes with problema from Preethi arora and suneetha arora.pdf
Basic_C++ Notes with problema from Preethi arora and suneetha arora.pdfBasic_C++ Notes with problema from Preethi arora and suneetha arora.pdf
Basic_C++ Notes with problema from Preethi arora and suneetha arora.pdf
Computer Programmer
 
Lecture 3 Conditionals, expressions and Variables
Lecture 3   Conditionals, expressions and VariablesLecture 3   Conditionals, expressions and Variables
Lecture 3 Conditionals, expressions and Variables
Syed Afaq Shah MACS CP
 
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysUnit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
DevaKumari Vijay
 
Java input Scanner
Java input Scanner Java input Scanner
Java input Scanner
Huda Alameen
 
03a control structures
03a   control structures03a   control structures
03a control structures
Manzoor ALam
 
LESSON 4 Control Flow Statements in JAVA.pptx
LESSON 4 Control Flow Statements in JAVA.pptxLESSON 4 Control Flow Statements in JAVA.pptx
LESSON 4 Control Flow Statements in JAVA.pptx
KiRe6
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
HCMUTE
 
Lewis_Cocking_AP_Decision_Making_For_Coding
Lewis_Cocking_AP_Decision_Making_For_CodingLewis_Cocking_AP_Decision_Making_For_Coding
Lewis_Cocking_AP_Decision_Making_For_Coding
GeorgeTsak
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
Saad Sheikh
 
control statements of clangauge (ii unit)
control statements of clangauge (ii unit)control statements of clangauge (ii unit)
control statements of clangauge (ii unit)
Prashant Sharma
 
PE1 Module 3.ppt
PE1 Module 3.pptPE1 Module 3.ppt
PE1 Module 3.ppt
balewayalew
 
C# language basics (Visual Studio)
C# language basics (Visual Studio) C# language basics (Visual Studio)
C# language basics (Visual Studio)
rnkhan
 
C# language basics (Visual studio)
C# language basics (Visual studio)C# language basics (Visual studio)
C# language basics (Visual studio)
rnkhan
 
Ad

More from Ahmad Idrees (8)

An overview of computers and programming languages
An overview of computers and programming languages An overview of computers and programming languages
An overview of computers and programming languages
Ahmad Idrees
 
Marketing research links consumer
Marketing research links consumer Marketing research links consumer
Marketing research links consumer
Ahmad Idrees
 
Marketing mix and 4 p's
Marketing mix and 4 p's Marketing mix and 4 p's
Marketing mix and 4 p's
Ahmad Idrees
 
Managing marketing information
Managing marketing information Managing marketing information
Managing marketing information
Ahmad Idrees
 
Swot analysis Marketing Principle
Swot analysis Marketing Principle Swot analysis Marketing Principle
Swot analysis Marketing Principle
Ahmad Idrees
 
C++ programming program design including data structures
C++ programming program design including data structures C++ programming program design including data structures
C++ programming program design including data structures
Ahmad Idrees
 
Basic qualities of a good businessman
Basic qualities of a good businessmanBasic qualities of a good businessman
Basic qualities of a good businessman
Ahmad Idrees
 
Top 40 seo myths everyone should know about
Top 40 seo myths everyone should know aboutTop 40 seo myths everyone should know about
Top 40 seo myths everyone should know about
Ahmad Idrees
 
An overview of computers and programming languages
An overview of computers and programming languages An overview of computers and programming languages
An overview of computers and programming languages
Ahmad Idrees
 
Marketing research links consumer
Marketing research links consumer Marketing research links consumer
Marketing research links consumer
Ahmad Idrees
 
Marketing mix and 4 p's
Marketing mix and 4 p's Marketing mix and 4 p's
Marketing mix and 4 p's
Ahmad Idrees
 
Managing marketing information
Managing marketing information Managing marketing information
Managing marketing information
Ahmad Idrees
 
Swot analysis Marketing Principle
Swot analysis Marketing Principle Swot analysis Marketing Principle
Swot analysis Marketing Principle
Ahmad Idrees
 
C++ programming program design including data structures
C++ programming program design including data structures C++ programming program design including data structures
C++ programming program design including data structures
Ahmad Idrees
 
Basic qualities of a good businessman
Basic qualities of a good businessmanBasic qualities of a good businessman
Basic qualities of a good businessman
Ahmad Idrees
 
Top 40 seo myths everyone should know about
Top 40 seo myths everyone should know aboutTop 40 seo myths everyone should know about
Top 40 seo myths everyone should know about
Ahmad Idrees
 

Recently uploaded (20)

Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
Political History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptxPolitical History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
Operations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdfOperations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdf
Arab Academy for Science, Technology and Maritime Transport
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
P-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 finalP-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 final
bs22n2s
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdfBiophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
PKLI-Institute of Nursing and Allied Health Sciences Lahore , Pakistan.
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...
Sandeep Swamy
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
P-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 finalP-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 final
bs22n2s
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...
Sandeep Swamy
 

Control structures i

  • 1. Chapter 4: Control Structures I JAVA PROGRAMMING: FROM PROBLEM ANALYSIS TO PROGRAM DESIGN, SECOND EDITION
  • 2. 2 Chapter Objectives  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate logical (Boolean) expressions.  Learn how to use the selection control structures if, if…else, and switch in a program.
  • 3. 3 Control Structures  Three methods of processing a program:  In sequence  Branching  Looping  Branch: Altering the flow of program execution by making a selection or choice.  Loop: Altering the flow of program execution by repeating statements.
  • 5. 5 Relational Operators  Relational operator:  Allows you to make comparisons in a program.  Binary operator.  Condition is represented by a logical expression in Java.  Logical expression: An expression that has a value of either true or false.
  • 7. 7 Relational Operators and Primitive Data Types  Can be used with integral and floating-point data types.  Can be used with the char data type.  Unicode Collating Sequence.
  • 8. 8 Relational Operators and Primitive Data Types
  • 9. 9 Comparing Strings  class String  Method compareTo  Method equals  Given string str1 and str2      = >> << str2str10 str2str10 str2str10 reTo(str2)str1.compa stringifintegeran stringtoequalisstringif stringifintegeran
  • 10. 10 Comparing Strings String str1 = "Hello"; String str2 = "Hi"; String str3 = "Air"; String str4 = "Bill"; String str5 = "Bigger";
  • 15. 15 Short-Circuit Evaluation  A process in which the computer evaluates a logical expression from left to right and stops as soon as the value of the expression is known.
  • 16. 16 Selection  One-way selection  Two-way selection  Compound (block of) statements  Multiple selections (nested if)  Conditional operator  switch structures
  • 17. 17 One-Way Selection  Syntax: if (expression) statement  Expression referred to as decision maker.  Statement referred to as action statement.
  • 18. 18 Example 4-11 //Determine the absolute value of an integer import javax.swing.JOptionPane; public class AbsoluteValue { public static void main(String[] args) { int number; int temp; String numString; numString = JOptionPane.showInputDialog ("Enter an integer:"); //Line 1 number = Integer.parseInt(numString); //Line 2 temp = number; //Line 3 One-Way Selection
  • 19. 19 if (number < 0) //Line 4 number = -number; //Line 5 JOptionPane.showMessageDialog(null, "The absolute value of " + temp + " is " + number, "Absolute Value", JOptionPane.INFORMATION_MESSAGE); //Line 6 System.exit(0); } One-Way Selection
  • 20. 20 Two-Way Selection  Syntax: if (expression) statement1 else statement2  else statement must be paired with an if.
  • 22. 22 Two-Way Selection Example 4-14 if (hours > 40.0) wages = 40.0 * rate + 1.5 * rate * (hours - 40.0); else wages = hours * rate;
  • 23. 23 Example 4-15 if (hours > 40.0); //Line 1 wages = 40.0 * rate + 1.5 * rate * (hours - 40.0); //Line 2 else //Line 3 wages = hours * rate; //Line 4 Because a semicolon follows the closing parenthesis of the if statement (Line 1), the else statement stands alone. The semicolon at the end of the if statement (see Line 1) ends the if statement, so the statement at Line 2 separates the else clause from the if statement. That is, else is by itself. Because there is no separate else statement in Java, this code generates a syntax error. Two-Way Selection
  • 24. 24 Compound (Block of) Statements Syntax: { statement1 statement2 . . . statementn }
  • 25. 25 Compound (Block of) Statements if (age > 18) { System.out.println("Eligible to vote."); System.out.println("No longer a minor."); } else { System.out.println("Not eligible to vote."); System.out.println("Still a minor."); }
  • 26. 26 Multiple Selection: Nested if  Syntax: if (expression1) statement1 else if (expression2) statement2 else statement3  Else is associated with the most recent incomplete if.  Multiple if statements can be used in place of if… else statements.  May take longer to evaluate.
  • 27. 27 Conditional (? :) Operator  Ternary operator  Syntax: expression1 ? expression2 : expression3  If expression1 = true, then the result of the condition is expression2. Otherwise, the result of the condition is expression3.
  • 28. 28 switch Structures  Expression is also known as selector.  Expression can be an identifier.  Value can only be integral. switch (expression) { case value1: statements1 break; case value2: statements2 break; ... case valuen: statementsn break; default: statements }
  • 30. 30 Example 4-24 switch (grade) { case 'A': System.out.println("The grade is A."); break; case 'B': System.out.println("The grade is B."); break; case 'C': System.out.println("The grade is C."); break; case 'D': System.out.println("The grade is D."); break; case 'F': System.out.println("The grade is F."); break; default: System.out.println("The grade is invalid."); } switch Structures
  • 31. 31 Programming Example: Cable Company Billing  Input: Customer’s account number, customer code, number of premium channels to which customer subscribes, number of basic service connections (in the case of business customers).  Output: Customer’s account number and the billing amount.
  • 32. 32 Programming Example: Cable Company Billing Solution: 1. Prompt user for information. 2. Use switch statements based on customer’s type. 3. Use an if statement nested within a switch statement to determine the amount due by each customer.
  • 33. 33 Chapter Summary  Control structures are used to process programs.  Logical expressions and order of precedence of operators are used in expressions.  Compare strings.  If statements.  if…else statements.  switch structures.  Proper syntax for using control statements.