for(int i = 0; i < 100; i++)
100 iterations
for(int i = 10; i > 0; i++)
Infinite Loop: i will never decrease
What is a method?
A segment of code with a name that can be called over and over again
Which loop would you choose for a program that fills the Comp Sci gradebook with a 100 for each student's grade?
For Loop
A for loop that repeats 5 times
for(int i = 0; i < 5; i++)
for(int i = 0; i <= 100; i++)
101 iterations
//assume that Scanner kb has been declared and initialized
SOP("Is this a loop?");
String answer = kb.nextLine();
while(answer.equals("yes")){
SOP("This is a loop!");
}
Infinite Loop: answer never changes
What is a loop?
A segment of code that will be repeated based on a specific condition
Which loop would I choose for a robot that will continue to make cookies until my cookie jar is full?
While Loop
(Do/While - 50 points)
A method that takes one integer and returns that integer's cubed value
public static void cube(int x){
return x*x*x;
}
x = 0;
while(x < 100){
SOP ("This is a loop!");
x+=5;
}
20 iterations
//assume Scanner kb has been declared and initialized
do{SOP("Is this a loop?");
String answer = kb.nextLine();
} while(!answer.equals("no"));
Compile Time Error - scope of answer ends before while statement
access modifier
controls who can access the method
Which loop should I choose if I am writing a program for a robot that takes care of the "bottomless soft drink" orders at a restaurant?
Do/While
(While Loop - 50 points)
A while loop that executes only if the String variable answer is "yes"
while(answer.equals("yes"){
//code here
}
int x = 0;
do{
SOP("This is a loop!");
}while(x != 0);
1 iteration
public static void{
//here is some code
}
Compile-Time Error: No Method Name!
parameter
the declared type and number of pieces of data a method can receive
What structure should I use if I need to print a set of instructions to the screen every time my user makes a mistake?
A void method
("Method" without "void" - 50 points)
a method that takes no parameters and returns nothing
public static void myMethod(){
//code goes here
}
//assume Scanner kb has been declared & initialized
String answer = "";
do{SOP("Is this a loop?");
answer = kb.nextLine();
} while (!answer.equals("yes");
As many iterations as it takes for the user to enter "yes", but it will run at least once
public static void myMethod(int x){
return x * x;
}
Compile-Time Error: void methods cannot use return
argument
What structure would I use if I needed to frequently calculate the volume of different containers?
A method with parameters (return value)
(Method no specific details - 50 points)
A method that takes a String as a parameter and prints "best teacher" if the string is "Mrs. Nabors"
public static void myMethod(String s){
if(s.equals("Mrs.Nabors"){
SOP("Best Teacher!");
}
}
//assume Scanner kb has been declared and initialized
SOP("Is this a loop?");
String answer = kb.nextLine();
if(answer.equals("yes")){
while(!answer.equals("no")){
SOP("Is this still a loop?");
answer = kb.nextLine();
}
}
0 iterations if the user does not enter "yes" the first time; at least one iteration if the user enters "yes" the first time
public static int myMethod(x){
return x*x;
}
Compile-Time Error: Parameter needs a data type!
scope
the limitations of which part of code knows about a variable's value/existence
Which structure should you use if you need to give every student in the Comp Sci gradebook a score that is dependent on how many questions on a test they got correct?
for loop w/ if/else if/else or switch inside the loop
A method that takes an integer x and will print "This is the song that never ends" x times
public static void myMethod(int x){
for(int i = 0; i < x; i++){
SOP("This is the song that never ends");
}
}