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

A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
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
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 

Recently uploaded (20)

A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
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
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 

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.