SlideShare a Scribd company logo
1 of 16
CSE110
Principles of Programming
with Java
Lecture 13:
Loops and Conditional statements
Javier Gonzalez-Sanchez
javiergs@asu.edu
javiergs.engineering.asu.edu
Office Hours: By appointment
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 2
break Statement
• We can use a “break” statement to get out of the
loop
for (int i=1; i<=100; i=i+2) {
System.out.println(i);
if (i % 15 == 0) {
break;
}
}
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 3
continue Statement
• After executing a continue statement, the rest of
the statements within the loop will be skipped, then
the loop condition will be evaluated again.
for (int i=1; i<=4; i+=1) {
System.out.println(i);
System.out.println("Before");
if (i % 2 == 0) {
continue;
}
System.out.println("After");
}
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 4
Nested loops
• We can have a loop inside of another loop
for (int i=1; i<=3; i=i+1) {
for (int j=4; j>=1; j=j-1) {
System.out.println(i + "," + j);
}
}
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 5
One more thing
• We can have a loop inside of another loop
for (int i=1; i<=3; i++) {
for (int j=4; j>=1; j--) {
System.out.println(i + "," + j);
}
}
Tic Tac Toe
Case Study
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 7
TicTacToe.java
import javax.swing.*;
public class TicTacToe {
public static void main(String [] args) {
// 1. initialize
do {
// 2. user move
// 3. continue?
// 4. computer move
// 5. winner or tie?
// 6. print
} while (true);
}
}
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 8
TicTacToe.java
// 1. initialize
char a = ' ', b = ' ', c = ' ';
char d = ' ', e = ' ', f = ' ';
char g = ' ', h = ' ', i = ' ';
System.out.println("Game: ");
System.out.println(" " + a + " | " + b + " | " + c);
System.out.println("----------");
System.out.println(" " + d + " | " + e + " | " + f);
System.out.println("----------");
System.out.println(" " + g + " | " + h + " | " + i);
// 2. user move
boolean userFail;
do {
String option = JOptionPane.showInputDialog("Player:");
int move = Integer.parseInt(option);
userFail = true;
switch (move) {
case 1: if (a == ' ') {a = 'X'; userFail = false;} break;
case 2: if (b == ' ') {b = 'X'; userFail = false;} break;
case 3: if (c == ' ') {c = 'X'; userFail = false;} break;
case 4: if (d == ' ') {d = 'X'; userFail = false;} break;
case 5: if (e == ' ') {e = 'X'; userFail = false;} break;
case 6: if (f == ' ') {f = 'X'; userFail = false;} break;
case 7: if (g == ' ') {g = 'X'; userFail = false;} break;
case 8: if (h == ' ') {h = 'X'; userFail = false;} break;
case 9: if (i == ' ') {i = 'X'; userFail = false;} break;
default: userFail = true;
}
} while (userFail == true);
// 3. continue?
if (a!=' ' && b!=' ' && c!=' ' &&
d!=' ' && e!=' ' && f!=' ' &&
g!=' ' && h!=' ' && i!=' ') break;
// 4. computer move
boolean computerFail = false;
do {
double x = Math.random();
double y = Math.random();
if (x <= 0.33) {
if (y <= 0.33) {
if (a == ' '){a = 'O'; computerFail = false;} else {computerFail = true;}
} else if (y > 0.33 && y < 0.66) {
if (b == ' ') {b = 'O'; computerFail = false;} else {computerFail = true;}
} else if (y >= 0.66) {
if (c == ' ') {c = 'O'; computerFail = false;} else {computerFail = true;}
}
} else if (x > 0.33 && x < 0.66) {
if (y <= 0.33) {
if (d == ' ') {d = 'O'; computerFail = false;} else {computerFail = true;}
} else if (y > 0.33 && y < 0.66) {
if (e == ' ') {e = 'O'; computerFail = false;} else {computerFail = true;}
} else if (y >= 0.66) {
if (f == ' ') {f = 'O'; computerFail = false;} else {computerFail = true;}
}
} else if (x >= 0.66) {
if (y <= 0.33) {
if (g == ' ') {g = 'O'; computerFail = false;} else {computerFail = true;}
} else if (y > 0.33 && y < 0.66) {
if (h == ' ') {h = 'O'; computerFail = false;} else {computerFail = true;}
} else if (y >= 0.66) {
if (i == ' ') {i = 'O'; computerFail = false;} else {computerFail = true;}
}
}
} while (computerFail == true);
// 5. a winner or tie?
if (a=='O' && b=='O' && c=='O' ||
d=='O' && e=='O' && f=='O' ||
g=='O' && h=='O' && i=='O' ||
a=='O' && d=='O' && g=='O' ||
b=='O' && e=='O' && f=='O' ||
c=='O' && f=='O' && i=='O' ||
a=='O' && e=='O' && i=='O' ||
c=='O' && e=='O' && g=='O' ) {
System.out.println("YOU LOST!");
break;
} else if (a=='X' && b=='X' && c=='X' ||
d=='X' && e=='X' && f=='X' ||
g=='X' && h=='X' && i=='X' ||
a=='X' && d=='X' && g=='X' ||
b=='X' && e=='X' && f=='X' ||
c=='X' && f=='X' && i=='X' ||
a=='X' && e=='X' && i=='X' ||
c=='X' && e=='X' && g=='X' ) {
System.out.println("You Win!");
break;
} else if (a!=' ' && b!=' ' && c!=' ' &&
d!=' ' && e!=' ' && f!=' ' &&
g!=' ' && h!=' ' && i!=' ') {
System.out.println("It is a tie");
break;
}
// 6. print
System.out.println("Game: ");
System.out.println(" " + a + " | " + b + " | " + c);
System.out.println("----------");
System.out.println(" " + d + " | " + e + " | " + f);
System.out.println("----------");
System.out.println(" " + g + " | " + h + " | " + i);
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 14
TicTacToe.java
import javax.swing.*;
public class TicTacToe.java {
public static void main(String [] args) {
// 1. initialize
do {
// 2. user move
// 3. continue?
// 4. computer move
// 5. winner or tie?
// 6. print
} while (true);
}
}
Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 15
Reference
Chapter 3 and 4
CSE110 - Principles of Programming
Javier Gonzalez-Sanchez
javiergs@asu.edu
Summer 2017
Disclaimer. These slides can only be used as study material for the class CSE110 at ASU. They cannot be distributed or used for another purpose.

More Related Content

What's hot

Utility Classes Are Killing Us
Utility Classes Are Killing UsUtility Classes Are Killing Us
Utility Classes Are Killing UsYegor Bugayenko
 
The Ring programming language version 1.5.4 book - Part 51 of 185
The Ring programming language version 1.5.4 book - Part 51 of 185The Ring programming language version 1.5.4 book - Part 51 of 185
The Ring programming language version 1.5.4 book - Part 51 of 185Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 55 of 189
The Ring programming language version 1.6 book - Part 55 of 189The Ring programming language version 1.6 book - Part 55 of 189
The Ring programming language version 1.6 book - Part 55 of 189Mahmoud Samir Fayed
 
Python avanzado - parte 1
Python avanzado - parte 1Python avanzado - parte 1
Python avanzado - parte 1coto
 
The Ring programming language version 1.10 book - Part 63 of 212
The Ring programming language version 1.10 book - Part 63 of 212The Ring programming language version 1.10 book - Part 63 of 212
The Ring programming language version 1.10 book - Part 63 of 212Mahmoud Samir Fayed
 
The Ring programming language version 1.3 book - Part 41 of 88
The Ring programming language version 1.3 book - Part 41 of 88The Ring programming language version 1.3 book - Part 41 of 88
The Ring programming language version 1.3 book - Part 41 of 88Mahmoud Samir Fayed
 
Homemade GoTo mount for Telescopes using Nylon wheels, GT2 belts and 100:1 ge...
Homemade GoTo mount for Telescopes using Nylon wheels, GT2 belts and 100:1 ge...Homemade GoTo mount for Telescopes using Nylon wheels, GT2 belts and 100:1 ge...
Homemade GoTo mount for Telescopes using Nylon wheels, GT2 belts and 100:1 ge...Janis Alnis
 
The Ring programming language version 1.7 book - Part 64 of 196
The Ring programming language version 1.7 book - Part 64 of 196The Ring programming language version 1.7 book - Part 64 of 196
The Ring programming language version 1.7 book - Part 64 of 196Mahmoud Samir Fayed
 
Video club consulta
Video club consultaVideo club consulta
Video club consultaRuth Cujilan
 
Patrick Kettner - JavaScript without javascript
Patrick Kettner - JavaScript without javascriptPatrick Kettner - JavaScript without javascript
Patrick Kettner - JavaScript without javascriptOdessaJS Conf
 
Inteligencia artificial 4
Inteligencia artificial 4Inteligencia artificial 4
Inteligencia artificial 4Nauber Gois
 
Problems Collection of Differential Equation II
Problems Collection of Differential Equation IIProblems Collection of Differential Equation II
Problems Collection of Differential Equation IIMeiva Lestari
 
Javascript Without Javascript
Javascript Without JavascriptJavascript Without Javascript
Javascript Without JavascriptPatrick Kettner
 
A Course in Fuzzy Systems and Control Matlab Chapter two
A Course in Fuzzy Systems and Control Matlab Chapter twoA Course in Fuzzy Systems and Control Matlab Chapter two
A Course in Fuzzy Systems and Control Matlab Chapter twoChung Hua Universit
 
ゲーム理論BASIC 第25回 -動的な情報不完備ゲーム-
ゲーム理論BASIC 第25回 -動的な情報不完備ゲーム-ゲーム理論BASIC 第25回 -動的な情報不完備ゲーム-
ゲーム理論BASIC 第25回 -動的な情報不完備ゲーム-ssusere0a682
 
2c astable monostable
2c astable monostable2c astable monostable
2c astable monostableyeksdech
 

What's hot (19)

Utility Classes Are Killing Us
Utility Classes Are Killing UsUtility Classes Are Killing Us
Utility Classes Are Killing Us
 
The Ring programming language version 1.5.4 book - Part 51 of 185
The Ring programming language version 1.5.4 book - Part 51 of 185The Ring programming language version 1.5.4 book - Part 51 of 185
The Ring programming language version 1.5.4 book - Part 51 of 185
 
The Ring programming language version 1.6 book - Part 55 of 189
The Ring programming language version 1.6 book - Part 55 of 189The Ring programming language version 1.6 book - Part 55 of 189
The Ring programming language version 1.6 book - Part 55 of 189
 
05 2 관계논리비트연산
05 2 관계논리비트연산05 2 관계논리비트연산
05 2 관계논리비트연산
 
Python avanzado - parte 1
Python avanzado - parte 1Python avanzado - parte 1
Python avanzado - parte 1
 
The Ring programming language version 1.10 book - Part 63 of 212
The Ring programming language version 1.10 book - Part 63 of 212The Ring programming language version 1.10 book - Part 63 of 212
The Ring programming language version 1.10 book - Part 63 of 212
 
The Ring programming language version 1.3 book - Part 41 of 88
The Ring programming language version 1.3 book - Part 41 of 88The Ring programming language version 1.3 book - Part 41 of 88
The Ring programming language version 1.3 book - Part 41 of 88
 
Homemade GoTo mount for Telescopes using Nylon wheels, GT2 belts and 100:1 ge...
Homemade GoTo mount for Telescopes using Nylon wheels, GT2 belts and 100:1 ge...Homemade GoTo mount for Telescopes using Nylon wheels, GT2 belts and 100:1 ge...
Homemade GoTo mount for Telescopes using Nylon wheels, GT2 belts and 100:1 ge...
 
Mouse and Cat Game In C++
Mouse and Cat Game In C++Mouse and Cat Game In C++
Mouse and Cat Game In C++
 
The Ring programming language version 1.7 book - Part 64 of 196
The Ring programming language version 1.7 book - Part 64 of 196The Ring programming language version 1.7 book - Part 64 of 196
The Ring programming language version 1.7 book - Part 64 of 196
 
Video club consulta
Video club consultaVideo club consulta
Video club consulta
 
Patrick Kettner - JavaScript without javascript
Patrick Kettner - JavaScript without javascriptPatrick Kettner - JavaScript without javascript
Patrick Kettner - JavaScript without javascript
 
Inteligencia artificial 4
Inteligencia artificial 4Inteligencia artificial 4
Inteligencia artificial 4
 
DEV C++
DEV C++DEV C++
DEV C++
 
Problems Collection of Differential Equation II
Problems Collection of Differential Equation IIProblems Collection of Differential Equation II
Problems Collection of Differential Equation II
 
Javascript Without Javascript
Javascript Without JavascriptJavascript Without Javascript
Javascript Without Javascript
 
A Course in Fuzzy Systems and Control Matlab Chapter two
A Course in Fuzzy Systems and Control Matlab Chapter twoA Course in Fuzzy Systems and Control Matlab Chapter two
A Course in Fuzzy Systems and Control Matlab Chapter two
 
ゲーム理論BASIC 第25回 -動的な情報不完備ゲーム-
ゲーム理論BASIC 第25回 -動的な情報不完備ゲーム-ゲーム理論BASIC 第25回 -動的な情報不完備ゲーム-
ゲーム理論BASIC 第25回 -動的な情報不完備ゲーム-
 
2c astable monostable
2c astable monostable2c astable monostable
2c astable monostable
 

Similar to CSE110 Loops and Conditionals in Java

conditional statements
conditional statementsconditional statements
conditional statementsJames Brotsos
 
ch04-conditional-execution.ppt
ch04-conditional-execution.pptch04-conditional-execution.ppt
ch04-conditional-execution.pptMahyuddin8
 
MineSweeper.java public class MS { public static void main(Strin.pdf
MineSweeper.java public class MS { public static void main(Strin.pdfMineSweeper.java public class MS { public static void main(Strin.pdf
MineSweeper.java public class MS { public static void main(Strin.pdfaniyathikitchen
 
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdfIn Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdfanjandavid
 
Hypercritical C++ Code Review
Hypercritical C++ Code ReviewHypercritical C++ Code Review
Hypercritical C++ Code ReviewAndrey Karpov
 
Find the output of the following code (Java for ICSE)
Find the output of the following code (Java for ICSE)Find the output of the following code (Java for ICSE)
Find the output of the following code (Java for ICSE)Mokshya Priyadarshee
 
To Err Is Human
To Err Is HumanTo Err Is Human
To Err Is HumanAlex Liu
 
L04 - Control Structuresjhgjhgjhgjhgfjgfjhgfjgf.pptx
L04 - Control Structuresjhgjhgjhgjhgfjgfjhgfjgf.pptxL04 - Control Structuresjhgjhgjhgjhgfjgfjhgfjgf.pptx
L04 - Control Structuresjhgjhgjhgjhgfjgfjhgfjgf.pptxEliasPetros
 
C++ L03-Control Structure
C++ L03-Control StructureC++ L03-Control Structure
C++ L03-Control StructureMohammad Shaker
 
import java.util.Scanner;public class Main {    public static in.pdf
import java.util.Scanner;public class Main {    public static in.pdfimport java.util.Scanner;public class Main {    public static in.pdf
import java.util.Scanner;public class Main {    public static in.pdfanwarsadath111
 
TASK #1In the domain class you will create a loop that will prompt.pdf
TASK #1In the domain class you will create a loop that will prompt.pdfTASK #1In the domain class you will create a loop that will prompt.pdf
TASK #1In the domain class you will create a loop that will prompt.pdfindiaartz
 

Similar to CSE110 Loops and Conditionals in Java (20)

conditional statements
conditional statementsconditional statements
conditional statements
 
ch04-conditional-execution.ppt
ch04-conditional-execution.pptch04-conditional-execution.ppt
ch04-conditional-execution.ppt
 
MineSweeper.java public class MS { public static void main(Strin.pdf
MineSweeper.java public class MS { public static void main(Strin.pdfMineSweeper.java public class MS { public static void main(Strin.pdf
MineSweeper.java public class MS { public static void main(Strin.pdf
 
Java Programmin: Selections
Java Programmin: SelectionsJava Programmin: Selections
Java Programmin: Selections
 
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdfIn Java using Eclipse, Im suppose to write a class that encapsulat.pdf
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
 
Hypercritical C++ Code Review
Hypercritical C++ Code ReviewHypercritical C++ Code Review
Hypercritical C++ Code Review
 
Find the output of the following code (Java for ICSE)
Find the output of the following code (Java for ICSE)Find the output of the following code (Java for ICSE)
Find the output of the following code (Java for ICSE)
 
To Err Is Human
To Err Is HumanTo Err Is Human
To Err Is Human
 
Practice
PracticePractice
Practice
 
L04 - Control Structuresjhgjhgjhgjhgfjgfjhgfjgf.pptx
L04 - Control Structuresjhgjhgjhgjhgfjgfjhgfjgf.pptxL04 - Control Structuresjhgjhgjhgjhgfjgfjhgfjgf.pptx
L04 - Control Structuresjhgjhgjhgjhgfjgfjhgfjgf.pptx
 
Ch4
Ch4Ch4
Ch4
 
project3
project3project3
project3
 
Final Project
Final ProjectFinal Project
Final Project
 
C programs
C programsC programs
C programs
 
Comp102 lec 6
Comp102   lec 6Comp102   lec 6
Comp102 lec 6
 
C++ L03-Control Structure
C++ L03-Control StructureC++ L03-Control Structure
C++ L03-Control Structure
 
import java.util.Scanner;public class Main {    public static in.pdf
import java.util.Scanner;public class Main {    public static in.pdfimport java.util.Scanner;public class Main {    public static in.pdf
import java.util.Scanner;public class Main {    public static in.pdf
 
Fekra c++ Course #2
Fekra c++ Course #2Fekra c++ Course #2
Fekra c++ Course #2
 
Ch4
Ch4Ch4
Ch4
 
TASK #1In the domain class you will create a loop that will prompt.pdf
TASK #1In the domain class you will create a loop that will prompt.pdfTASK #1In the domain class you will create a loop that will prompt.pdf
TASK #1In the domain class you will create a loop that will prompt.pdf
 

More from Javier Gonzalez-Sanchez (20)

201804 SER332 Lecture 01
201804 SER332 Lecture 01201804 SER332 Lecture 01
201804 SER332 Lecture 01
 
201801 SER332 Lecture 03
201801 SER332 Lecture 03201801 SER332 Lecture 03
201801 SER332 Lecture 03
 
201801 SER332 Lecture 04
201801 SER332 Lecture 04201801 SER332 Lecture 04
201801 SER332 Lecture 04
 
201801 SER332 Lecture 02
201801 SER332 Lecture 02201801 SER332 Lecture 02
201801 SER332 Lecture 02
 
201801 CSE240 Lecture 26
201801 CSE240 Lecture 26201801 CSE240 Lecture 26
201801 CSE240 Lecture 26
 
201801 CSE240 Lecture 25
201801 CSE240 Lecture 25201801 CSE240 Lecture 25
201801 CSE240 Lecture 25
 
201801 CSE240 Lecture 24
201801 CSE240 Lecture 24201801 CSE240 Lecture 24
201801 CSE240 Lecture 24
 
201801 CSE240 Lecture 23
201801 CSE240 Lecture 23201801 CSE240 Lecture 23
201801 CSE240 Lecture 23
 
201801 CSE240 Lecture 22
201801 CSE240 Lecture 22201801 CSE240 Lecture 22
201801 CSE240 Lecture 22
 
201801 CSE240 Lecture 21
201801 CSE240 Lecture 21201801 CSE240 Lecture 21
201801 CSE240 Lecture 21
 
201801 CSE240 Lecture 20
201801 CSE240 Lecture 20201801 CSE240 Lecture 20
201801 CSE240 Lecture 20
 
201801 CSE240 Lecture 19
201801 CSE240 Lecture 19201801 CSE240 Lecture 19
201801 CSE240 Lecture 19
 
201801 CSE240 Lecture 18
201801 CSE240 Lecture 18201801 CSE240 Lecture 18
201801 CSE240 Lecture 18
 
201801 CSE240 Lecture 17
201801 CSE240 Lecture 17201801 CSE240 Lecture 17
201801 CSE240 Lecture 17
 
201801 CSE240 Lecture 16
201801 CSE240 Lecture 16201801 CSE240 Lecture 16
201801 CSE240 Lecture 16
 
201801 CSE240 Lecture 15
201801 CSE240 Lecture 15201801 CSE240 Lecture 15
201801 CSE240 Lecture 15
 
201801 CSE240 Lecture 14
201801 CSE240 Lecture 14201801 CSE240 Lecture 14
201801 CSE240 Lecture 14
 
201801 CSE240 Lecture 13
201801 CSE240 Lecture 13201801 CSE240 Lecture 13
201801 CSE240 Lecture 13
 
201801 CSE240 Lecture 12
201801 CSE240 Lecture 12201801 CSE240 Lecture 12
201801 CSE240 Lecture 12
 
201801 CSE240 Lecture 11
201801 CSE240 Lecture 11201801 CSE240 Lecture 11
201801 CSE240 Lecture 11
 

Recently uploaded

Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 

Recently uploaded (20)

Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 

CSE110 Loops and Conditionals in Java

  • 1. CSE110 Principles of Programming with Java Lecture 13: Loops and Conditional statements Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu Office Hours: By appointment
  • 2. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 2 break Statement • We can use a “break” statement to get out of the loop for (int i=1; i<=100; i=i+2) { System.out.println(i); if (i % 15 == 0) { break; } }
  • 3. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 3 continue Statement • After executing a continue statement, the rest of the statements within the loop will be skipped, then the loop condition will be evaluated again. for (int i=1; i<=4; i+=1) { System.out.println(i); System.out.println("Before"); if (i % 2 == 0) { continue; } System.out.println("After"); }
  • 4. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 4 Nested loops • We can have a loop inside of another loop for (int i=1; i<=3; i=i+1) { for (int j=4; j>=1; j=j-1) { System.out.println(i + "," + j); } }
  • 5. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 5 One more thing • We can have a loop inside of another loop for (int i=1; i<=3; i++) { for (int j=4; j>=1; j--) { System.out.println(i + "," + j); } }
  • 7. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 7 TicTacToe.java import javax.swing.*; public class TicTacToe { public static void main(String [] args) { // 1. initialize do { // 2. user move // 3. continue? // 4. computer move // 5. winner or tie? // 6. print } while (true); } }
  • 8. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 8 TicTacToe.java // 1. initialize char a = ' ', b = ' ', c = ' '; char d = ' ', e = ' ', f = ' '; char g = ' ', h = ' ', i = ' '; System.out.println("Game: "); System.out.println(" " + a + " | " + b + " | " + c); System.out.println("----------"); System.out.println(" " + d + " | " + e + " | " + f); System.out.println("----------"); System.out.println(" " + g + " | " + h + " | " + i);
  • 9. // 2. user move boolean userFail; do { String option = JOptionPane.showInputDialog("Player:"); int move = Integer.parseInt(option); userFail = true; switch (move) { case 1: if (a == ' ') {a = 'X'; userFail = false;} break; case 2: if (b == ' ') {b = 'X'; userFail = false;} break; case 3: if (c == ' ') {c = 'X'; userFail = false;} break; case 4: if (d == ' ') {d = 'X'; userFail = false;} break; case 5: if (e == ' ') {e = 'X'; userFail = false;} break; case 6: if (f == ' ') {f = 'X'; userFail = false;} break; case 7: if (g == ' ') {g = 'X'; userFail = false;} break; case 8: if (h == ' ') {h = 'X'; userFail = false;} break; case 9: if (i == ' ') {i = 'X'; userFail = false;} break; default: userFail = true; } } while (userFail == true);
  • 10. // 3. continue? if (a!=' ' && b!=' ' && c!=' ' && d!=' ' && e!=' ' && f!=' ' && g!=' ' && h!=' ' && i!=' ') break;
  • 11. // 4. computer move boolean computerFail = false; do { double x = Math.random(); double y = Math.random(); if (x <= 0.33) { if (y <= 0.33) { if (a == ' '){a = 'O'; computerFail = false;} else {computerFail = true;} } else if (y > 0.33 && y < 0.66) { if (b == ' ') {b = 'O'; computerFail = false;} else {computerFail = true;} } else if (y >= 0.66) { if (c == ' ') {c = 'O'; computerFail = false;} else {computerFail = true;} } } else if (x > 0.33 && x < 0.66) { if (y <= 0.33) { if (d == ' ') {d = 'O'; computerFail = false;} else {computerFail = true;} } else if (y > 0.33 && y < 0.66) { if (e == ' ') {e = 'O'; computerFail = false;} else {computerFail = true;} } else if (y >= 0.66) { if (f == ' ') {f = 'O'; computerFail = false;} else {computerFail = true;} } } else if (x >= 0.66) { if (y <= 0.33) { if (g == ' ') {g = 'O'; computerFail = false;} else {computerFail = true;} } else if (y > 0.33 && y < 0.66) { if (h == ' ') {h = 'O'; computerFail = false;} else {computerFail = true;} } else if (y >= 0.66) { if (i == ' ') {i = 'O'; computerFail = false;} else {computerFail = true;} } } } while (computerFail == true);
  • 12. // 5. a winner or tie? if (a=='O' && b=='O' && c=='O' || d=='O' && e=='O' && f=='O' || g=='O' && h=='O' && i=='O' || a=='O' && d=='O' && g=='O' || b=='O' && e=='O' && f=='O' || c=='O' && f=='O' && i=='O' || a=='O' && e=='O' && i=='O' || c=='O' && e=='O' && g=='O' ) { System.out.println("YOU LOST!"); break; } else if (a=='X' && b=='X' && c=='X' || d=='X' && e=='X' && f=='X' || g=='X' && h=='X' && i=='X' || a=='X' && d=='X' && g=='X' || b=='X' && e=='X' && f=='X' || c=='X' && f=='X' && i=='X' || a=='X' && e=='X' && i=='X' || c=='X' && e=='X' && g=='X' ) { System.out.println("You Win!"); break; } else if (a!=' ' && b!=' ' && c!=' ' && d!=' ' && e!=' ' && f!=' ' && g!=' ' && h!=' ' && i!=' ') { System.out.println("It is a tie"); break; }
  • 13. // 6. print System.out.println("Game: "); System.out.println(" " + a + " | " + b + " | " + c); System.out.println("----------"); System.out.println(" " + d + " | " + e + " | " + f); System.out.println("----------"); System.out.println(" " + g + " | " + h + " | " + i);
  • 14. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 14 TicTacToe.java import javax.swing.*; public class TicTacToe.java { public static void main(String [] args) { // 1. initialize do { // 2. user move // 3. continue? // 4. computer move // 5. winner or tie? // 6. print } while (true); } }
  • 15. Javier Gonzalez-Sanchez | CSE110 | Summer 2017 | 15 Reference Chapter 3 and 4
  • 16. CSE110 - Principles of Programming Javier Gonzalez-Sanchez javiergs@asu.edu Summer 2017 Disclaimer. These slides can only be used as study material for the class CSE110 at ASU. They cannot be distributed or used for another purpose.