No student devices needed. Know more
39 questions
allows selection of specific sections of code to be executed
decision control structures(if,else,switch)
repetition control structures(while,do-while,for)
branching statements(break,continue,return)
allow executing specific sections of code a number of times
decision control structures(if,else,switch)
repetition control structures(while,do-while,for)
branching statements(break,continue,return)
allows redirection of program flow
decision control structures(if,else,switch)
repetition control structures(while,do-while,for)
branching statements(break,continue,return)
allows us to change the ordering of how the statements in our programs are executed
decision control structures
repetition control structures
control structures
branching statements
specifies that a statement(or block of code) will be executed if and only if a certain boolean statement is true.
if-else statement
if statement
else if statement
nested if-else statement
What is the output?
int grade = 68;
if(grade > 60) {
System.out.println("Congratulations!");
}
"Congratulations!"
Congratulations!
Syntax Error
Compilation Error
What is the output?
int grade = 68;
if(grade > 60) {
System.out.println("Congratulations!");
System.out.println("You passed!");
}
Congratulations!
You passed!
"Congratulations!"
"You passed!"
Congratulations
You passed
its a part of a statement that should evaluate a boolean value that means that the execution of the condition should either result to a value of true or a fulse
if-else statement
boolean_expression
statement 1
statement 2
condition
used when we want to execute a certain statement if a condition is true, and a different statement if the condition is false
if statement
nested if-else statement
if-else statement
else if statement
What is the output?
int grade = 68;
if(grade > 60)
System.out.println("Congratulations!");
else
System.out.println("Sorry you failed");
"Congratulations!"
"Sorry you failed"
Congratulations!
Sorry you failed
Congratulations!
Sorry you failed
What is the output?
int grade = 68;
if(grade > 60) {
System.out.println("Congratulations!");
System.out.println("You passed!");
}
else {
System.out.println("Sorry you failed");
}
Sorry you failed
Syntax Error
Congratulations!
You passed!
Compile Error
this means that you can have other if-else inside another if-else block
if-else statement
else-if statement
if-statement
nested if-else blocks
the cascading of structures allows us to make more complex selections
if-else-else if statement
else-if
if statement
else statement
What is the output?
int grade = 105;
if(grade<75) {
System.out.println("Failed");
}
else if(grade>74||grade<=100) {
System.out.println("passed!");
}
else {
System.out.println("invalid");
}
Failed
passed
invalid
error
What is the output?
double grade = 92.0;
if(grade>=90) {
System.out.println("Excellent");
}
else if(grade<90&&grade>=100) {
System.out.println("Good job!");
}
else if(grade<80&&grade>=60) {
System.out.println("Study harder!");
}
else {
System.out.println("Sorry you failed");
}
Excellent
Good job!
Study harder!
Sorry you failed
allows branching on multiple outcomes
switch
while
do-while
is an integer or character expression
switch_expression
case_selector1,case_selector2 and so on
switch_statement
What is the output?
int grade = 92;
switch(grade) {
case 100:
System.out.println("Excellent!");
break;
case 90:
System.out.println("Good job!");
break;
case 80:
System.out.println("Study harder!");
break;
default:
System.out.println("Sorry you failed");
Excellent!
Good job!
Study harder!
Sorry you failed
What is the output?
int x = 0;
while(x<10) {
System.out.println(x);
x++;
}
0
1
2
3
4
5
6
7
8
9
0123456789
012345678910
0 10
what is the output?
while(true) {
System.out.println("hello");
}
hello hello hello
hello
compile error
infinite loop hello
what is the output?
while(false) {
System.out.println("hello");
}
hello
false
compile time error
false hello
what is the output?
int x = 0;
do {
System.out.println(x);
x++;
}while(x<10);
0123456789
0
1
2
3
4
5
6
7
8
9
10
9
8
7
6
5
4
3
2
1
012345678910
what is the output?
do {
System.out.println("hello");
}while(true);
infinite loop hello
hello
hello hello hello
true
what is the output?
do {
System.out.println("hello");
}while(false);
hello
false
infinite loop hello
compile time error
allows execution of the same code a number of times
for loop
while loop
do-while loop
what is the output?
int i;
for(i=0;i<10;i++) {
System.out.println(i);
}
0
1
2
3
4
5
6
7
8
9
0123456789
10
1
What is the output?
int i=0;
while(i<10) {
System.out.println(i);
i++;
}
012310
0
1
2
3
4
5
6
7
8
9
0123456789
error
terminates the enclosing switch statement, and flow control transfers to the statement immediately following the switch
break
continue
return
unlabeled break
what is output?
String names[] = {"Beah","Bianca","Lance","Belle","Nico",
"Yza","Gem","Ethan"};
String searchName = "Yza";
boolean foundName = false;
for(int i=0; i<names.length;i++) {
if(names[i].equals(searchName)) {
foundName = true;
break;
}
}
if(foundName)
System.out.println(searchName + " found!");
else System.out.println(searchName + " not found");
searchName found
Yza found!
Yza not found!
searchName not found
terminates an outer statements
labeled break statement
unlabeled break statement
break
continue
int[][] numbers = {{1,2,3},{4,5,6},{7,8,9}};
int searchNum = 5;
boolean foundNum = false;
searchLabel:
for(int i=0;i<numbers.length;i++) {
for(int j=0;j<numbers[i].length;j++) {
if(searchNum == numbers[i][j]) {
foundNum = true;
break searchLabel;
}
}
}
if(foundNum)System.out.println(searchNum + " found!");
else System.out.println(searchNum + " notfound!");
compile time error
5 found!
5 not found!
searchNum
what is the output?
int count;
for(count=1;count<=10;count++) {
if(count==5)
break;
System.out.println(" " +count);
}
System.out.println("broke out of loop at " + count);
1
2
3
4
broke out of loop at 5
1
2
3
4
5
broke out of loop at 5
error
what is the output?
boolean t = true;
first:{
second:{
third:{
System.out.println("Before the break;");
if(t)break second;
System.out.println("This won't execute");
}
System.out.println("This won't execute");
}
System.out.println("This is after second block");
}
before the break
this is after the second block
this wont execute
syntax error
compile time error
what is the output?
for(int count=1;count<=10;count++) {
if(count==5)
continue;
System.out.println(""+count);
}
System.out.println("used to continue to skip printing 5");
1
2
3
4
6
7
8
9
10
used to continue to skip printing 5
5
1
2
3
4
5
used to skip printing 5
error
begin:for(int count=1;count<=10;count++) {
if(count==5)
continue begin;
System.out.println(""+count);
}
System.out.println("used to continue to skip printing 5");
1234678910 used to skip printing 5
5 used to continue to skip printing 5
1
2
3
4
6
7
8
9
10
used to continue to skip printing 5
5
used to continue to skip counting 5
what is the output?
String names[] = {"Bianca","Lance","Beah","Beah"};
int count = 0;
for(int i = 0; i<names.length; i++) {
if(!names[i].equals("Beah")) {
continue;
}
count++;
}
System.out.println("There are "+count+" Beahs in the list");
There are 2 Beahs in the list
syntax error
compile error
there are
beahs in the list
outerLoop:
for(int i=0; i<5; i++) {
for(int j=0;j<5;j++) {
System.out.println("Inside for(j) loop");
if(j==2) continue outerLoop;
}
System.out.println("Inside for(i) loop");
}
Inside for(i) loop
Inside for(i) loop
Inside for(i) loop
Inside for(i) loop
Inside for(i) loop
Inside for(i) loop
Inside for(j) loop
Inside for(j) loop
Inside for(j) loop
Inside for(j) loop
Inside for(j) loop
Inside for(j) loop
Inside for(j) loop
Inside for(j) loop
Inside for(j) loop
Inside for(j) loop
Inside for(j) loop
Inside for(j) loop
Inside for(j) loop
Inside for(j) loop
Inside for(j) loop
Inside for(j) loop
Inside for(i) loop
used to exit from the current method
return
continue
break
labeled break
flow of control returns to the statement that follows the original method call
return
break
continue
unlabeled break
Explore all questions with a free account