Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
74 views

Loops: Oop Using Java Oop Using Java

The document discusses different types of loops in Java including for, while, and do-while loops. It provides examples of how to write for loops, while loops, and do-while loops. It also discusses the break and continue statements in loops and provides examples of calculating factorials and averages using loops.

Uploaded by

Dina Amira
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

Loops: Oop Using Java Oop Using Java

The document discusses different types of loops in Java including for, while, and do-while loops. It provides examples of how to write for loops, while loops, and do-while loops. It also discusses the break and continue statements in loops and provides examples of calculating factorials and averages using loops.

Uploaded by

Dina Amira
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

OOP Using JAVA

OOP Using JAVA
06 – Loops
Dr. Mostafa A. El‐Hosseini
melhosseini@eng.kfs.edu.eg
www.melhosseini.co.cc
Recap
» Data Input using Keyin
i i
» Relational Operators
p
» Logical Operators
» If statements
If statements
» If – else statements
» Else – if statements
» Switch statements
Switch statements
» Examples

Dr. Mostafa A. El‐Hosseini ‐ 2010 2
AGENDA
» For loop
» While loop
While loop
» Do‐while loop
» Nested loops
» Examples
» Break 
» Continue

Dr. Mostafa A. El‐Hosseini ‐ 2010 3
For loop
For loop

Dr. Mostafa A. El‐Hosseini ‐ 2010 4
For loop
For loop

Dr. Mostafa A. El‐Hosseini ‐ 2010 5
For loop
For loop

Dr. Mostafa A. El‐Hosseini ‐ 2010 6
HINTS
» for ( int i = 1; i <= 100; i++ )
» for ( int i = 100; i
for ( int 100; i >>= 1; i
1; i‐‐ )
» for ( int i = 7; i <= 77; i += 7 )
» for ( int i = 20; i >= 2; i ‐= 2 )
» for ( int j = 2; j <= 20; j += 3 )
for ( int j = 2; j <= 20; j += 3 )
» for ( int j = 99; j >= 0; j ‐= 11 )

Dr. Mostafa A. El‐Hosseini ‐ 2010 7
Common Programming Error
Common Programming Error
» When the control variable of a for structure is 
y
initially defined in the initialization section of 
the header of the for structure, using the 
control variable after the body of the structure
control variable after the body of the structure 
is a syntax error.

Dr. Mostafa A. El‐Hosseini ‐ 2010 8
While loop
While loop

Dr. Mostafa A. El‐Hosseini ‐ 2010 9
While loop
While loop

Dr. Mostafa A. El‐Hosseini ‐ 2010 10
Do While Loop
Do‐While Loop
» A characteristic of the while loop is that if the test 
h i i f h hil l i h if h
condition is initially false, the loop never 
executes. For example, the while loop in the 
t F l th hil l i th
following code fragment will not execute the 
statement body because the variable x evaluates 
statement body because the variable x evaluates
to 0 before the first iteration
iintt x = 0;
0
. . .
while (x != 0)
hil ( ! 0)
System.out.println(x);

Dr. Mostafa A. El‐Hosseini ‐ 2010 11
Do While
Do‐While
» In the do‐while
h d hil loop, the test expression is 
l h i i
evaluated after the loop executes. This 
ensures that the loop executes at least once, 
as in the following example:
int x = 0;
. . .
do
System.out.println(x);
while (x != 0);

Dr. Mostafa A. El‐Hosseini ‐ 2010 12
Do While
Do‐While

Dr. Mostafa A. El‐Hosseini ‐ 2010 13
Factorial example
Factorial example
public class myFact
bli l F t
{
public static void main(String[] args)
{
int number,fact,num;
fact = 1;
fact = 1;
num= Keyin.inInt("Number =  ");
for (int i = 2; i<=num;i++)
fact *= i;
System.out.println("Factorial =  "+fact);
}
}

Dr. Mostafa A. El‐Hosseini ‐ 2010 14
Running code
Running code

Dr. Mostafa A. El‐Hosseini ‐ 2010 15
Quiz
• Write down a program to calculate the sum of 
even numbers between 0 and 49.

Dr. Mostafa A. El‐Hosseini ‐ 2010 16
Ans.
Ans
public class addeven
bli l dd
{

public static void main(String[] args)
{
int sum = 0;
for (int i = 0;i<49;i+=2)
sum +=i;
System.out.println("sum =  "+sum);
}
}

Dr. Mostafa A. El‐Hosseini ‐ 2010 17
Quiz
» A class of ten students took a quiz. The grades 
(
(integers in the range 0 to 100) for this quiz 
g g ) q
are available to you. Determine the class 
average on the quiz The class average is equal
average on the quiz. The class average is equal 
to the sum of the grades divided by the 
number of students
number of students. 

Dr. Mostafa A. El‐Hosseini ‐ 2010 18
Break
» The break statement, when executed in a 
while, for, do/while or switch structure, causes 
immediate exit from that structure. 
» Execution continues with the first statement 
Execution continues with the first statement
after the structure. Common uses of the break 
statement are to escape early from a loop or 
l f l
skip the remainder of a switch structure.

Dr. Mostafa A. El‐Hosseini ‐ 2010 19
Continue
» The continue statement, when executed in a 
p the 
while, for or do/while structure, skips
remaining statements in the loop body and 
proceeds with the next iteration of the loop. 
with the next iteration of the loop
» In while and do/while structures, the program 
evaluates the loop‐continuation test 
l h l
immediately after the continue statement 
executes.

Dr. Mostafa A. El‐Hosseini ‐ 2010 20
Homework
• Write down a program to calculate the roots 
q q
of a number of quadratic equations in the 
form of  ax2 + bx + c = 0

Dr. Mostafa A. El‐Hosseini ‐ 2010 21

You might also like