SlideShare a Scribd company logo
SWE-103T Object Oriented
Programming
Instructors:
Engr. Anila Saghir
Software Engineering Department
5/22/2022 SWE-103T OOP SED,SSUET 1
Week 04
5/22/2022 SWE-103T OOP SED,SSUET 2
Chapter #5
Loops
Chapter 5 introduces the concept of loops,, the loop design strategy to develop
loops, To control a loop with a sentinel value, a while loop, for loops, do while loop,
the similarities and differences between three types of loop statements, nested
loops, program control with break and continue
5/22/2022 SWE-103T OOP SED,SSUET 3
Introduction
โ€ข Loops are constructs that control repeated executions of a block of
statements. Looping in programming languages is a feature which
facilitates the execution of a set of instructions/functions
repeatedly while some condition evaluates to true.
โ€ข Java provides three types of loop statements: while loops, do-while
loops, and for loops.
โ€ข While all the ways provide similar basic functionality, they differ in
their syntax and condition checking time.
A loop can be used to tell a program to execute statements
repeatedly
5/22/2022 SWE-103T OOP SED,SSUET 4
The while Loop
5/22/2022 SWE-103T OOP SED,SSUET
โ€ข A while loop executes statements repeatedly while the condition is
true.
โ€ข The syntax for the while loop is:
while (loop-continuation-condition) {
// Loop body
Statement(s);
}
5
Example
5/22/2022 SWE-103T OOP SED,SSUET 6
Loop Design Strategies
โ€ข Writing a correct loop is not an easy task for novice programmers. Consider three steps when
writing a loop.
โ€ข Step 1: Identify the statements that need to be repeated.
โ€ข Step 2: Wrap these statements in a loop like this:
while (true) {
Statements;
}
โ€ข Step 3: Code the loop-continuation-condition & add appropriate statements to control the loop.
while (loop-continuation-condition) {
Statements;
Additional statements for controlling the loop;
}
5/22/2022 SWE-103T OOP SED,SSUET 7
โ€ข Example: Ask the user to generate table of 2.
5/22/2022 SWE-103T OOP SED,SSUET 8
Controlling a Loop with a Sentinel Value
โ€ข Another common technique for controlling a loop is to designate a
special value when reading and processing a set of values
โ€ข This special input value, known as a sentinel value, signifies the
end of the input.
โ€ข A loop that uses a sentinel value to control its execution is called a
sentinel-controlled loop
5/22/2022 SWE-103T OOP SED,SSUET 9
โ€ข Example: Ask the user to enter input until he/she enters 0.
5/22/2022 SWE-103T OOP SED,SSUET 10
Do while loop
โ€ข A do-while loop is the same as a while loop except that it executes
the loop body first and then checks the loop continuation
condition.
โ€ข The do-while loop is a variation of the while loop. Its syntax is:
do {
// Loop body;
Statement(s);
} while (loop-continuation-condition);
5/22/2022 SWE-103T OOP SED,SSUET 11
โ€ข Example:
import java.util.Scanner;
public class DoWhileProgram {
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
System.out.println("Enter five inputs: ");
int count=1;
do{
System.out.println("Enter "+ count+ (" input: "));
int rollNo=input.nextInt();
System.out.println("The "+ count +" input value is: "+ rollNo );
count++;
}while(count<=5);
}
}
5/22/2022 SWE-103T OOP SED,SSUET 12
Difference between while and do-while
loops
โ€ข The difference between a while loop and a do-while loop is the
order in which the loop-continuation-condition is evaluated and
the loop body executed.
โ€ข We can write a loop using either the while loop or the do-while
loop
5/22/2022 SWE-103T OOP SED,SSUET 13
For Loop
โ€ข A for loop has a concise syntax for writing loops. Often we write a
while loop in the following common form:
i = initialValue; // Initialize loop control variable
while (i < endValue){
// Loop body
i++; /
โ€ข A for loop can be used to simplify the preceding loop as:
for (i = initialValue; i < endValue; i++){
// Loop body
}
5/22/2022 SWE-103T OOP SED,SSUET 14
For Loop Syntax
for (initial-action; loop-cont-cond; action-after-each-iteration)
{
// Loop body;
Statement(s);
}
5/22/2022 SWE-103T OOP SED,SSUET 15
Example 1:
โ€ข Ask the user to take 5 inputs.
5/22/2022 SWE-103T OOP SED,SSUET 16
Example 2 Generate n multiples of 2.
import java.util.Scanner;
public class ForloopProgram {
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
System.out.println("Enter value of n: ");
int multiples, n=input.nextInt();
System.out.println("The multiples of 2 are: ");
for(int i=1; i<=n;i++ ){
multiples=2*i;
System.out.print( multiples+" " );
}
}
}
5/22/2022 SWE-103T OOP SED,SSUET 17
Tasks
โ€ข Calculate the factorial of the number.
โ€ข Calculate sum of the numbers from 1 to n
5/22/2022 SWE-103T OOP SED,SSUET 18

More Related Content

Similar to Conditional Statements

2nd year computer science chapter 12 notes
2nd year computer science chapter 12 notes2nd year computer science chapter 12 notes
2nd year computer science chapter 12 notes
muhammadFaheem656405
ย 
Chapter 12 Computer Science ( ICS 12).pdf
Chapter 12 Computer Science ( ICS 12).pdfChapter 12 Computer Science ( ICS 12).pdf
Chapter 12 Computer Science ( ICS 12).pdf
AamirShahzad527024
ย 
Cs1123 6 loops
Cs1123 6 loopsCs1123 6 loops
Cs1123 6 loops
TAlha MAlik
ย 
Basics of Java
Basics of JavaBasics of Java
Basics of Java
Engr Saghir
ย 
1.My Presentation.pptx
1.My Presentation.pptx1.My Presentation.pptx
1.My Presentation.pptx
ArslanAliArslanAli
ย 
Loop-1.pptx
Loop-1.pptxLoop-1.pptx
Loop-1.pptx
MuhammadArsalan896347
ย 
prt123.pptx
prt123.pptxprt123.pptx
prt123.pptx
ASADKS
ย 
Algorithm-RepetitionSentinellNestedLoop_Solution.pptx
Algorithm-RepetitionSentinellNestedLoop_Solution.pptxAlgorithm-RepetitionSentinellNestedLoop_Solution.pptx
Algorithm-RepetitionSentinellNestedLoop_Solution.pptx
AliaaAqilah3
ย 
Object oriented programming18 control structures looping
Object oriented programming18 control structures loopingObject oriented programming18 control structures looping
Object oriented programming18 control structures looping
Vaibhav Khanna
ย 
Java Chapter 05 - Conditions & Loops: part 5
Java Chapter 05 - Conditions & Loops: part 5Java Chapter 05 - Conditions & Loops: part 5
Java Chapter 05 - Conditions & Loops: part 5
DanWooster1
ย 
Loops in C.pptx
Loops in C.pptxLoops in C.pptx
Loops in C.pptx
nagalakshmig4
ย 
C language (Part 2)
C language (Part 2)C language (Part 2)
C language (Part 2)
SURBHI SAROHA
ย 
Survey on Analysing and Optimizing the LLVM Divergence Analysis
Survey on Analysing and Optimizing the LLVM Divergence AnalysisSurvey on Analysing and Optimizing the LLVM Divergence Analysis
Survey on Analysing and Optimizing the LLVM Divergence Analysis
IRJET Journal
ย 
Loop structures
Loop structuresLoop structures
Loop structures
tazeem sana
ย 
DESIGN AND IMPLEMENTATION OF AREA AND POWER OPTIMISED NOVEL SCANFLOP
DESIGN AND IMPLEMENTATION OF AREA AND POWER OPTIMISED NOVEL SCANFLOPDESIGN AND IMPLEMENTATION OF AREA AND POWER OPTIMISED NOVEL SCANFLOP
DESIGN AND IMPLEMENTATION OF AREA AND POWER OPTIMISED NOVEL SCANFLOP
VLSICS Design
ย 
01 oracle architecture
01 oracle architecture01 oracle architecture
01 oracle architecture
Smitha Padmanabhan
ย 
neiljaysonching
neiljaysonchingneiljaysonching
neiljaysonching
Jlien Ching
ย 
Loc, los and loes at speed testing methodologies for automatic test pattern g...
Loc, los and loes at speed testing methodologies for automatic test pattern g...Loc, los and loes at speed testing methodologies for automatic test pattern g...
Loc, los and loes at speed testing methodologies for automatic test pattern g...
eSAT Publishing House
ย 
Loc, los and loes at speed testing methodologies for automatic test pattern g...
Loc, los and loes at speed testing methodologies for automatic test pattern g...Loc, los and loes at speed testing methodologies for automatic test pattern g...
Loc, los and loes at speed testing methodologies for automatic test pattern g...
eSAT Journals
ย 
Adii RDBMS.pptxyfufufufufifigigigiffufufututit
Adii RDBMS.pptxyfufufufufifigigigiffufufututitAdii RDBMS.pptxyfufufufufifigigigiffufufututit
Adii RDBMS.pptxyfufufufufifigigigiffufufututit
adityashinde1567
ย 

Similar to Conditional Statements (20)

2nd year computer science chapter 12 notes
2nd year computer science chapter 12 notes2nd year computer science chapter 12 notes
2nd year computer science chapter 12 notes
ย 
Chapter 12 Computer Science ( ICS 12).pdf
Chapter 12 Computer Science ( ICS 12).pdfChapter 12 Computer Science ( ICS 12).pdf
Chapter 12 Computer Science ( ICS 12).pdf
ย 
Cs1123 6 loops
Cs1123 6 loopsCs1123 6 loops
Cs1123 6 loops
ย 
Basics of Java
Basics of JavaBasics of Java
Basics of Java
ย 
1.My Presentation.pptx
1.My Presentation.pptx1.My Presentation.pptx
1.My Presentation.pptx
ย 
Loop-1.pptx
Loop-1.pptxLoop-1.pptx
Loop-1.pptx
ย 
prt123.pptx
prt123.pptxprt123.pptx
prt123.pptx
ย 
Algorithm-RepetitionSentinellNestedLoop_Solution.pptx
Algorithm-RepetitionSentinellNestedLoop_Solution.pptxAlgorithm-RepetitionSentinellNestedLoop_Solution.pptx
Algorithm-RepetitionSentinellNestedLoop_Solution.pptx
ย 
Object oriented programming18 control structures looping
Object oriented programming18 control structures loopingObject oriented programming18 control structures looping
Object oriented programming18 control structures looping
ย 
Java Chapter 05 - Conditions & Loops: part 5
Java Chapter 05 - Conditions & Loops: part 5Java Chapter 05 - Conditions & Loops: part 5
Java Chapter 05 - Conditions & Loops: part 5
ย 
Loops in C.pptx
Loops in C.pptxLoops in C.pptx
Loops in C.pptx
ย 
C language (Part 2)
C language (Part 2)C language (Part 2)
C language (Part 2)
ย 
Survey on Analysing and Optimizing the LLVM Divergence Analysis
Survey on Analysing and Optimizing the LLVM Divergence AnalysisSurvey on Analysing and Optimizing the LLVM Divergence Analysis
Survey on Analysing and Optimizing the LLVM Divergence Analysis
ย 
Loop structures
Loop structuresLoop structures
Loop structures
ย 
DESIGN AND IMPLEMENTATION OF AREA AND POWER OPTIMISED NOVEL SCANFLOP
DESIGN AND IMPLEMENTATION OF AREA AND POWER OPTIMISED NOVEL SCANFLOPDESIGN AND IMPLEMENTATION OF AREA AND POWER OPTIMISED NOVEL SCANFLOP
DESIGN AND IMPLEMENTATION OF AREA AND POWER OPTIMISED NOVEL SCANFLOP
ย 
01 oracle architecture
01 oracle architecture01 oracle architecture
01 oracle architecture
ย 
neiljaysonching
neiljaysonchingneiljaysonching
neiljaysonching
ย 
Loc, los and loes at speed testing methodologies for automatic test pattern g...
Loc, los and loes at speed testing methodologies for automatic test pattern g...Loc, los and loes at speed testing methodologies for automatic test pattern g...
Loc, los and loes at speed testing methodologies for automatic test pattern g...
ย 
Loc, los and loes at speed testing methodologies for automatic test pattern g...
Loc, los and loes at speed testing methodologies for automatic test pattern g...Loc, los and loes at speed testing methodologies for automatic test pattern g...
Loc, los and loes at speed testing methodologies for automatic test pattern g...
ย 
Adii RDBMS.pptxyfufufufufifigigigiffufufututit
Adii RDBMS.pptxyfufufufufifigigigiffufufututitAdii RDBMS.pptxyfufufufufifigigigiffufufututit
Adii RDBMS.pptxyfufufufufifigigigiffufufututit
ย 

Recently uploaded

Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
Jyoti Chand
ย 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
MJDuyan
ย 
ZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptxZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptx
dot55audits
ย 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
Krassimira Luka
ย 
B. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdfB. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdf
BoudhayanBhattachari
ย 
Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47
MysoreMuleSoftMeetup
ย 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
ย 
Constructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective CommunicationConstructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective Communication
Chevonnese Chevers Whyte, MBA, B.Sc.
ย 
MARY JANE WILSON, A โ€œBOA MรƒEโ€ .
MARY JANE WILSON, A โ€œBOA MรƒEโ€           .MARY JANE WILSON, A โ€œBOA MรƒEโ€           .
MARY JANE WILSON, A โ€œBOA MรƒEโ€ .
Colรฉgio Santa Teresinha
ย 
Bร€I TแบฌP Bแป” TRแปข TIแบพNG ANH 8 Cแบข Nฤ‚M - GLOBAL SUCCESS - Nฤ‚M HแปŒC 2023-2024 (Cร“ FI...
Bร€I TแบฌP Bแป” TRแปข TIแบพNG ANH 8 Cแบข Nฤ‚M - GLOBAL SUCCESS - Nฤ‚M HแปŒC 2023-2024 (Cร“ FI...Bร€I TแบฌP Bแป” TRแปข TIแบพNG ANH 8 Cแบข Nฤ‚M - GLOBAL SUCCESS - Nฤ‚M HแปŒC 2023-2024 (Cร“ FI...
Bร€I TแบฌP Bแป” TRแปข TIแบพNG ANH 8 Cแบข Nฤ‚M - GLOBAL SUCCESS - Nฤ‚M HแปŒC 2023-2024 (Cร“ FI...
Nguyen Thanh Tu Collection
ย 
IGCSE Biology Chapter 14- Reproduction in Plants.pdf
IGCSE Biology Chapter 14- Reproduction in Plants.pdfIGCSE Biology Chapter 14- Reproduction in Plants.pdf
IGCSE Biology Chapter 14- Reproduction in Plants.pdf
Amin Marwan
ย 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
ย 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
ย 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Denish Jangid
ย 
Bร€I TแบฌP Bแป” TRแปข TIแบพNG ANH LแปšP 9 Cแบข Nฤ‚M - GLOBAL SUCCESS - Nฤ‚M HแปŒC 2024-2025 - ...
Bร€I TแบฌP Bแป” TRแปข TIแบพNG ANH LแปšP 9 Cแบข Nฤ‚M - GLOBAL SUCCESS - Nฤ‚M HแปŒC 2024-2025 - ...Bร€I TแบฌP Bแป” TRแปข TIแบพNG ANH LแปšP 9 Cแบข Nฤ‚M - GLOBAL SUCCESS - Nฤ‚M HแปŒC 2024-2025 - ...
Bร€I TแบฌP Bแป” TRแปข TIแบพNG ANH LแปšP 9 Cแบข Nฤ‚M - GLOBAL SUCCESS - Nฤ‚M HแปŒC 2024-2025 - ...
Nguyen Thanh Tu Collection
ย 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
ย 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
ย 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
ย 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
ย 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
imrankhan141184
ย 

Recently uploaded (20)

Wound healing PPT
Wound healing PPTWound healing PPT
Wound healing PPT
ย 
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) CurriculumPhilippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
Philippine Edukasyong Pantahanan at Pangkabuhayan (EPP) Curriculum
ย 
ZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptxZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptx
ย 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
ย 
B. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdfB. Ed Syllabus for babasaheb ambedkar education university.pdf
B. Ed Syllabus for babasaheb ambedkar education university.pdf
ย 
Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47
ย 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
ย 
Constructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective CommunicationConstructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective Communication
ย 
MARY JANE WILSON, A โ€œBOA MรƒEโ€ .
MARY JANE WILSON, A โ€œBOA MรƒEโ€           .MARY JANE WILSON, A โ€œBOA MรƒEโ€           .
MARY JANE WILSON, A โ€œBOA MรƒEโ€ .
ย 
Bร€I TแบฌP Bแป” TRแปข TIแบพNG ANH 8 Cแบข Nฤ‚M - GLOBAL SUCCESS - Nฤ‚M HแปŒC 2023-2024 (Cร“ FI...
Bร€I TแบฌP Bแป” TRแปข TIแบพNG ANH 8 Cแบข Nฤ‚M - GLOBAL SUCCESS - Nฤ‚M HแปŒC 2023-2024 (Cร“ FI...Bร€I TแบฌP Bแป” TRแปข TIแบพNG ANH 8 Cแบข Nฤ‚M - GLOBAL SUCCESS - Nฤ‚M HแปŒC 2023-2024 (Cร“ FI...
Bร€I TแบฌP Bแป” TRแปข TIแบพNG ANH 8 Cแบข Nฤ‚M - GLOBAL SUCCESS - Nฤ‚M HแปŒC 2023-2024 (Cร“ FI...
ย 
IGCSE Biology Chapter 14- Reproduction in Plants.pdf
IGCSE Biology Chapter 14- Reproduction in Plants.pdfIGCSE Biology Chapter 14- Reproduction in Plants.pdf
IGCSE Biology Chapter 14- Reproduction in Plants.pdf
ย 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
ย 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
ย 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
ย 
Bร€I TแบฌP Bแป” TRแปข TIแบพNG ANH LแปšP 9 Cแบข Nฤ‚M - GLOBAL SUCCESS - Nฤ‚M HแปŒC 2024-2025 - ...
Bร€I TแบฌP Bแป” TRแปข TIแบพNG ANH LแปšP 9 Cแบข Nฤ‚M - GLOBAL SUCCESS - Nฤ‚M HแปŒC 2024-2025 - ...Bร€I TแบฌP Bแป” TRแปข TIแบพNG ANH LแปšP 9 Cแบข Nฤ‚M - GLOBAL SUCCESS - Nฤ‚M HแปŒC 2024-2025 - ...
Bร€I TแบฌP Bแป” TRแปข TIแบพNG ANH LแปšP 9 Cแบข Nฤ‚M - GLOBAL SUCCESS - Nฤ‚M HแปŒC 2024-2025 - ...
ย 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
ย 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
ย 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
ย 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
ย 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
ย 

Conditional Statements

  • 1. SWE-103T Object Oriented Programming Instructors: Engr. Anila Saghir Software Engineering Department 5/22/2022 SWE-103T OOP SED,SSUET 1
  • 2. Week 04 5/22/2022 SWE-103T OOP SED,SSUET 2
  • 3. Chapter #5 Loops Chapter 5 introduces the concept of loops,, the loop design strategy to develop loops, To control a loop with a sentinel value, a while loop, for loops, do while loop, the similarities and differences between three types of loop statements, nested loops, program control with break and continue 5/22/2022 SWE-103T OOP SED,SSUET 3
  • 4. Introduction โ€ข Loops are constructs that control repeated executions of a block of statements. Looping in programming languages is a feature which facilitates the execution of a set of instructions/functions repeatedly while some condition evaluates to true. โ€ข Java provides three types of loop statements: while loops, do-while loops, and for loops. โ€ข While all the ways provide similar basic functionality, they differ in their syntax and condition checking time. A loop can be used to tell a program to execute statements repeatedly 5/22/2022 SWE-103T OOP SED,SSUET 4
  • 5. The while Loop 5/22/2022 SWE-103T OOP SED,SSUET โ€ข A while loop executes statements repeatedly while the condition is true. โ€ข The syntax for the while loop is: while (loop-continuation-condition) { // Loop body Statement(s); } 5
  • 7. Loop Design Strategies โ€ข Writing a correct loop is not an easy task for novice programmers. Consider three steps when writing a loop. โ€ข Step 1: Identify the statements that need to be repeated. โ€ข Step 2: Wrap these statements in a loop like this: while (true) { Statements; } โ€ข Step 3: Code the loop-continuation-condition & add appropriate statements to control the loop. while (loop-continuation-condition) { Statements; Additional statements for controlling the loop; } 5/22/2022 SWE-103T OOP SED,SSUET 7
  • 8. โ€ข Example: Ask the user to generate table of 2. 5/22/2022 SWE-103T OOP SED,SSUET 8
  • 9. Controlling a Loop with a Sentinel Value โ€ข Another common technique for controlling a loop is to designate a special value when reading and processing a set of values โ€ข This special input value, known as a sentinel value, signifies the end of the input. โ€ข A loop that uses a sentinel value to control its execution is called a sentinel-controlled loop 5/22/2022 SWE-103T OOP SED,SSUET 9
  • 10. โ€ข Example: Ask the user to enter input until he/she enters 0. 5/22/2022 SWE-103T OOP SED,SSUET 10
  • 11. Do while loop โ€ข A do-while loop is the same as a while loop except that it executes the loop body first and then checks the loop continuation condition. โ€ข The do-while loop is a variation of the while loop. Its syntax is: do { // Loop body; Statement(s); } while (loop-continuation-condition); 5/22/2022 SWE-103T OOP SED,SSUET 11
  • 12. โ€ข Example: import java.util.Scanner; public class DoWhileProgram { public static void main(String[] args) { Scanner input= new Scanner(System.in); System.out.println("Enter five inputs: "); int count=1; do{ System.out.println("Enter "+ count+ (" input: ")); int rollNo=input.nextInt(); System.out.println("The "+ count +" input value is: "+ rollNo ); count++; }while(count<=5); } } 5/22/2022 SWE-103T OOP SED,SSUET 12
  • 13. Difference between while and do-while loops โ€ข The difference between a while loop and a do-while loop is the order in which the loop-continuation-condition is evaluated and the loop body executed. โ€ข We can write a loop using either the while loop or the do-while loop 5/22/2022 SWE-103T OOP SED,SSUET 13
  • 14. For Loop โ€ข A for loop has a concise syntax for writing loops. Often we write a while loop in the following common form: i = initialValue; // Initialize loop control variable while (i < endValue){ // Loop body i++; / โ€ข A for loop can be used to simplify the preceding loop as: for (i = initialValue; i < endValue; i++){ // Loop body } 5/22/2022 SWE-103T OOP SED,SSUET 14
  • 15. For Loop Syntax for (initial-action; loop-cont-cond; action-after-each-iteration) { // Loop body; Statement(s); } 5/22/2022 SWE-103T OOP SED,SSUET 15
  • 16. Example 1: โ€ข Ask the user to take 5 inputs. 5/22/2022 SWE-103T OOP SED,SSUET 16
  • 17. Example 2 Generate n multiples of 2. import java.util.Scanner; public class ForloopProgram { public static void main(String[] args) { Scanner input= new Scanner(System.in); System.out.println("Enter value of n: "); int multiples, n=input.nextInt(); System.out.println("The multiples of 2 are: "); for(int i=1; i<=n;i++ ){ multiples=2*i; System.out.print( multiples+" " ); } } } 5/22/2022 SWE-103T OOP SED,SSUET 17
  • 18. Tasks โ€ข Calculate the factorial of the number. โ€ข Calculate sum of the numbers from 1 to n 5/22/2022 SWE-103T OOP SED,SSUET 18