0% found this document useful (0 votes)
59 views41 pages

Control Structures I: Ava Rogramming

This document discusses various control structures in Java programming including branching, looping, relational operators, comparing strings, logical operators, selection structures, and switch statements. It provides examples to illustrate if/else statements, compound statements, nested if statements, and the conditional operator.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views41 pages

Control Structures I: Ava Rogramming

This document discusses various control structures in Java programming including branching, looping, relational operators, comparing strings, logical operators, selection structures, and switch statements. It provides examples to illustrate if/else statements, compound statements, nested if statements, and the conditional operator.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Control Structures I

Java Programming

1
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.

2
Control Structures

3
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.

4
Relational Operators

5
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.

6
Some Characters of the Unicode Character Set and their Unicode Value as
a Decimal Integer

7
8
Relational Operators and Primitive Data Types

9
Comparing Strings
 class String
 Method compareTo
 Method equals
 Given string str1 and str2
an integer  0 if string str1  str2

str1.compareTo(str2) 0 if string str1 is equal to string str2
an integer  0 if string str1  str2

10
Comparing Strings
String str1 = "Hello";
String str2 = "Hi";
String str3 = "Air";
String str4 = "Bill";
String str5 = "Bigger";

11
Comparing Strings

12
//The String method compareTo

public class StringComparison


{
public static void main(String[] args)
{
String str1 = "Hello"; //Line 1
String str2 = "Hi"; //Line 2
String str3 = "Air"; //Line 3
String str4 = "Bill"; //Line 4
String str5 = "Bigger"; //Line 5

System.out.println("Line 6: " +
"str1.compareTo(str2) evaluates to "
+ str1.compareTo(str2)); //Line 6

System.out.println("Line 7: " +
"str1.compareTo(\"Hen\") evaluates to "
+ str1.compareTo("Hen"));

13
System.out.println("Line 8: " +
"str4.compareTo(str3) evaluates to "
+ str4.compareTo(str3)); //Line 8

System.out.println("Line 9: " +
"str1.compareTo(\"hello\") evaluates to "
+ str1.compareTo("hello")); //Line 9

System.out.println("Line 10: " +


"str2.compareTo(\"Hi\") evaluates to "
+ str2.compareTo("Hi")); //Line 10

System.out.println("Line 11: " +


"str4.compareTo(\"Billy\") evaluates to "
+ str4.compareTo("Billy")); //Line 11

System.out.println("Line 12: " +


"str5.compareTo(\"Big\") evaluates to "
+ str5.compareTo("Big")); //Line 12

System.out.println("Line 13: " +


"str1.compareTo(\"Hello \") evaluates to "
+ str1.compareTo("Hello ")); //Line 13
}
} 14
Comparing Strings

15
/Logical operators System.out.println("!found && (hours >= 0)
evaluates to "
public class LogicalOperators + (!found && (hours >= 0)));
{ System.out.println("!(found && (hours >= 0))
public static void main(String[] args) evaluates to "
{ + (!(found && (hours >= 0))));
boolean found = true; System.out.println("hours + overTime <= 75.00
evaluates to "
double hours = 45.30;
+ (hours + overTime <= 75.00));
double overTime = 15.00;
System.out.println("(count >= 0) && (count <=
int count = 20; 100) "
char ch = 'B'; + "evaluates to "
+ ((count >= 0) && (count <= 100)));
System.out.printf("found = %b, hours = %.2f, System.out.println("('A' <= ch && ch <= 'Z')
overTime = " evaluates to "
+ "%.2f, count = %2d, ch = + ('A' <= ch && ch <= 'Z'));
%c%n%n",
}
found, hours, overTime, count, ch);
}

System.out.println("!found evaluates to " +


!found);
System.out.println("hours > 40.00 evaluates to "
+ (hours > 40.00));

16
Comparing Strings

17
Comparing Strings

18
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.

19
Selection
 One-way selection
 Two-way selection
 Compound (block of) statements
 Multiple selections (nested if)
 Conditional operator
 switch structures

20
One-Way Selection
 Syntax:
if (expression)
statement
 Expression referred to as decision maker.
 Statement referred to as action statement.

21
One-Way Selection
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

22
One-Way Selection
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);
}

23
Two-Way Selection
 Syntax:
if (expression)
statement1
else
statement2
 else statement must be paired with an if.

24
Two-Way Selection

25
Two-Way Selection

Example 4-14

if (hours > 40.0)


wages = 40.0 * rate +
1.5 * rate * (hours - 40.0);
else
wages = hours * rate;

26
Two-Way Selection
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.
27
Compound (Block of) Statements
Syntax:
{
statement1
statement2
.
.
.
statementn
}

29
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.");
}

30
Multiple Selection: Nested if
 Syntax:  Else is associated with the
most recent incomplete if.
if (expression1)  Multiple if statements can
statement1 be used in place of
if…else statements.
else
 May take longer to
if (expression2)
evaluate.
statement2
else
statement3

31
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.

33
switch Structures

switch (expression)
{  Expression is also
case value1: statements1
break;
known as selector.
case value2: statements2  Expression can be an
break;
... identifier.
case valuen: statementsn  Value can only be
break;
default: statements integral.
}

34
switch Structures

35
switch Structures
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.");
}

36
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.

39
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.

40
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.

41

You might also like