information technology studies computer programming 1
TRANSCRIPT
• Learn about decision making
• Make decisions with the if and if...elsestructures
• Use multiple statements in an if or if...else structure
• Nest if and if...else statements
• Use AND and OR operators
4NLAC Department of
Information Techonology Studies
Objectives
October 13, 2021
• Learn to make accurate and efficient decisions
• Use the switch statement
• Use the conditional and NOT operators
• Understand precedence
5NLAC Department of
Information Techonology Studies
Objectives (continued)
October 13, 2021
• Pseudocode– Use paper and pencil
– Plan program’s logic • By writing plain English statements
– Accomplish important steps in a given task
– Use everyday language
• Flowchart– Steps in diagram form
– Series of shapes connected by arrows
6NLAC Department of
Information TechonologyStudies
Understanding Decision Making
October 13, 2021
• Flowchart (continued)
– Programmers use variety of shapes to represent different tasks
• Rectangle to represent any unconditional step
• Diamond to represent any decision
• Sequence structure
– One step follows another unconditionally
– Cannot branch away or skip a step
7NLAC Department of
Information Techonology Studies
Understanding Decision Making (continued)
October 13, 2021
• Decision structure
– Involves choosing between alternative courses of action
– Based on some value within program
• All computer decisions are yes-or-no decisions
• Boolean values
– Values true and false
– Used in every computer decision
8NLAC Department of
Information Techonology Studies
Understanding Decision Making (continued)
October 13, 2021
• if statement
– Simplest statement to make decision
– Boolean expression appears within parentheses
– Space between keyword if and opening parentheses
– Execution always continues to next independent statement
– Use double equal sign (==) to determine equivalency
9NLAC Department of
Information Techonology Studies
Making Decisions with theif and if...else Structures
October 13, 2021
if (booleanExpression)
single statement;
if Statement
Evaluates to true or false
Must be enclosed in
parentheses.
Remember to indent the subordinate statement
October 13, 2021NLAC Department of Information
Techonology Studies11
Syntax:
if flowchart
Boolean
test
Subordinate
statement(s)
Independent
statement(s)
true
false
October 13, 2021NLAC Department of Information
Techonology Studies12
Example:
October 13, 2021NLAC Department of Information
Techonology Studies13
if (x == 0)
System.out.println(“The number is zero”);
System.out.println(“Program Terminated”);
Example:int m = 5;
if (m >= 0)
System.out.println(m);
System.out.println(m+1);
Statement
executes
The booleantest returns
TRUE,
Exits
outside the
if and
execute the
independent
statement
5m
Output:
October 13, 2021NLAC Department of Information
Techonology Studies14
Example:int m = 5;
if (m >= 10)
System.out.println(m);
System.out.println(m+1);
Ignores if statement
The booleantest returns
FALSE,
Exits
outside the
if and
execute the
independent
statement
5m
Output:
October 13, 2021NLAC Department of Information
Techonology Studies15
start
i >= 5
i = 5
Print (i+1)
end
yes
no
Fig. 1 Program Flowchart October 13, 2021
NLAC Department of Information Techonology Studies
16
print i
Pitfall: Misplacing a Semicolon in an if Statement
• No semicolon at end of first line of if statement
– if (m == 10)
– Statement does not end there
• When semicolon follows if directly
– Empty statement contains only semicolon
– Execution continues with the next independent statement NLAC Department of Information
Techonology Studies17
int m = 5;
if (m >= 0)
System.out.println(m);
System.out.println(m+1);
October 13, 2021
Pitfall: Misplacing a Semicolon in an if Statement
• No semicolon at end of first line of if statement
– if (m == 10)
– Statement does not end there
• When semicolon follows if directly
– Empty statement
– Execution continues with the next independent statement
NLAC Department of Information
Techonology Studies18
int m = 5;
if (m >= 0);
System.out.println(m);
System.out.println(m+1);
Do not do this
Indention has no effect
October 13, 2021
• Attempt to determine equivalency– Using single equal sign
– Rather than double equal sign
– Illegal
• Can store Boolean expression’s value in Boolean variable– Before using in if
statement
19
NLAC Department of
Information Techonology
Studies
Pitfall: Using the Assignment Operator Instead of the Equivalency Operator
int m = 5;
if (m = 0)
System.out.println(m);
System.out.println(m+1);
Use equivalency symbol ==
October 13, 2021
• Problem#1: Create a java program that allows the user to enter a name, the birth year and the current year. Compute and display the age. If the age is lower than 18, display the message, “Your are not yet allowed to vote!”; otherwise, the program terminates.
• Formula:
age = current year - birth year
October 13, 2021NLAC Department of Information
Techonology Studies21
• Pseudocode:
1. enter the name, the birth year and current year
2. Compute the age.
3. If age is lower than 18
• Display message, “Your are not allowed to vote!”
4. Otherwise,
• Program terminatesOctober 13, 2021
NLAC Department of Information Techonology Studies
22
start
Name,Birth year,
Current year
age < 18
age = current year – birth year
You are not allowed to vote
end
yes
noFig. 1 Program Flowchart
October 13, 2021NLAC Department of Information
Techonology Studies23
Activity
• Problem#2: Create a java program that allows the user to enter the customer a name, the bill amount of the customer . A 5% discount rate is given to the customer if the amount bill is 1000 and above; otherwise, the program terminates. Compute and display the discount and the amount due of the customer.
• Formula:
Discount = bill amount * 0.05
amount due = bill amount – discount
October 13, 2021NLAC Department of Information
Techonology Studies24
More Activities on if structure
• Write a java program that allows the user to enter the customer name, age, and bill.
• If the age is 60 and above, a 20% discount is deducted from the bill of the senior citizen.
October 13, 2021NLAC Department of Information
Techonology Studies26
More Activities on if structure
• Write a java program that allows the user to enter the customer name, age, and bill.
• If the age is 60 and above, a 20% discount is deducted from the bill of the senior citizen.
October 13, 2021NLAC Department of Information
Techonology Studies27
The if...else Statement
if (booleanExpression)
{
statement(s);
}
else
{
statement(s);
}
Note: blocks with braces are
necessary when there are
multiple subordinate
statements. They are optional
when there is a single
subordinate statement.
October 13, 2021NLAC Department of Information
Techonology Studies29
Syntax:
if-- else flowchart
Boolean
test
statement(s)
subordinate to if
Independent
statement(s)
true
false
statement(s)
subordinate to else
October 13, 2021NLAC Department of Information
Techonology Studies30
• if...else
statement
– Performs one action when Boolean expression evaluates true
– Performs different action when Boolean expression evaluates false
31NLAC Department of
Information Techonology Studies
if...else Structure (cont)
if
(booleanExpression)
{
statement(s);
}
else
{
statement(s);
}
October 13, 2021
• if...else
statement (continued)
– Statement that executes when if is true or falseends with semicolon
– Vertically align keyword if with the keyword else
32NLAC Department of
Information Techonology Studies
The if...else Structure (continued)
if (booleanExpression)
{
statement(s);
}
else
{
statement(s);
}
October 13, 2021
• if...else
statement (continued)– Illegal to code else without if
– Depending on evaluation of Boolean expression following if• Only one resulting
action takes place
33NLAC Department of
Information Techonology Studies
The if...else Structure (continued)
if (booleanExpression);
{
statement(s);
}
else
{
statement(s);
}
October 13, 2021
• Execute more than one statement
– Use pair of curly braces
• Place dependent statements within block
• Crucial to place curly braces correctly
34
NLAC Department of
Information Techonology
Studies
Using Multiple Statementsin an if or if...else Structure
October 13, 2021
if (booleanExpression);
{
statement(s);
}
else
{
statement(s);
}
• Any variable declared within block local to that block
35
NLAC Department of
Information Techonology
Studies
Using Multiple Statementsin an if or if...else Structure
October 13, 2021
if (booleanExpression);
{
int var1;
...;
}
else
{
statement(s);
}
• Problem#1: Create a java program that allows the user to enter the customer a name, the bill amount of the customer and customer status code( 1- regular customer, and 2- walk in customer). Only regular customer receives a discount of 10%. Compute and display the discount and the amount due of the customer.
• Formula: amount due = bill amount - discount
October 13, 2021NLAC Department of Information
Techonology Studies37
• Pseudocode:
1. enter the customer name, the bill amount of the customer and customer status code
2. If the customer status code is 1
a. Compute for the discount
3. Otherwise,
a. No discount
4. Compute the amount due.
5. Display the discount, and the amount due.
October 13, 2021NLAC Department of Information
Techonology Studies38
start
Customer nameBill amount
Customer status code
Status code is 1?
Discount = 0
Discount = bill amount * .10Discount
Amount due
Amount due = bill amount - discount
end
yes
no
Fig. 1 Program Flowchart October 13, 2021
NLAC Department of Information Techonology Studies
39
Variable declaration & intitialization
Accepts input from the user
If boolean test returns TRUE, execute
Goes out from the if, then execute independent statement
If booleantest returns FALSE, execute
Group Activity
• Problem#1: Create a java program that allows the user to enter the student name, and the average grade. Determine whether the grade is PASSED or FAILED.
• Write the pseudocode and draw the flowchart of the given problem.
October 13, 2021NLAC Department of Information
Techonology Studies41
Group Activity • Problem#2: Create a program that will allow the user to enter the
employee name, rate per hour and hours worked. Compute for the overtime hours and overtime pay, and the regular pay if hours worked is more than 40 hours; otherwise computer for the regular pay, overtime hours and overtime pay is zero. Compute and display the regular pay, tithe, gross pay, and net pay.
• Formula:
• OT Hours = hrs worked – 40
• OT Pay = OT hours * rate per hour * 1.5
• Regular pay = rate per hour * 40 (for hours worked more than 40)
• Tithe = 10 % of regular pay
• Gross pay = sum ot regular pay & OT Pay
• Net pay = gross pay less titheOctober 13, 2021
NLAC Department of Information Techonology Studies
42
Group Activity
• Problem#3: Create a java program that allows the user to enter the student name, and the gender code M or F. Display Male for M and Female for F.
• Write the psuedocode and draw the flowchart
October 13, 2021NLAC Department of Information
Techonology Studies44
start
Student name,Gender code
Gender code ==
‘M’
Male
end
yes
Fig. 1 Program Flowchart October 13, 2021
NLAC Department of Information Techonology Studies
45
Female
no
Code Segment:
char genderCode;
String studName;
System.out.println(“Student name: ”);
studName = input.nextLine();
System.out.println(“Gender Code [M/F]: ”);
genderCode = input.next().charAt(0);
if (genderCode == ‘M’)
System.out.println(“Male”);
else
System.out.println(“Female”);
October 13, 2021NLAC Department of Information
Techonology Studies46
Code Segment:
char genderCode;
String studName;
System.out.println(“Student name: ”);
studName = input.nextLine();
System.out.println(“Gender Code [M/F]: ”);
genderCode = input.next().charAt(0);
if (genderCode == ‘M’)
System.out.println(“Male”);
else
System.out.println(“Female”);
October 13, 2021NLAC Department of Information
Techonology Studies47
Code Segment:
char genderCode;
String studName;
System.out.println(“Student name: ”);
studName = input.nextLine();
System.out.println(“Gender Code [M/F]: ”);
genderCode = input.next().charAt(0);
if (genderCode == ‘M’)
System.out.println(“Male”);
else
System.out.println(“Female”);
October 13, 2021NLAC Department of Information
Techonology Studies48
The if-else if Statement
Sometimes you need to be able to test a series of conditions
You can do this with the if-else if statement
October 13, 2021NLAC Department of Information
Techonology Studies50
If BooleanExpression1is true, then statement or block 1 is executed.
If BooleanExpression1is false, then BooleanExpression2
is tested. If BooleanExpression2 is
true, then statement orblock 2 is executed.
If BooleanExpression2 is false, then statement orblock 3 is executed.
October 13, 2021NLAC Department of Information
Techonology Studies51
if (booleanExpression1)
{
statement/block1;
}
else if (booleanExpression2)
{
statement/block2;
}
...
else
{
statement/block3;;
}
Syntax:
Note: You can have as many if else clauses as is needed.
FlowChart for if – else if (cont)
Boolean
test
statement(s)
subordinate to if
Independent
statement(s)
true
false
statement(s)
subordinate to elseif
Boolean
test
false
statement(s)
subordinate to else
true
October 13, 2021NLAC Department of Information
Techonology Studies52
start
Student name,Gender code
Gender code ==
‘M’
Male
end
yes
no
Fig. 1 Program Flowchart October 13, 2021
NLAC Department of Information Techonology Studies
53
Gender code == ‘F’
Female
yes
no
Code Segment:
char genderCode;
String studName;
System.out.println(“Student name: ”);
studName = input.nextLine();
System.out.println(“Gender Code [M/F]: ”);
genderCode = input.next().charAt(0);
if (genderCode == ‘M’)
System.out.println(“Male”);
else
System.out.println(“Female”);
October 13, 2021NLAC Department of Information
Techonology Studies54
Code Segment:
char genderCode;
String studName;
System.out.println(“Student name: ”);
studName = input.nextLine();
System.out.println(“Gender Code [M/F]: ”);
genderCode = input.next().charAt(0);
if (genderCode == ‘M’)
System.out.println(“Male”);
else if (genderCode == ‘F’)
System.out.println(“Female”);
October 13, 2021NLAC Department of Information
Techonology Studies55
• Logical AND operator
– Alternative to some nested if statements
– Used between two Boolean expressions to determine whether both are true
– Written as two ampersands (&&)
• Include complete Boolean expression on each side
– Both Boolean expressions that surround operator
• Must be true before action in statement can occur
56NLAC Department of
Information Techonology Studies
Using Logical AND and OR Operators
October 13, 2021
Example: Using the && Operator
if ( num > 0 && num < 10 )
System.out.println( "num is between 0 and 10" ) ;
57NLAC Department of
Information Techonology Studies
Using Logical Operators
October 13, 2021
System.out.print("Student name: ");
studName = input.nextLine();
System.out.print("Gender Code [M/F]: ");
genderCode = input.next().charAt(0);
if (genderCode == 'M' || genderCode=='m')
System.out.println("Male");
else if (genderCode == 'F' || genderCode=='f')
System.out.println("Female");
October 13, 2021NLAC Department of Information
Techonology Studies58
• Nested if statements
– Statements in which if structure is contained inside of another if structure
– Use when two conditions must be met before some action is taken
• Pay careful attention to placement of elseclauses
• else statements
– Always associated with if on “first in-last out” basis
59NLAC Department of
Information Techonology Studies
Nesting if and if...elseStatements
October 13, 2021
Syntax:if(Boolean_expression 1)
{
if(Boolean_expression 2)
{
statement(s);
}
}
Example:if ( num > 0 ) // Outer if
if ( num < 10 ) // Inner if
System.out.println( "num is between 0 and 10" ) ;
60NLAC Department of
Information Techonology Studies
Nesting if and if...elseStatements
Statement of inner if
Statement of outer if
October 13, 2021
Laboratory Activity
October 13, 2021NLAC Department of Information
Techonology Studies62
Up Next: switch…case Statement
• switch statement
– Alternative to series of nested if statements
– Test single variable against series of exact integer or character values
63
NLAC Department of
Information Techonology
Studies
Using the switch Statement
October 13, 2021
• Switch structure keywords
– switch
• Starts structure
• Followed by test expression
• Enclosed in parentheses
– case
• Followed by one of the possible values for test expression and colon
64NLAC Department of
Information Techonology Studies
Using the switch Statement (continued)
October 13, 2021
• Switch structure keywords (continued)– break
• Optionally terminates switch structure at end of each case
– default
• Used prior to any action if test variable does not match any case
• Optional
65NLAC Department of
Information Techonology Studies
Using the switch Statement (continued)
October 13, 2021
NLAC Department of Information Techonology Studies
66
Example using the switch Statement
int year;
System.out.println(“Enter year: “);year = input.nextInt();
if (year==1)System.out.println(“Freshman”);
else if (year==2)System.out.println(“Sophomore”);
else if (year==3)System.out.println(“Junior”);
else if (year==4)System.out.println(“Senior”);
else System.out.println(“Invalid Year”);
October 13, 2021
• break statements in switch structure
– If break statement omitted
• Program finds match for test variable
• All statements within switch statement execute from that point forward
• case statement
– No need to write code for each case
– Evaluate char variables
• Ignore whether uppercase or lowercase
67NLAC Department of
Information Techonology Studies
Using the switch Statement (continued)
October 13, 2021
• Why use switch statements?
– Convenient when several alternative courses of action depend on single integer or character variable
– Use only when there are reasonable number of specific matching values to be tested
68NLAC Department of
Information Techonology Studies
Using the switch Statement (continued)
October 13, 2021
Exerciseif (a==1)
x += 6;
else if (a == 2)
x += 10;
else if (a == 3)
x += 16;
else if (a == 4)
x += 34;
Convert this if statement into a switch statement
October 13, 2021NLAC Department of Information
Techonology Studies69
• NOT operator– Written as exclamation point (!)
– Negates result of any Boolean expression
– When preceded by NOT operator • Any expression evaluates as:
– true becomes false
– false becomes true
• Statements with NOT operator– Harder to read
– Require double set of parentheses
70NLAC Department of
Information Techonology Studies
Using the NOT Operator
October 13, 2021
• Combine as many AND or OR operators as needed
• Operator’s precedence
– How expression is evaluated
– Order agrees with common algebraic usage
• Arithmetic done first
• Assignment done last
• AND operator evaluated before OR operator
• Statements in parentheses evaluated first
71NLAC Department of
Information Techonology Studies
Understanding Precedence
October 13, 2021
NLAC Department of Information Techonology Studies
72
Understanding Precedence (continued)
October 13, 2021