SlideShare uma empresa Scribd logo
1 de 13
BON SECOURS COLLEGE FOR WOMEN
THANJAVUR
DEPARTMENT OF INFORMATION
TECHNOLOGY
Dr.R.SUGANYA
Head & Asst.Professor
Bon Secours College for Women
Thanjavur
PROGRAMING IN JAVA
CONTROL STRUCTURE
CONTROL STRUCTURE
There are three types in Java:
 If/else/else if, ternary operator and switch.
 Loops that are used to iterate through multiple values/objects
and repeatedly run specific code blocks.
 The basic loop types in Java are for, while and do while.
 Branching Statements, which are used to alter the flow of
control in loops
There are three kinds of control structures:
• Conditional Branches, which we use for choosing
between two or more paths. There are three types in
Java: if/else/else if, ternary operator and switch.
• Loops that are used to iterate through multiple
values/objects and repeatedly run specific code
blocks. The basic loop types in Java are for, while and do
while.
• Branching Statements, which are used to alter the flow of
control in loops. There are two types in
Java: break and continue.
2. If/Else/Else If
• The if/else statement is the most basic of control structure
but can also be considered the very basis of decision making
in programming.
• While if can be used by itself, the most common use-
scenario is choosing between two paths with if/else:
if (count > 2) { System.out.println("Count is higher than 2"); }
else { System.out.println("Count is lower or equal than 2");
}Theoretically, we can infinitely chain or nest if/else blocks
but this will hurt code readability, and that's why it's not
advised.
• We'll explore alternative statements in the rest of this
article.
3. Ternary Operator
• We can use the ternary operator as a shorthand expression that
works like an if/else statement.
• Let's see our if/else example again:
• if (count > 2) { System.out.println("Count is higher than 2"); }
else { System.out.println("Count is lower or equal than 2"); }We
can refactor this with a ternary as follows:
• System.out.println(count > 2 ? "Count is higher than 2" : "Count
is lower or equal than 2");While ternary can be a great way to
make our code more readable, it isn't always a good substitute
for if/else.
4.Switch
• If we have multiple cases to choose from, we can use
a switch statement.
• Let's again see a simple example:
• int count = 3; switch (count) { case 0:
System.out.println("Count is equal to 0"); break; case 1:
System.out.println("Count is equal to 1"); break; default:
System.out.println("Count is either negative, or higher
than 1"); break; }Three or more if/else statements can be
hard to read. As one of the possible workarounds, we can
use switch, as seen above.
5.Loops
• We use loops when we need to repeat the same code
multiple times in succession.
• Let's see a quick example of
comparable for and while type of loops:
• for (int i = 1; i <= 50; i++) { methodToRepeat(); } int
whileCounter = 1; while (whileCounter <= 50) {
methodToRepeat(); whileCounter++; } Both code blocks
above will call methodToRepeat 50 times.
6. Break
• We need to use break to exit early from a loop.
• Let's see a quick example:
• List<String> names = getNameList(); String name = "John
Doe"; int index = 0; for ( ; index < names.length; index++) { if
(names[index].equals(name)) { break; } }Here, we are looking
for a name in a list of names, and we want to stop looking
once we've found it.
• A loop would normally go to completion, but we've
used break here to short-circuit that and exit early.
7. Continue
• Simply put, continue means to skip the rest of the loop we're
in:
• List<String> names = getNameList(); String name = "John
Doe"; String list = ""; for (int i = 0; i < names.length; i++) { if
(names[i].equals(name)) { continue; } list += names[i]; }Here,
we skip appending the duplicate names into the list.
Different control structure:
 The basic Control Structures in programming languages are:
Conditionals (or Selection):
 which are used to execute one or more statements if a
condition is met. Loops (or Iteration):
 which purpose is to repeat a statement a certain number of
times or while a condition is fulfilled.
Control statements in Java:
• Decision Making Statements.
• Simple if statement.
• if-else statement.
• Nested if statement.
• Switch statement.
• Looping statements.
• While.
• Do-while.

Mais conteúdo relacionado

Semelhante a JAVA.pptx

ch2 Python flow control.pdf
ch2 Python flow control.pdfch2 Python flow control.pdf
ch2 Python flow control.pdfRanjanaThakuria1
 
plsql tutorialhub....
plsql tutorialhub....plsql tutorialhub....
plsql tutorialhub....Abhiram Vijay
 
Control statements
Control statementsControl statements
Control statementsCutyChhaya
 
Presentation 2nd
Presentation 2ndPresentation 2nd
Presentation 2ndConnex
 
java basics - keywords, statements data types and arrays
java basics - keywords, statements data types and arraysjava basics - keywords, statements data types and arrays
java basics - keywords, statements data types and arraysmellosuji
 
Fundamental programming structures in java
Fundamental programming structures in javaFundamental programming structures in java
Fundamental programming structures in javaShashwat Shriparv
 
ProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdf
ProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdfProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdf
ProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdflailoesakhan
 
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysUnit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysDevaKumari Vijay
 
Programming Concepts
Programming ConceptsProgramming Concepts
Programming Conceptsjoshss7
 
prt123.pptx
prt123.pptxprt123.pptx
prt123.pptxASADKS
 

Semelhante a JAVA.pptx (20)

CPP04 - Selection
CPP04 - SelectionCPP04 - Selection
CPP04 - Selection
 
Init() Lesson 3
Init() Lesson 3Init() Lesson 3
Init() Lesson 3
 
ch2 Python flow control.pdf
ch2 Python flow control.pdfch2 Python flow control.pdf
ch2 Python flow control.pdf
 
kotlin-nutshell.pptx
kotlin-nutshell.pptxkotlin-nutshell.pptx
kotlin-nutshell.pptx
 
02_Data Types in java.pdf
02_Data Types in java.pdf02_Data Types in java.pdf
02_Data Types in java.pdf
 
plsql tutorialhub....
plsql tutorialhub....plsql tutorialhub....
plsql tutorialhub....
 
Md04 flow control
Md04 flow controlMd04 flow control
Md04 flow control
 
String.pptx
String.pptxString.pptx
String.pptx
 
Control statements
Control statementsControl statements
Control statements
 
130707833146508191
130707833146508191130707833146508191
130707833146508191
 
Presentation 2nd
Presentation 2ndPresentation 2nd
Presentation 2nd
 
java basics - keywords, statements data types and arrays
java basics - keywords, statements data types and arraysjava basics - keywords, statements data types and arrays
java basics - keywords, statements data types and arrays
 
02basics
02basics02basics
02basics
 
Fundamental programming structures in java
Fundamental programming structures in javaFundamental programming structures in java
Fundamental programming structures in java
 
ProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdf
ProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdfProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdf
ProgPrinc_Lecture_3_Data_Structures_and_Iteration-2.pdf
 
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arraysUnit 2-data types,Variables,Operators,Conitionals,loops and arrays
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
 
Programming Concepts
Programming ConceptsProgramming Concepts
Programming Concepts
 
prt123.pptx
prt123.pptxprt123.pptx
prt123.pptx
 
Ch4_part1.ppt
Ch4_part1.pptCh4_part1.ppt
Ch4_part1.ppt
 
Ch4_part1.ppt
Ch4_part1.pptCh4_part1.ppt
Ch4_part1.ppt
 

Último

Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 

Último (20)

Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 

JAVA.pptx

  • 1. BON SECOURS COLLEGE FOR WOMEN THANJAVUR
  • 2. DEPARTMENT OF INFORMATION TECHNOLOGY Dr.R.SUGANYA Head & Asst.Professor Bon Secours College for Women Thanjavur
  • 4. CONTROL STRUCTURE There are three types in Java:  If/else/else if, ternary operator and switch.  Loops that are used to iterate through multiple values/objects and repeatedly run specific code blocks.  The basic loop types in Java are for, while and do while.  Branching Statements, which are used to alter the flow of control in loops
  • 5. There are three kinds of control structures: • Conditional Branches, which we use for choosing between two or more paths. There are three types in Java: if/else/else if, ternary operator and switch. • Loops that are used to iterate through multiple values/objects and repeatedly run specific code blocks. The basic loop types in Java are for, while and do while. • Branching Statements, which are used to alter the flow of control in loops. There are two types in Java: break and continue.
  • 6. 2. If/Else/Else If • The if/else statement is the most basic of control structure but can also be considered the very basis of decision making in programming. • While if can be used by itself, the most common use- scenario is choosing between two paths with if/else: if (count > 2) { System.out.println("Count is higher than 2"); } else { System.out.println("Count is lower or equal than 2"); }Theoretically, we can infinitely chain or nest if/else blocks but this will hurt code readability, and that's why it's not advised. • We'll explore alternative statements in the rest of this article.
  • 7. 3. Ternary Operator • We can use the ternary operator as a shorthand expression that works like an if/else statement. • Let's see our if/else example again: • if (count > 2) { System.out.println("Count is higher than 2"); } else { System.out.println("Count is lower or equal than 2"); }We can refactor this with a ternary as follows: • System.out.println(count > 2 ? "Count is higher than 2" : "Count is lower or equal than 2");While ternary can be a great way to make our code more readable, it isn't always a good substitute for if/else.
  • 8. 4.Switch • If we have multiple cases to choose from, we can use a switch statement. • Let's again see a simple example: • int count = 3; switch (count) { case 0: System.out.println("Count is equal to 0"); break; case 1: System.out.println("Count is equal to 1"); break; default: System.out.println("Count is either negative, or higher than 1"); break; }Three or more if/else statements can be hard to read. As one of the possible workarounds, we can use switch, as seen above.
  • 9. 5.Loops • We use loops when we need to repeat the same code multiple times in succession. • Let's see a quick example of comparable for and while type of loops: • for (int i = 1; i <= 50; i++) { methodToRepeat(); } int whileCounter = 1; while (whileCounter <= 50) { methodToRepeat(); whileCounter++; } Both code blocks above will call methodToRepeat 50 times.
  • 10. 6. Break • We need to use break to exit early from a loop. • Let's see a quick example: • List<String> names = getNameList(); String name = "John Doe"; int index = 0; for ( ; index < names.length; index++) { if (names[index].equals(name)) { break; } }Here, we are looking for a name in a list of names, and we want to stop looking once we've found it. • A loop would normally go to completion, but we've used break here to short-circuit that and exit early.
  • 11. 7. Continue • Simply put, continue means to skip the rest of the loop we're in: • List<String> names = getNameList(); String name = "John Doe"; String list = ""; for (int i = 0; i < names.length; i++) { if (names[i].equals(name)) { continue; } list += names[i]; }Here, we skip appending the duplicate names into the list.
  • 12. Different control structure:  The basic Control Structures in programming languages are: Conditionals (or Selection):  which are used to execute one or more statements if a condition is met. Loops (or Iteration):  which purpose is to repeat a statement a certain number of times or while a condition is fulfilled.
  • 13. Control statements in Java: • Decision Making Statements. • Simple if statement. • if-else statement. • Nested if statement. • Switch statement. • Looping statements. • While. • Do-while.