SlideShare una empresa de Scribd logo
1 de 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

Más contenido relacionado

La actualidad más candente

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
 

La actualidad más candente (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
 

Destacado

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
 
дніпропетровщина моя рідна земля
дніпропетровщина моя рідна землядніпропетровщина моя рідна земля
дніпропетровщина моя рідна земляСергій Якуба
 

Destacado (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
 
дніпропетровщина моя рідна земля
дніпропетровщина моя рідна землядніпропетровщина моя рідна земля
дніпропетровщина моя рідна земля
 

Similar a Control Structures and Loops in Java Programming

Similar a 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
 

Último

What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
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
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
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
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
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
 
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
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...AliaaTarek5
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...panagenda
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
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
 

Último (20)

What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
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
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
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
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
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
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
Why device, WIFI, and ISP insights are crucial to supporting remote Microsoft...
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
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
 

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