SlideShare une entreprise Scribd logo
1  sur  31
Control
structures
University of
Human Development
Contents
Comparison operators
Control structures
 if Statements
 For Statement
 switch Statement
 while & do loop
Comparison Operators
== Tests whether two values are equal
!= Tests whether two values are not equal
< Tests if left side value is less than to the right
side value
> Tests if left side value is greater than to the right
side value
<= Tests if left side value is less than or equal to the
right side value
>= Tests if left side value is greater than or equal to
the right side value
Operators
if Statements
Syntax :
(1)
he syntax of an if statement is
if (conditional clause)
{
Statement(s) block
}
 There are three basic forms for an if statement:
public class IT
{
public static void main(String args[])
{
int x = 10;
if ( x < 20 )
{
System.out.print("This is if statement");
}
}
}
Example
Result
Use when you want to do one thing or another thing
Syntax: (2)
if (conditional clause)
{
Statement(s) block
}
else
{
Statement(s) block
}
if. Else S…
public class IT
{
public static void main(String args[])
{
int x = 30;
if( x < 20 )
{
System.out.print("This is if statement");
}
else{
System.out.print("This is else statement");
}
}
}
Example
Result
if else if statement
Syntax
if (conditional clause) (3)
{
Statement(s) block
}
else if (conditional clause)
{
Statement(s) block
}
Use when there are three or more possibilities
if statement
User input
Example
Result
User is…
The for Loop :
A for loop is a repetition control
structure that allows you to efficiently
write a loop that needs to execute a
specific number of times.
A for loop is useful when you know how
many times a task is to be repeated.
Syntax :
The syntax of a for loop is:
for (initialization; Boolean_expression; update)
{
//Statements
}
The for Loop
Here is the flow of control in a for loop:
The initialization step is executed first, and
only once. This step allows you to
declare and initialize any loop control
variables. You are not required to put a
statement here, as long as a semicolon
appears.
Next, the Boolean expression is evaluated
The for Loop
If it is true, the body of the loop is
executed. If it is false, the body of the loop
does not execute and flow of control
jumps to the next statement past the for
loop.
After the body of the for loop executes, the
flow of control jumps back up to the
update statement
The for Loop
This statement allows you to update any
loop control
variables. This statement can be left blank,
as long as a semicolon appears after
the Boolean expression.
The Boolean expression is now evaluated
again. If it is true, the loop executes
and the process repeats itself (body of loop,
then update step, then Boolean
expression). After the Boolean expression is
false, the for loop terminates.
The for Loop
Example
public class IT
{
public static void main( String args[])
{
For ( int x = 10; x < 20; x = x+1)
{
System.out.print("value of x : " + x );
System.out.print("n");
}
}
}
This would produce the following result :
Result
Switch Statement
A switch statement allows a variable to be
tested for equality against a list of values.
Each value is called a case, and the
variable being switched on is checked for
each case.
Syntax :
The syntax of enhanced for loop is:
Switch (expression) {
case value :
//Statements
break; //optional
case value :
//Statements
break; //optional
//You can have any number of case statements.
default : //Optional
//Statements }
Switch Statement
 The following rules apply to a switch statement:
 The variable used in a switch statement can
only be a byte, short, int, or char.
 You can have any number of case statements
within a switch. Each case is followed by the
value to be compared to and a colon.
 The value for a case must be the same data
type as the variable in the switch and it must be
a constant or a literal.
Switch Statement
 When the variable being switched on is equal to a case, the
statements following that case will execute until a break
statement is reached.
 When a break statement is reached, the switch terminates,
and the flow of control jumps to the next line following the
switch statement.
 Not every case needs to contain a break. If no break
appears, the flow of control will fall through to subsequent
cases until a break is reached.
 A switch statement can have an optional default case,
which must appear at the end of the switch. The default
case can be used for performing a task when none of the
cases is true. No break is needed in the default case.
Switch Statement
Example:
Result
Compile and run above program using
various command line arguments.
This would produce the following result:
While loop :
While Loop Syntax
Do - While Loop
While Loop Syntax
While Loop
REFERENCES :
http://www.homeandlearn.co.uk/java/jav
a_if_else_statements.html
http://www.tutorialspoint.com/java/java_
decision_making.htm
file:///C:/Users/DotNet/Downloads/files-
2._Lectures_8-Do_While_Loop.pdf
End

Contenu connexe

Tendances

Switch Case in C Programming
Switch Case in C ProgrammingSwitch Case in C Programming
Switch Case in C ProgrammingSonya Akter Rupa
 
The Switch Statement in java
The Switch Statement in javaThe Switch Statement in java
The Switch Statement in javaTalha Saleem
 
Switch statements in Java
Switch statements  in JavaSwitch statements  in Java
Switch statements in JavaJin Castor
 
9. statements (conditional statements)
9. statements (conditional statements)9. statements (conditional statements)
9. statements (conditional statements)Way2itech
 
Selection statements
Selection statementsSelection statements
Selection statementsHarsh Dabas
 
java programming- control statements
 java programming- control statements java programming- control statements
java programming- control statementsjyoti_lakhani
 
Control structures i
Control structures i Control structures i
Control structures i Ahmad Idrees
 
Switch statement, break statement, go to statement
Switch statement, break statement, go to statementSwitch statement, break statement, go to statement
Switch statement, break statement, go to statementRaj Parekh
 
10. switch case
10. switch case10. switch case
10. switch caseWay2itech
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statementsKuppusamy P
 
Presentation on C Switch Case Statements
Presentation on C Switch Case StatementsPresentation on C Switch Case Statements
Presentation on C Switch Case StatementsDipesh Pandey
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)Jyoti Bhardwaj
 

Tendances (20)

Decision making and looping
Decision making and loopingDecision making and looping
Decision making and looping
 
Switch Case in C Programming
Switch Case in C ProgrammingSwitch Case in C Programming
Switch Case in C Programming
 
The Switch Statement in java
The Switch Statement in javaThe Switch Statement in java
The Switch Statement in java
 
Control statements in java programmng
Control statements in java programmngControl statements in java programmng
Control statements in java programmng
 
Switch statements in Java
Switch statements  in JavaSwitch statements  in Java
Switch statements in Java
 
9. statements (conditional statements)
9. statements (conditional statements)9. statements (conditional statements)
9. statements (conditional statements)
 
Selection statements
Selection statementsSelection statements
Selection statements
 
java programming- control statements
 java programming- control statements java programming- control statements
java programming- control statements
 
LOOPS AND DECISIONS
LOOPS AND DECISIONSLOOPS AND DECISIONS
LOOPS AND DECISIONS
 
Control structures i
Control structures i Control structures i
Control structures i
 
Switch statement, break statement, go to statement
Switch statement, break statement, go to statementSwitch statement, break statement, go to statement
Switch statement, break statement, go to statement
 
10. switch case
10. switch case10. switch case
10. switch case
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
 
Java control flow statements
Java control flow statementsJava control flow statements
Java control flow statements
 
Selection structure
Selection structureSelection structure
Selection structure
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statements
 
Presentation on C Switch Case Statements
Presentation on C Switch Case StatementsPresentation on C Switch Case Statements
Presentation on C Switch Case Statements
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)
 
C# conditional branching statement
C# conditional branching statementC# conditional branching statement
C# conditional branching statement
 
Control statement in c
Control statement in cControl statement in c
Control statement in c
 

En vedette

NEWS HEADLINES June 3, 2014
NEWS HEADLINES June 3, 2014NEWS HEADLINES June 3, 2014
NEWS HEADLINES June 3, 2014Headlines.am
 
готовій план наркопоста.Docке
готовій план наркопоста.Docкеготовій план наркопоста.Docке
готовій план наркопоста.DocкеСергій Якуба
 
Матеріали до йроку 18 з всесвітньої історії 7-клас
Матеріали до йроку 18 з всесвітньої історії 7-класМатеріали до йроку 18 з всесвітньої історії 7-клас
Матеріали до йроку 18 з всесвітньої історії 7-класНадежда Бедикян
 
HONG KONG NEWS - OCTOBER 9, 2014
HONG KONG NEWS - OCTOBER 9, 2014HONG KONG NEWS - OCTOBER 9, 2014
HONG KONG NEWS - OCTOBER 9, 2014Headlines.am
 
Formative Feedback Harper Adams LT Forum
Formative Feedback Harper Adams LT ForumFormative Feedback Harper Adams LT Forum
Formative Feedback Harper Adams LT ForumRachel Forsyth
 
тиждень безпеки життєдіяльності
тиждень безпеки життєдіяльностітиждень безпеки життєдіяльності
тиждень безпеки життєдіяльностіСергій Якуба
 
щодо запобігання бездоглядності
щодо запобігання бездоглядностіщодо запобігання бездоглядності
щодо запобігання бездоглядностіСергій Якуба
 
профілактика шкідливих звичок
профілактика шкідливих звичокпрофілактика шкідливих звичок
профілактика шкідливих звичокСергій Якуба
 
Moxon Huddersfield interview questions and answers
Moxon Huddersfield interview questions and answersMoxon Huddersfield interview questions and answers
Moxon Huddersfield interview questions and answersrlykimbe
 
HEIR conference 8-9 September 2014: Forsyth and Stubbs
HEIR conference 8-9 September 2014: Forsyth and StubbsHEIR conference 8-9 September 2014: Forsyth and Stubbs
HEIR conference 8-9 September 2014: Forsyth and StubbsRachel Forsyth
 
дніпропетровщина моя рідна земля
дніпропетровщина моя рідна землядніпропетровщина моя рідна земля
дніпропетровщина моя рідна земляСергій Якуба
 

En vedette (15)

NEWS HEADLINES June 3, 2014
NEWS HEADLINES June 3, 2014NEWS HEADLINES June 3, 2014
NEWS HEADLINES June 3, 2014
 
About dexcs2014of
About dexcs2014ofAbout dexcs2014of
About dexcs2014of
 
готовій план наркопоста.Docке
готовій план наркопоста.Docкеготовій план наркопоста.Docке
готовій план наркопоста.Docке
 
Inf tema 2_urok12_6_klas
Inf tema 2_urok12_6_klasInf tema 2_urok12_6_klas
Inf tema 2_urok12_6_klas
 
Матеріали до йроку 18 з всесвітньої історії 7-клас
Матеріали до йроку 18 з всесвітньої історії 7-класМатеріали до йроку 18 з всесвітньої історії 7-клас
Матеріали до йроку 18 з всесвітньої історії 7-клас
 
HONG KONG NEWS - OCTOBER 9, 2014
HONG KONG NEWS - OCTOBER 9, 2014HONG KONG NEWS - OCTOBER 9, 2014
HONG KONG NEWS - OCTOBER 9, 2014
 
Formative Feedback Harper Adams LT Forum
Formative Feedback Harper Adams LT ForumFormative Feedback Harper Adams LT Forum
Formative Feedback Harper Adams LT Forum
 
тиждень безпеки життєдіяльності
тиждень безпеки життєдіяльностітиждень безпеки життєдіяльності
тиждень безпеки життєдіяльності
 
щодо запобігання бездоглядності
щодо запобігання бездоглядностіщодо запобігання бездоглядності
щодо запобігання бездоглядності
 
Webinar 02 2015
Webinar 02 2015Webinar 02 2015
Webinar 02 2015
 
день учителя 2014
день учителя 2014день учителя 2014
день учителя 2014
 
профілактика шкідливих звичок
профілактика шкідливих звичокпрофілактика шкідливих звичок
профілактика шкідливих звичок
 
Moxon Huddersfield interview questions and answers
Moxon Huddersfield interview questions and answersMoxon Huddersfield interview questions and answers
Moxon Huddersfield interview questions and answers
 
HEIR conference 8-9 September 2014: Forsyth and Stubbs
HEIR conference 8-9 September 2014: Forsyth and StubbsHEIR conference 8-9 September 2014: Forsyth and Stubbs
HEIR conference 8-9 September 2014: Forsyth and Stubbs
 
дніпропетровщина моя рідна земля
дніпропетровщина моя рідна землядніпропетровщина моя рідна земля
дніпропетровщина моя рідна земля
 

Similaire à Control Structures and Loops in Java Programming

Similaire à Control Structures and Loops in Java Programming (20)

Control statements
Control statementsControl statements
Control statements
 
Switch Case in C Program
Switch Case in C ProgramSwitch Case in C Program
Switch Case in C Program
 
Java loops
Java loopsJava loops
Java loops
 
Chapter 5 java
Chapter 5 javaChapter 5 java
Chapter 5 java
 
Chapter 4(1)
Chapter 4(1)Chapter 4(1)
Chapter 4(1)
 
Conditional Statement-pptx-lesson3asdfsa
Conditional Statement-pptx-lesson3asdfsaConditional Statement-pptx-lesson3asdfsa
Conditional Statement-pptx-lesson3asdfsa
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C ppt
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
 
Computer programming 2 Lesson 9
Computer programming 2  Lesson 9Computer programming 2  Lesson 9
Computer programming 2 Lesson 9
 
Computer programming 2 - Lesson 7
Computer programming 2 - Lesson 7Computer programming 2 - Lesson 7
Computer programming 2 - Lesson 7
 
BSc. III Unit iii VB.NET
BSc. III Unit iii VB.NETBSc. III Unit iii VB.NET
BSc. III Unit iii VB.NET
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
07 flow control
07   flow control07   flow control
07 flow control
 
Flow of control
Flow of controlFlow of control
Flow of control
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 
C Programming - Decision making, Looping
C  Programming - Decision making, LoopingC  Programming - Decision making, Looping
C Programming - Decision making, Looping
 
Python session3
Python session3Python session3
Python session3
 
Chapter 2 - Flow of Control Part I.pdf
Chapter 2 -  Flow of Control Part I.pdfChapter 2 -  Flow of Control Part I.pdf
Chapter 2 - Flow of Control Part I.pdf
 
Control structures in C
Control structures in CControl structures in C
Control structures in C
 
Selection Statements in C Programming
Selection Statements in C ProgrammingSelection Statements in C Programming
Selection Statements in C Programming
 

Dernier

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
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
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
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
 
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
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
[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
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
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
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
🐬 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
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
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
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 

Dernier (20)

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
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...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
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
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
[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
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
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...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
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...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 

Control Structures and Loops in Java Programming

  • 2. Contents Comparison operators Control structures  if Statements  For Statement  switch Statement  while & do loop
  • 3. Comparison Operators == Tests whether two values are equal != Tests whether two values are not equal < Tests if left side value is less than to the right side value > Tests if left side value is greater than to the right side value <= Tests if left side value is less than or equal to the right side value >= Tests if left side value is greater than or equal to the right side value Operators
  • 4. if Statements Syntax : (1) he syntax of an if statement is if (conditional clause) { Statement(s) block }  There are three basic forms for an if statement:
  • 5. public class IT { public static void main(String args[]) { int x = 10; if ( x < 20 ) { System.out.print("This is if statement"); } } } Example
  • 7. Use when you want to do one thing or another thing Syntax: (2) if (conditional clause) { Statement(s) block } else { Statement(s) block } if. Else S…
  • 8. public class IT { public static void main(String args[]) { int x = 30; if( x < 20 ) { System.out.print("This is if statement"); } else{ System.out.print("This is else statement"); } } } Example
  • 10. if else if statement Syntax if (conditional clause) (3) { Statement(s) block } else if (conditional clause) { Statement(s) block } Use when there are three or more possibilities if statement
  • 13. The for Loop : A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times. A for loop is useful when you know how many times a task is to be repeated.
  • 14. Syntax : The syntax of a for loop is: for (initialization; Boolean_expression; update) { //Statements } The for Loop
  • 15. Here is the flow of control in a for loop: The initialization step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears. Next, the Boolean expression is evaluated The for Loop
  • 16. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement past the for loop. After the body of the for loop executes, the flow of control jumps back up to the update statement The for Loop
  • 17. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the Boolean expression. The Boolean expression is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then update step, then Boolean expression). After the Boolean expression is false, the for loop terminates. The for Loop
  • 18. Example public class IT { public static void main( String args[]) { For ( int x = 10; x < 20; x = x+1) { System.out.print("value of x : " + x ); System.out.print("n"); } } }
  • 19. This would produce the following result : Result
  • 20. Switch Statement A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.
  • 21. Syntax : The syntax of enhanced for loop is: Switch (expression) { case value : //Statements break; //optional case value : //Statements break; //optional //You can have any number of case statements. default : //Optional //Statements } Switch Statement
  • 22.  The following rules apply to a switch statement:  The variable used in a switch statement can only be a byte, short, int, or char.  You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.  The value for a case must be the same data type as the variable in the switch and it must be a constant or a literal. Switch Statement
  • 23.  When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.  When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.  Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.  A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case. Switch Statement
  • 25. Result Compile and run above program using various command line arguments. This would produce the following result:
  • 28. Do - While Loop
  • 31. End