Download Lesson 1.4 Decision and looping structures File

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
ICS2O Decision and looping structures
Topics: Decision and looping structures
Curriculum expectations:
B1.5 identify situations in which decision and looping structures are required;
Resources/Materials required:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html
Lecture: Decision and looping structures
The if-then and if-then-else Statements
The if-then statement is the most basic of all the control flow statements. It tells your program to execute a
certain section of code only if a particular test evaluates to true.
Int x=10;
If (x>0){
System.out.println(“x is positive”);
}
else{
System.out.println(“x is negative”);
}
-------------------------------------public class TestNumber{
public static void main(String[] args){
double x=Double.parseDouble(args[0]);
if (x>0){
System.out.println(x+ " is positive");
}
else{
System.out.println(x+ " is negative");
}
}
}
Activity:
Type, compile and execute the following program
The switch Statement
Unlike if-then and if-then-else statements, the switch statement can have a number of possible
execution paths. A switch works with the byte, short, char, and int primitive data types. It also works
with enumerated types (discussed in Enum Types), the String class, and a few special classes that
wrap certain primitive types: Character, Byte, Short, and Integer
-------------------------------------------
The while and do-while Statements
The while statement continually executes a block of statements while a particular condition is true.
Its syntax can be expressed as:
while (expression) {
statement(s)
}
The for Statement
The for statement provides a compact way to iterate over a range of values. Programmers
often refer to it as the "for loop" because of the way in which it repeatedly loops until a
particular condition is satisfied. The general form of the for statement can be expressed as
follows:
for (initialization; termination;
increment) {
statement(s)
}
Homework:
1. Write a Calculator which can calculate any two numbers based on “ + - * /”
For example:
Input: java Calculator 3 + 2
Output: 3+2=5
Input: java Calculator 3 - 2
Output: 3-2=1
Input: java Calculator 3 / 2
Output: 3 / 2=1.5
Input: java Calculator 3 * 2
Output: 3 * 2=6
Teaching Strategies:
Direct Instruction (teacher-led)
*
Class Activity (teacher facilitation)
Direct Instruction (discussion possible)
*
Experiential learning (learn by doing)
Class Discussion (teacher facilitated)
Worksheets/Surveys
Small Group Discussion
Individual or Group Research
Partner Discussion/Conferencing
Teacher Modeling
1:1 Conferencing Teacher & Student.
Text-based modeling
Teacher reading to class
Use of Computers / Internet
Silent individual reading
Use of video tape or audio materials
*
*
Group based reading
Role Playing
Independent Work (teacher facilitation)
Presentations.
Group Work (teacher facilitation)
Guest Speaker / Interviews / Questions