04-Fundamentals of JAVA - Loops
04-Fundamentals of JAVA - Loops
Fundamentals of Java
Loops
ZEYAD ALI ZALI@QU.EDU.QA
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
Summary of Lecture 01-02-03
Lecture 01
Edit program
Lecture 02
Compile program
javac filename.java Declara7on of variable and memory
Byte code -> JVM Java primi7ve data types
Load filename.class 5 basic Arithme7c opera7ons
Verify Byte code Precedence of opera7ons
Execute Java filename Combined opera7ons
Just-in-7me compila7on System.out.print(); String Class, New keyword
Translate Byte code to machine language System.out.println(); String methods
System.out.prin3(); Scanner class and nextXXX()
Lecture 03 methods
If..statement Dialog box and message box
If..else JOp7onPane class
If..else if…else if..else showMessageDialog();showInputDialog()
Switch Conver7ng String object using parse method.
Equality operator
Logical operators
Rela7onal operators
Java Loops
The Increment and Decrement Operators
• In programming (Java, C, C++, JavaScript etc. ), the increment operator ++
increases the value of a variable by 1. Similarly, the decrement operator --
decreases the value of a variable by 1.
int a=10, b = 5 , c = 6;
a++;
b--;
c++;
System.out.printf("a=%d , b=%d , c=%d %n" , a, b , c);
//Output => a=11 , b=4 , c=7
The Increment and Decrement Operators
++ and -- operator as prefix and postfix
• If you use the ++ operator as prefix like: ++var. The value of var is first
incremented by 1 then, it returns the value.
• If you use the ++ operator as postfix like: var++. The original value of var
is returned first then, var is incremented by 1.
• The -- operator works in a similar way like the ++ operator except it
decreases the value by 1.
int a=10, b = 5 , c = 6 , d , e;
d=a++; // d=a first (10) then a is incremented by 1 a = 11
c=--b; // b is decremented first then c=b (both = 5)
b=++a; // a is incremented first then b=a (both = 12)
e = --b + a-- ; //b is decremented first (11) then e = 11 + 12 then a is decremented
System.out.printf("a=%d , b=%d , c=%d , d=%d e=%d %n" , a, b , c , d , e);
//Output a=11 , b=11 , c=4 , d=10 e=23
The while Loop
• While the condition is true, the statements will execute repeatedly.
• The while loop is a pretest loop, which means that it will test the value of
the condition prior to executing the loop.
int i = 1;
while(condition) while ( i <= 5) { // condition
{ System.out.println(i);
statements;
} i++; // increment
true
boolean
expression? statement(s) }
false
Infinite Loop
• In order for a while loop to end, the condition must become false. The
following loop will never end:
int i = 1;
while ( i <= 5) { // condition will never become false 1 =1
System.out.println(i);
} ... forever
• A while loop executes 0 or more times. If the initial condition is false, the
loop will not execute.
int i = 10;
while ( i <= 5) { // initial condition is false. Loop body skipped
System.out.println(i);
} // No output. Loop never executed
The while Loop for Input Validation
• Input validation is the process of ensuring that user input is valid.
double gpa;
Scanner input = new Scanner(System.in);
System.out.print("Enter GPA (0-4.0): ");
gpa = input.nextDouble();
while ( gpa > 4 || gpa < 0) {//Validate the input
System.out.print("Invalid GPA, Please re-enter a valid value: ");
gpa = input.nextDouble();
// Keep getting until a valid value is entered
}
System.out.printf("Your GPA = %.2f %n", gpa);
The do-while Loop
• The do-while loop is a post-test loop, which means it will execute the loop
prior to testing the condition.
do int i =1;
{ do {
statement(s); statement(s)
System.out.println(i);
}while (condition);
i++;
true }while (i <= 3);
boolean
expression?
false
The do-while Loop
• The do-while loop executes at least once since the condition is tested at the
bottom of the loop.
int i =99;
do { //Loop executed once
System.out.println(i);
i++;
}while (i <= 3); //Condition is found false here
//Outputs 99 once
The for Loop
• A pre-test loop. The for loop allows the programmer to initialize a control
variable, test a condition, and modify the control variable all in one line
of code.
for(initialization; test; update)
{
statement(s);
} true
boolean
expression? statement(s) update
int k=1;
while ( k < 10) {
System.out.println("K = " + k);
k++;
if ( k % 4 == 0)
break;
}
System.out.println("I am out of the loop!");
The continue Statement
• The continue statement will cause the currently executing iteration of a loop
to terminate and the next iteration will begin.
• The continue statement will cause the evaluation of the condition in while
and for loops.
• Like the break statement, the continue statement should be avoided because it
makes the code hard to read and debug.