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.

More Related Content

What's hot

Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
Hossain Md Shakhawat
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
Saad Sheikh
 
Types of loops in c language
Types of loops in c languageTypes of loops in c language
Types of loops in c language
sneha2494
 
Simple if else statement,nesting of if else statement &amp; else if ladder
Simple if else statement,nesting of if else statement &amp; else if ladderSimple if else statement,nesting of if else statement &amp; else if ladder
Simple if else statement,nesting of if else statement &amp; else if ladder
Moni Adhikary
 
Control structure C++
Control structure C++Control structure C++
Control structure C++
Anil Kumar
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
Madishetty Prathibha
 
Loops c++
Loops c++Loops c++
Loops c++
Shivani Singh
 
Control statements
Control statementsControl statements
Control statements
Kanwalpreet Kaur
 
Control structures in c
Control structures in cControl structures in c
Looping Statements and Control Statements in Python
Looping Statements and Control Statements in PythonLooping Statements and Control Statements in Python
Looping Statements and Control Statements in Python
PriyankaC44
 
Java Decision Control
Java Decision ControlJava Decision Control
Java Decision Control
Jayfee Ramos
 
If-else and switch-case
If-else and switch-caseIf-else and switch-case
If-else and switch-case
Manash Kumar Mondal
 
Nested loops
Nested loopsNested loops
Nested loops
Neeru Mittal
 
Operators in java presentation
Operators in java presentationOperators in java presentation
Operators in java presentation
kunal kishore
 
C if else
C if elseC if else
C if else
Ritwik Das
 
Java if else condition - powerpoint persentation
Java if else condition - powerpoint persentationJava if else condition - powerpoint persentation
Java if else condition - powerpoint persentation
Maneesha Caldera
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in Java
Ravi_Kant_Sahu
 
virtual function
virtual functionvirtual function
virtual function
VENNILAV6
 
Function in Python
Function in PythonFunction in Python
Function in Python
Yashdev Hada
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Madishetty Prathibha
 

What's hot (20)

Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
Types of loops in c language
Types of loops in c languageTypes of loops in c language
Types of loops in c language
 
Simple if else statement,nesting of if else statement &amp; else if ladder
Simple if else statement,nesting of if else statement &amp; else if ladderSimple if else statement,nesting of if else statement &amp; else if ladder
Simple if else statement,nesting of if else statement &amp; else if ladder
 
Control structure C++
Control structure C++Control structure C++
Control structure C++
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
 
Loops c++
Loops c++Loops c++
Loops c++
 
Control statements
Control statementsControl statements
Control statements
 
Control structures in c
Control structures in cControl structures in c
Control structures in c
 
Looping Statements and Control Statements in Python
Looping Statements and Control Statements in PythonLooping Statements and Control Statements in Python
Looping Statements and Control Statements in Python
 
Java Decision Control
Java Decision ControlJava Decision Control
Java Decision Control
 
If-else and switch-case
If-else and switch-caseIf-else and switch-case
If-else and switch-case
 
Nested loops
Nested loopsNested loops
Nested loops
 
Operators in java presentation
Operators in java presentationOperators in java presentation
Operators in java presentation
 
C if else
C if elseC if else
C if else
 
Java if else condition - powerpoint persentation
Java if else condition - powerpoint persentationJava if else condition - powerpoint persentation
Java if else condition - powerpoint persentation
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in Java
 
virtual function
virtual functionvirtual function
virtual function
 
Function in Python
Function in PythonFunction in Python
Function in Python
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 

Viewers also liked

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
 
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++ 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
 

Viewers also liked (20)

Control statements in Java
Control statements  in JavaControl statements  in Java
Control statements in Java
 
Control Structures
Control StructuresControl Structures
Control Structures
 
Flowchart Diagram Templates by Creately
Flowchart Diagram Templates by CreatelyFlowchart Diagram Templates by Creately
Flowchart Diagram Templates by Creately
 
Basic c++ programs
Basic c++ programsBasic c++ programs
Basic c++ programs
 
Principle of marketing
Principle of marketing Principle of marketing
Principle of marketing
 
Python in Computer Vision
Python in Computer VisionPython in Computer Vision
Python in Computer Vision
 
C++ ppt
C++ pptC++ ppt
C++ ppt
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language
 
Effective writing, tips for Bloggers
Effective writing, tips for BloggersEffective writing, tips for Bloggers
Effective writing, tips for Bloggers
 
Basics of c++ Programming Language
Basics of c++ Programming LanguageBasics of c++ Programming Language
Basics of c++ Programming Language
 
What is business
What is businessWhat is business
What is business
 
System outputs - Computer System
System outputs - Computer SystemSystem outputs - Computer System
System outputs - Computer System
 
Basic characteristics of business
Basic characteristics of businessBasic characteristics of business
Basic characteristics of business
 
What is computer Introduction to Computing
What is computer Introduction  to Computing What is computer Introduction  to Computing
What is computer Introduction to Computing
 
Strategic planning and mission statement
Strategic planning and mission statement Strategic planning and mission statement
Strategic planning and mission statement
 
Basic elements of java
Basic elements of java Basic elements of java
Basic elements of java
 
Introduction to objects and inputoutput
Introduction to objects and inputoutput Introduction to objects and inputoutput
Introduction to objects and inputoutput
 
Control structures ii
Control structures ii Control structures ii
Control structures ii
 
IBM MQ V9 Overview
IBM MQ V9 OverviewIBM MQ V9 Overview
IBM MQ V9 Overview
 

Similar to Control structures i

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
Lecture 3
Lecture 3Lecture 3
Lecture 3
Soran University
 
Ch04
Ch04Ch04
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
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
HCMUTE
 
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
 
Chapter 2 - Flow of Control Part I.pdf
Chapter 2 -  Flow of Control Part I.pdfChapter 2 -  Flow of Control Part I.pdf
Chapter 2 - Flow of Control Part I.pdf
KirubelWondwoson1
 
Control statments in c
Control statments in cControl statments in c
Control statments in c
CGC Technical campus,Mohali
 
Programming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-ExpressionsProgramming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-Expressions
LovelitJose
 

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
 
Lecture 3 Conditionals, expressions and Variables
Lecture 3   Conditionals, expressions and VariablesLecture 3   Conditionals, expressions and Variables
Lecture 3 Conditionals, expressions and Variables
 
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
 
Ch05.pdf
Ch05.pdfCh05.pdf
Ch05.pdf
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Ch04
Ch04Ch04
Ch04
 
Java input Scanner
Java input Scanner Java input Scanner
Java input Scanner
 
Ch05-converted.pptx
Ch05-converted.pptxCh05-converted.pptx
Ch05-converted.pptx
 
03a control structures
03a   control structures03a   control structures
03a control structures
 
Operators
OperatorsOperators
Operators
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
 
control statements of clangauge (ii unit)
control statements of clangauge (ii unit)control statements of clangauge (ii unit)
control statements of clangauge (ii unit)
 
PE1 Module 3.ppt
PE1 Module 3.pptPE1 Module 3.ppt
PE1 Module 3.ppt
 
C# language basics (Visual Studio)
C# language basics (Visual Studio) C# language basics (Visual Studio)
C# language basics (Visual Studio)
 
C# language basics (Visual studio)
C# language basics (Visual studio)C# language basics (Visual studio)
C# language basics (Visual studio)
 
slides03.ppt
slides03.pptslides03.ppt
slides03.ppt
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to Java
 
Chapter 2 - Flow of Control Part I.pdf
Chapter 2 -  Flow of Control Part I.pdfChapter 2 -  Flow of Control Part I.pdf
Chapter 2 - Flow of Control Part I.pdf
 
Control statments in c
Control statments in cControl statments in c
Control statments in c
 
Programming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-ExpressionsProgramming in java - Concepts- Operators- Control statements-Expressions
Programming in java - Concepts- Operators- Control statements-Expressions
 

More from 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
 

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
 
Marketing research links consumer
Marketing research links consumer Marketing research links consumer
Marketing research links consumer
 
Marketing mix and 4 p's
Marketing mix and 4 p's Marketing mix and 4 p's
Marketing mix and 4 p's
 
Managing marketing information
Managing marketing information Managing marketing information
Managing marketing information
 
Swot analysis Marketing Principle
Swot analysis Marketing Principle Swot analysis Marketing Principle
Swot analysis Marketing Principle
 
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
 
Basic qualities of a good businessman
Basic qualities of a good businessmanBasic qualities of a good businessman
Basic qualities of a good businessman
 
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
 

Recently uploaded

Webinar Innovative assessments for SOcial Emotional Skills
Webinar Innovative assessments for SOcial Emotional SkillsWebinar Innovative assessments for SOcial Emotional Skills
Webinar Innovative assessments for SOcial Emotional Skills
EduSkills OECD
 
OS ticketing tool: Troubleshooting Guide for DIKSHA’s concern.pptx
OS ticketing tool: Troubleshooting Guide for DIKSHA’s concern.pptxOS ticketing tool: Troubleshooting Guide for DIKSHA’s concern.pptx
OS ticketing tool: Troubleshooting Guide for DIKSHA’s concern.pptx
bipin95
 
Unlocking Educational Synergy-DIKSHA & Google Classroom.pptx
Unlocking Educational Synergy-DIKSHA & Google Classroom.pptxUnlocking Educational Synergy-DIKSHA & Google Classroom.pptx
Unlocking Educational Synergy-DIKSHA & Google Classroom.pptx
bipin95
 
The basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxThe basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptx
heathfieldcps1
 
How to Add Colour Kanban Records in Odoo 17 Notebook
How to Add Colour Kanban Records in Odoo 17 NotebookHow to Add Colour Kanban Records in Odoo 17 Notebook
How to Add Colour Kanban Records in Odoo 17 Notebook
Celine George
 
Bedok NEWater Photostory - COM322 Assessment (Story 2)
Bedok NEWater Photostory - COM322 Assessment (Story 2)Bedok NEWater Photostory - COM322 Assessment (Story 2)
Bedok NEWater Photostory - COM322 Assessment (Story 2)
Liyana Rozaini
 
Year-to-Date Filter in Odoo 17 Dashboard
Year-to-Date Filter in Odoo 17 DashboardYear-to-Date Filter in Odoo 17 Dashboard
Year-to-Date Filter in Odoo 17 Dashboard
Celine George
 
Kesadaran_Berbangsa_dan_Bernegara_Nasion.pptx
Kesadaran_Berbangsa_dan_Bernegara_Nasion.pptxKesadaran_Berbangsa_dan_Bernegara_Nasion.pptx
Kesadaran_Berbangsa_dan_Bernegara_Nasion.pptx
artenzmartenkai
 
What is Rescue Session in Odoo 17 POS - Odoo 17 Slides
What is Rescue Session in Odoo 17 POS - Odoo 17 SlidesWhat is Rescue Session in Odoo 17 POS - Odoo 17 Slides
What is Rescue Session in Odoo 17 POS - Odoo 17 Slides
Celine George
 
NLC English 7 Consolidation Lesson plan for teacher
NLC English 7 Consolidation Lesson plan for teacherNLC English 7 Consolidation Lesson plan for teacher
NLC English 7 Consolidation Lesson plan for teacher
AngelicaLubrica
 
NAEYC Code of Ethical Conduct Resource Book
NAEYC Code of Ethical Conduct Resource BookNAEYC Code of Ethical Conduct Resource Book
NAEYC Code of Ethical Conduct Resource Book
lakitawilson
 
formative Evaluation By Dr.Kshirsagar R.V
formative Evaluation By Dr.Kshirsagar R.Vformative Evaluation By Dr.Kshirsagar R.V
formative Evaluation By Dr.Kshirsagar R.V
DrRavindrakshirsagar1
 
New Features in Odoo 17 Sign - Odoo 17 Slides
New Features in Odoo 17 Sign - Odoo 17 SlidesNew Features in Odoo 17 Sign - Odoo 17 Slides
New Features in Odoo 17 Sign - Odoo 17 Slides
Celine George
 
SEQUNCES Lecture_Notes_Unit4_chapter11_sequence
SEQUNCES  Lecture_Notes_Unit4_chapter11_sequenceSEQUNCES  Lecture_Notes_Unit4_chapter11_sequence
SEQUNCES Lecture_Notes_Unit4_chapter11_sequence
Murugan Solaiyappan
 
Howe Writing Center - Orientation Summer 2024
Howe Writing Center - Orientation Summer 2024Howe Writing Center - Orientation Summer 2024
Howe Writing Center - Orientation Summer 2024
Elizabeth Walsh
 
Chapter-2-Era-of-One-party-Dominance-Class-12-Political-Science-Notes-2 (1).pptx
Chapter-2-Era-of-One-party-Dominance-Class-12-Political-Science-Notes-2 (1).pptxChapter-2-Era-of-One-party-Dominance-Class-12-Political-Science-Notes-2 (1).pptx
Chapter-2-Era-of-One-party-Dominance-Class-12-Political-Science-Notes-2 (1).pptx
Brajeswar Paul
 
2024 KWL Back 2 School Summer Conference
2024 KWL Back 2 School Summer Conference2024 KWL Back 2 School Summer Conference
2024 KWL Back 2 School Summer Conference
KlettWorldLanguages
 
How to Create Sequence Numbers in Odoo 17
How to Create Sequence Numbers in Odoo 17How to Create Sequence Numbers in Odoo 17
How to Create Sequence Numbers in Odoo 17
Celine George
 
Genetics Teaching Plan: Dr.Kshirsagar R.V.
Genetics Teaching Plan: Dr.Kshirsagar R.V.Genetics Teaching Plan: Dr.Kshirsagar R.V.
Genetics Teaching Plan: Dr.Kshirsagar R.V.
DrRavindrakshirsagar1
 
matatag curriculum education for Kindergarten
matatag curriculum education for Kindergartenmatatag curriculum education for Kindergarten
matatag curriculum education for Kindergarten
SarahAlie1
 

Recently uploaded (20)

Webinar Innovative assessments for SOcial Emotional Skills
Webinar Innovative assessments for SOcial Emotional SkillsWebinar Innovative assessments for SOcial Emotional Skills
Webinar Innovative assessments for SOcial Emotional Skills
 
OS ticketing tool: Troubleshooting Guide for DIKSHA’s concern.pptx
OS ticketing tool: Troubleshooting Guide for DIKSHA’s concern.pptxOS ticketing tool: Troubleshooting Guide for DIKSHA’s concern.pptx
OS ticketing tool: Troubleshooting Guide for DIKSHA’s concern.pptx
 
Unlocking Educational Synergy-DIKSHA & Google Classroom.pptx
Unlocking Educational Synergy-DIKSHA & Google Classroom.pptxUnlocking Educational Synergy-DIKSHA & Google Classroom.pptx
Unlocking Educational Synergy-DIKSHA & Google Classroom.pptx
 
The basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxThe basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptx
 
How to Add Colour Kanban Records in Odoo 17 Notebook
How to Add Colour Kanban Records in Odoo 17 NotebookHow to Add Colour Kanban Records in Odoo 17 Notebook
How to Add Colour Kanban Records in Odoo 17 Notebook
 
Bedok NEWater Photostory - COM322 Assessment (Story 2)
Bedok NEWater Photostory - COM322 Assessment (Story 2)Bedok NEWater Photostory - COM322 Assessment (Story 2)
Bedok NEWater Photostory - COM322 Assessment (Story 2)
 
Year-to-Date Filter in Odoo 17 Dashboard
Year-to-Date Filter in Odoo 17 DashboardYear-to-Date Filter in Odoo 17 Dashboard
Year-to-Date Filter in Odoo 17 Dashboard
 
Kesadaran_Berbangsa_dan_Bernegara_Nasion.pptx
Kesadaran_Berbangsa_dan_Bernegara_Nasion.pptxKesadaran_Berbangsa_dan_Bernegara_Nasion.pptx
Kesadaran_Berbangsa_dan_Bernegara_Nasion.pptx
 
What is Rescue Session in Odoo 17 POS - Odoo 17 Slides
What is Rescue Session in Odoo 17 POS - Odoo 17 SlidesWhat is Rescue Session in Odoo 17 POS - Odoo 17 Slides
What is Rescue Session in Odoo 17 POS - Odoo 17 Slides
 
NLC English 7 Consolidation Lesson plan for teacher
NLC English 7 Consolidation Lesson plan for teacherNLC English 7 Consolidation Lesson plan for teacher
NLC English 7 Consolidation Lesson plan for teacher
 
NAEYC Code of Ethical Conduct Resource Book
NAEYC Code of Ethical Conduct Resource BookNAEYC Code of Ethical Conduct Resource Book
NAEYC Code of Ethical Conduct Resource Book
 
formative Evaluation By Dr.Kshirsagar R.V
formative Evaluation By Dr.Kshirsagar R.Vformative Evaluation By Dr.Kshirsagar R.V
formative Evaluation By Dr.Kshirsagar R.V
 
New Features in Odoo 17 Sign - Odoo 17 Slides
New Features in Odoo 17 Sign - Odoo 17 SlidesNew Features in Odoo 17 Sign - Odoo 17 Slides
New Features in Odoo 17 Sign - Odoo 17 Slides
 
SEQUNCES Lecture_Notes_Unit4_chapter11_sequence
SEQUNCES  Lecture_Notes_Unit4_chapter11_sequenceSEQUNCES  Lecture_Notes_Unit4_chapter11_sequence
SEQUNCES Lecture_Notes_Unit4_chapter11_sequence
 
Howe Writing Center - Orientation Summer 2024
Howe Writing Center - Orientation Summer 2024Howe Writing Center - Orientation Summer 2024
Howe Writing Center - Orientation Summer 2024
 
Chapter-2-Era-of-One-party-Dominance-Class-12-Political-Science-Notes-2 (1).pptx
Chapter-2-Era-of-One-party-Dominance-Class-12-Political-Science-Notes-2 (1).pptxChapter-2-Era-of-One-party-Dominance-Class-12-Political-Science-Notes-2 (1).pptx
Chapter-2-Era-of-One-party-Dominance-Class-12-Political-Science-Notes-2 (1).pptx
 
2024 KWL Back 2 School Summer Conference
2024 KWL Back 2 School Summer Conference2024 KWL Back 2 School Summer Conference
2024 KWL Back 2 School Summer Conference
 
How to Create Sequence Numbers in Odoo 17
How to Create Sequence Numbers in Odoo 17How to Create Sequence Numbers in Odoo 17
How to Create Sequence Numbers in Odoo 17
 
Genetics Teaching Plan: Dr.Kshirsagar R.V.
Genetics Teaching Plan: Dr.Kshirsagar R.V.Genetics Teaching Plan: Dr.Kshirsagar R.V.
Genetics Teaching Plan: Dr.Kshirsagar R.V.
 
matatag curriculum education for Kindergarten
matatag curriculum education for Kindergartenmatatag curriculum education for Kindergarten
matatag curriculum education for Kindergarten
 

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.