Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then...
-
Upload
milo-hicks -
Category
Documents
-
view
239 -
download
0
description
Transcript of Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then...
![Page 1: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/1.jpg)
Selection statements Repetition statements
![Page 2: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/2.jpg)
Lesson plan
• Main concepts• Practice session
– If- then– Switch– Nested if
![Page 3: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/3.jpg)
Sequential sequence and Control Statement
• Sequential sequences are insufficient to solve most problems.– Get in the Car.– Drive to the supermarket.
• Tank nearly empty?– Get in the Car.– If we need gas, then fill up.– Drive to the supermarket.
![Page 4: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/4.jpg)
Dependent Actions
• A question is asked.– A condition is checked.
• The dependent actions are only performed if the answer is Yes (the condition is true).
• Sometimes the dependent actions are performed, and sometimes they are not.
![Page 5: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/5.jpg)
Example: Logging In• Computer access:
– Request a login name from the user.
– Request a matching password.– If the password matches the login name, then grant access.
![Page 6: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/6.jpg)
if (<boolean expression>) <block>;else <block>;
Simple Choice Statement
if (<boolean expression>) single statement;else single statement;
![Page 7: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/7.jpg)
Boolean expression• Boolean expression: is a conditional
expression that is evaluated to either true or false.
• Conditional expression: is a three part expression:<exp.> <relational operators> <exp.>
• Boolean expressions can be combined by boolean operators
![Page 8: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/8.jpg)
Relational Operators
Expression Meaninga == b Is a equal to b?a != b Is a not equal to b?a > b Is a greater than b?a < b Is a less than b?a >= b Is a greater than or equal to b?a <= b Is a less than or equal to b?
![Page 9: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/9.jpg)
Boolean operators
&& means AND|| means OR! means NOT
![Page 10: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/10.jpg)
Boolean OperatorsOperator a b Result && false any-value false
&& true false false
&& true true true
|| true any-value true
|| false false false
|| false true true
![Page 11: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/11.jpg)
Example of Boolean expression
testScore < 62age > 35Height > 6.5(testScore < 62) && (age > 35)
testScore = 95 ???????
![Page 12: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/12.jpg)
Block
- Represents compound statement{
<statement 1><statement 2><statement 3>
}
![Page 13: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/13.jpg)
ExampleIf (age < 35) {
coPay = 5;System.out.println(“….”);
} Else {
coPay = 10;System.out.println(“….”);
}
![Page 14: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/14.jpg)
Practice• Identify invalid boolean
expression
• Identify invalid If-then-else statement
![Page 15: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/15.jpg)
Answers for last practice exercise
I.(i) (x+1>=0) && (x==1)
Valid(ii) x2+2x-1
Invalid. Arithmetic expression(iii) ((y%2) != 0)
Valid
![Page 16: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/16.jpg)
Answers to last practice exercise
if (x==y) {System.out.println(“ x=y”);
}Else {
System.out.println(“ x does not equal to y”);}
Should be Lowercase
![Page 17: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/17.jpg)
Answers to last practice exercise
if (x=y) then {
grade = “A”;}else {
Grade = “B”;}
If we actually ask (is x equal y) then it should be (x==y)
![Page 18: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/18.jpg)
Answers to last practice exercise
if (x==y) {grade = “A”;
else grade = “B”;
}
Hanging “else”
![Page 19: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/19.jpg)
if (<boolean expression>) <block>;[else <block>;] // optional
Simple Choice Statement
if (<boolean expression>) single statement;[else single statement;] // optional
![Page 20: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/20.jpg)
Answers to some questions• Java machine code is generated
and stored in *.class files. For example:– LoanCalculator.java – LoanCalculator.class
• When we compile a java program outside of Textpad, we need to use:
javac.exe <source_name>• When we run a java program
outside of Textpad, we need to use:java.exe <class_name>
![Page 21: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/21.jpg)
Answers to some questions
• How to clear screen from DOS:for (int i=0; i<25; i++)
System.out.println();
![Page 22: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/22.jpg)
Nested If Statement
if (<boolean expression>)if (<boolean expression>) if (<boolean expression>) {
statement 1}
else if (<boolean expression>) {statement 2
} else {
statement 3 }
else ….
![Page 23: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/23.jpg)
Example
if (score >= 95) grade= “A”;
else if (score >=90)grade=“A-”;else if (score>=85)
grade =“B+”;
![Page 24: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/24.jpg)
SWITCH statementswitch (variable) {<case label 1>: <case body 1>……<case label 2>: <case body 2>}
<case label i>: has the formcase <constant>
or: default<case body i>: is a block or a single statement,
with break variable: of integer, or char data type
![Page 25: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/25.jpg)
Example switch (month) {
case 1: System.out.println("January"); break;
case 2: System.out.println("February"); break;
case 3: System.out.println("March"); break;
case 4: System.out.println("April"); break;
case 5: System.out.println("May"); break;
case 6: System.out.println("June"); break;
……………..
default: System.out.println("Not a month!"); break;
}
![Page 26: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/26.jpg)
Char data type & boolean data type
• Char:– It is 2 bytes– Char constant is denoted by single
quote:for example: ‘s’, ‘h’
Special characters:\b backspace, \t tab
• Boolean:- Can be true or false
![Page 27: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/27.jpg)
Examples
char aLetter;aLetter =‘k’;…aLetter =‘k’;
boolean flag;flag = true;…flag = false;
![Page 28: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/28.jpg)
PracticeEvaluate boolean expressionsIdentify invalid Switch statement
![Page 29: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/29.jpg)
Project 1 (d)
• Exit the system normally:– System.exit(0);
• Exit the system abnormally:e.g: System.exit(1);– a nonzero status code indicates
abnormal termination.
![Page 30: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/30.jpg)
Repetition statements• Repetition of statements is a further
common facet of everyday life and object behavior:– Repeat every day:
• Get up; have breakfast; go off to work; have lunch; do more work; have an evening meal; relax; go to bed.
• Java has three types of loop for repetition:– while, do-while, for.
![Page 31: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/31.jpg)
The While Loop
while(<boolean expression>){ // Repeat multiple statements. statement 1 statement 2 statement 3 ...}
![Page 32: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/32.jpg)
Example
// Print the numbers 1 to maximum.Assuming that maximum is 100
int maximum = 100;int nextToPrint = 1; while(nextToPrint <= maximum){ System.out.print(nextToPrint+" "); // Get ready to print the next number. nextToPrint = nextToPrint +1; } System.out.println();}
![Page 33: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/33.jpg)
The Do Loopdo{ // Repeat multiple statements. statement 1 statement 2 statement 3 ...} while(<boolean expression);
•Note that the statements in the body of the loop are always executed at least one.•Note the final semicolon, which is required.
![Page 34: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/34.jpg)
Printing a Sequenceint maximum = 100;int nextToPrint = 1;
do{ System.out.print(nextToPrint+" "); // Get ready for the next number. nextToPrint = nextToPrint + 1; } while(nextToPrint <= maximum); System.out.println();}
![Page 35: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/35.jpg)
The For Loop• Used to repeat statements a fixed
number of times:– Add three teaspoons of sugar.– Knock twice and then give the
password.• Used very often with arrays and
collections of objects • Similar in effect to the while loop.
![Page 36: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/36.jpg)
The For-Loop Outline// Repeat multiple statements.for(initialization; condition; post-body update){ // Statements to be repeated. statement 1 statement 2 statement 3 ...}
•Commonly used with increment and decrement operators.
![Page 37: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/37.jpg)
Increment and Decrement
• Used as a shorthand for add-one-to and subtract-one-from:
•value = value+1;•value += 1;•value++;
• Prefix and postfix forms:•++value;•--value;•value--;
![Page 38: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/38.jpg)
A For Loop to Print a Sequence
int maximum = 100;int nextToPrint;for(nextToPrint = 1; nextToPrint <= maximum; nextToPrint++){
System.out.print(nextToPrint+“$“+” ”);}System.out.println();
The variable is not available outside the loop12345678910111$ 2$ 3$ 4$ 5$ 6$.
![Page 39: Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.](https://reader035.fdocuments.us/reader035/viewer/2022062317/5a4d1b2c7f8b9ab0599995a4/html5/thumbnails/39.jpg)
Practice
Develop a java class called:SumCalculator.java
which computes a sum of all integer from 1 to 100 and displays the result to the screen