SlideShare a Scribd company logo
1 of 28
Modern Programming Languages-
Control Statement
Java - Control Statements
Same as in C++ !
A program executes from top to bottom except when we use
control statements, we can control the order of execution of the
program, based on logic and values.
In Java, control statements can be divided into the following
three categories:
 Selection Statements
 Iteration Statements
 Jump Statements
Java - Control Statements
In Java, control statements can be divided into the following
three categories:
 Selection Statements
 Iteration Statements
 Jump Statements
Java – Control Statements
Selection Statements
• The if statements
• The if-else statements
• The if-else-if statements
• The switch statements
Java – Control Statements
If Statements
Java – Control Statements
Scanner !
Java – Control Statements
If else Statements
Java – Control Statements
rt Hand If...Else (Ternary Operator)
Java – Control Statements
If else If Statements
Java – Control Statements
Switch Statements The switch statement is a multi-way branch statement.
Java – Control Statements
Switch Statements The switch statement is a multi-way branch statement.
Java - Control Statements
In Java, control statements can be divided into the following
three categories:
 Selection Statements
 Iteration Statements
 Jump Statements
Java – Control Statements
Iteration Statements
Repeating the same code fragment several times until a
specified condition is satisfied is called iteration
Java provides the following loop for iteration statements:
• The while loop
• The for loop
• The do-while loop
• The for each loop
Java – Control Statements
The While Loop - It continually executes a statement (that is usually be a block)
while a condition is true. The condition must return a Boolean value.
Output Do not forget to increment the
variable used in the condition,
otherwise the loop will never end!
Java – Control Statements
The for loop - A for loop executes a statement (that is usually a block) as long as the
Boolean condition evaluates to true.
• Statement 1 is executed (one time) before the execution
of the code block.
• Statement 2 defines the condition for executing the code
block.
• Statement 3 is executed (every time) after the code block
has been executed.
Java – Control Statements
The For loop
Statement 1 sets a variable before the loop starts (int i = 0).
Statement 2 defines the condition for the loop to run (i must be
less than 5). If the condition is true, the loop will start over again, if
it is false, the loop will end.
Statement 3 increases a value (i++) each time the code block in
the loop has been executed.
Java – Control Statements
The For each loop
This was introduced in Java 5. This loop is basically used to
traverse the array or collection elements.
Java – Control Statements
The For each loop
This was introduced in Java 5. This loop is basically used to
traverse the array or collection elements.
Java – Control Statements
The For each loop
Java - Control Statements
In Java, control statements can be divided into the following
three categories:
 Selection Statements
 Iteration Statements
 Jump Statements
Java - Control Statements
Jump statements are used to unconditionally transfer the
program control to another part of the program.
Java provides the following jump statements:
• break statement
• continue statement
• return statement
Java - Control Statements
Break Statement
The break statement immediately quits the current iteration
and goes to the first statement following the loop.
The break statement has the following two forms:
• Labeled Break Statement
• Unlabeled Break Statement
Unlabeled Break Statement: This is used to jump program
control out of the specific loop on the specific condition.
Java - Control Statements
Output !
Java - Control Statements
Output !
Labeled Break Statement: This is used when we want to jump the
program control out of nested loops or multiple loops.
Java - Control Statements
Continue Statement
The continue statement is used when you want to continue running
the loop with the next iteration and want to skip the rest of the
statements of the body for the current iteration.
The continue statement has the following two forms:
• Labeled Continue Statement
• Unlabeled Continue Statement
Unlabeled Continue Statement: This statement skips the current
iteration of the innermost for, while and do-while loop.
Java - Control Statements
Output !
Java - Control Statements
Output !
Labeled Continue Statement: This statement skips the current
iteration of the loop with the specified label.
Java - Control Statements
Return Statement - The return statement is used to immediately
quit the current method and return to the calling method. It is
mandatory to use a return statement for non-void methods to
return a value.
// a method for computing the area of the rectangle
public int getArea()
{
return width * height;
}

More Related Content

Similar to Modern Programming Languages - Java Control Statements Explained

Chapter05-Control Structures.pptx
Chapter05-Control Structures.pptxChapter05-Control Structures.pptx
Chapter05-Control Structures.pptxAdrianVANTOPINA
 
Presentation of control statement
Presentation of control statement  Presentation of control statement
Presentation of control statement Bharat Rathore
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structuresayshasafdarwaada
 
OCA JAVA - 2 Programming with Java Statements
 OCA JAVA - 2 Programming with Java Statements OCA JAVA - 2 Programming with Java Statements
OCA JAVA - 2 Programming with Java StatementsFernando Gil
 
Java learning
Java learningJava learning
Java learningmoew3
 
Introduction To Programming with Python Lecture 2
Introduction To Programming with Python Lecture 2Introduction To Programming with Python Lecture 2
Introduction To Programming with Python Lecture 2Syed Farjad Zia Zaidi
 
Lecture - 5 Control Statement
Lecture - 5 Control StatementLecture - 5 Control Statement
Lecture - 5 Control Statementmanish kumar
 
Control structures
Control structuresControl structures
Control structuresGehad Enayat
 
Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Deepak Singh
 
Break, continue and return
Break, continue and return Break, continue and return
Break, continue and return Jadavsejal
 
Python Programming | JNTUK | UNIT 2 | Lecture 6 & 7 | Conditional & Control S...
Python Programming | JNTUK | UNIT 2 | Lecture 6 & 7 | Conditional & Control S...Python Programming | JNTUK | UNIT 2 | Lecture 6 & 7 | Conditional & Control S...
Python Programming | JNTUK | UNIT 2 | Lecture 6 & 7 | Conditional & Control S...FabMinds
 

Similar to Modern Programming Languages - Java Control Statements Explained (20)

Chapter05-Control Structures.pptx
Chapter05-Control Structures.pptxChapter05-Control Structures.pptx
Chapter05-Control Structures.pptx
 
22H51A6755.pptx
22H51A6755.pptx22H51A6755.pptx
22H51A6755.pptx
 
chapter 6.pptx
chapter 6.pptxchapter 6.pptx
chapter 6.pptx
 
Presentation of control statement
Presentation of control statement  Presentation of control statement
Presentation of control statement
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structures
 
C language (Part 2)
C language (Part 2)C language (Part 2)
C language (Part 2)
 
OCA JAVA - 2 Programming with Java Statements
 OCA JAVA - 2 Programming with Java Statements OCA JAVA - 2 Programming with Java Statements
OCA JAVA - 2 Programming with Java Statements
 
8 statement level
8 statement level8 statement level
8 statement level
 
Java learning
Java learningJava learning
Java learning
 
Introduction To Programming with Python Lecture 2
Introduction To Programming with Python Lecture 2Introduction To Programming with Python Lecture 2
Introduction To Programming with Python Lecture 2
 
Javasession4
Javasession4Javasession4
Javasession4
 
Comp ppt (1)
Comp ppt (1)Comp ppt (1)
Comp ppt (1)
 
Lecture - 5 Control Statement
Lecture - 5 Control StatementLecture - 5 Control Statement
Lecture - 5 Control Statement
 
Loops
LoopsLoops
Loops
 
Control structures
Control structuresControl structures
Control structures
 
Chapter 9 - Loops in C++
Chapter 9 - Loops in C++Chapter 9 - Loops in C++
Chapter 9 - Loops in C++
 
Break, continue and return
Break, continue and return Break, continue and return
Break, continue and return
 
C language 2
C language 2C language 2
C language 2
 
Decision making and looping
Decision making and loopingDecision making and looping
Decision making and looping
 
Python Programming | JNTUK | UNIT 2 | Lecture 6 & 7 | Conditional & Control S...
Python Programming | JNTUK | UNIT 2 | Lecture 6 & 7 | Conditional & Control S...Python Programming | JNTUK | UNIT 2 | Lecture 6 & 7 | Conditional & Control S...
Python Programming | JNTUK | UNIT 2 | Lecture 6 & 7 | Conditional & Control S...
 

Recently uploaded

Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 

Recently uploaded (20)

Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 

Modern Programming Languages - Java Control Statements Explained

  • 2. Java - Control Statements Same as in C++ ! A program executes from top to bottom except when we use control statements, we can control the order of execution of the program, based on logic and values. In Java, control statements can be divided into the following three categories:  Selection Statements  Iteration Statements  Jump Statements
  • 3. Java - Control Statements In Java, control statements can be divided into the following three categories:  Selection Statements  Iteration Statements  Jump Statements
  • 4. Java – Control Statements Selection Statements • The if statements • The if-else statements • The if-else-if statements • The switch statements
  • 5. Java – Control Statements If Statements
  • 6. Java – Control Statements Scanner !
  • 7. Java – Control Statements If else Statements
  • 8. Java – Control Statements rt Hand If...Else (Ternary Operator)
  • 9. Java – Control Statements If else If Statements
  • 10. Java – Control Statements Switch Statements The switch statement is a multi-way branch statement.
  • 11. Java – Control Statements Switch Statements The switch statement is a multi-way branch statement.
  • 12. Java - Control Statements In Java, control statements can be divided into the following three categories:  Selection Statements  Iteration Statements  Jump Statements
  • 13. Java – Control Statements Iteration Statements Repeating the same code fragment several times until a specified condition is satisfied is called iteration Java provides the following loop for iteration statements: • The while loop • The for loop • The do-while loop • The for each loop
  • 14. Java – Control Statements The While Loop - It continually executes a statement (that is usually be a block) while a condition is true. The condition must return a Boolean value. Output Do not forget to increment the variable used in the condition, otherwise the loop will never end!
  • 15. Java – Control Statements The for loop - A for loop executes a statement (that is usually a block) as long as the Boolean condition evaluates to true. • Statement 1 is executed (one time) before the execution of the code block. • Statement 2 defines the condition for executing the code block. • Statement 3 is executed (every time) after the code block has been executed.
  • 16. Java – Control Statements The For loop Statement 1 sets a variable before the loop starts (int i = 0). Statement 2 defines the condition for the loop to run (i must be less than 5). If the condition is true, the loop will start over again, if it is false, the loop will end. Statement 3 increases a value (i++) each time the code block in the loop has been executed.
  • 17. Java – Control Statements The For each loop This was introduced in Java 5. This loop is basically used to traverse the array or collection elements.
  • 18. Java – Control Statements The For each loop This was introduced in Java 5. This loop is basically used to traverse the array or collection elements.
  • 19. Java – Control Statements The For each loop
  • 20. Java - Control Statements In Java, control statements can be divided into the following three categories:  Selection Statements  Iteration Statements  Jump Statements
  • 21. Java - Control Statements Jump statements are used to unconditionally transfer the program control to another part of the program. Java provides the following jump statements: • break statement • continue statement • return statement
  • 22. Java - Control Statements Break Statement The break statement immediately quits the current iteration and goes to the first statement following the loop. The break statement has the following two forms: • Labeled Break Statement • Unlabeled Break Statement Unlabeled Break Statement: This is used to jump program control out of the specific loop on the specific condition.
  • 23. Java - Control Statements Output !
  • 24. Java - Control Statements Output ! Labeled Break Statement: This is used when we want to jump the program control out of nested loops or multiple loops.
  • 25. Java - Control Statements Continue Statement The continue statement is used when you want to continue running the loop with the next iteration and want to skip the rest of the statements of the body for the current iteration. The continue statement has the following two forms: • Labeled Continue Statement • Unlabeled Continue Statement Unlabeled Continue Statement: This statement skips the current iteration of the innermost for, while and do-while loop.
  • 26. Java - Control Statements Output !
  • 27. Java - Control Statements Output ! Labeled Continue Statement: This statement skips the current iteration of the loop with the specified label.
  • 28. Java - Control Statements Return Statement - The return statement is used to immediately quit the current method and return to the calling method. It is mandatory to use a return statement for non-void methods to return a value. // a method for computing the area of the rectangle public int getArea() { return width * height; }