KEMBAR78
for loop in java | PPTX
ORGANIZED BY MAJID JHATIAL
JAVA PROGRAMMER
My majid2936@gmail.com
Only for loop exercise and a lot of example in
For loop
Coming soon upload while-loop do-while loop
LOOP REPETITION STATEMENTS
Repetition of statements allow us to
execute a statements multiple time.
Java has three kinds repetition
statements
1 for loop
2 While loop
3 Do-while loop
WHAT IS FOR LOOP ?
For loop is also known as counter loop
There are three part of for loop.
following syntax:
For(initialization ,condition; increment )
1 Initialization is executed once before the loop
begins
2 The statement is executed until the condition
becomes false
3 The increment portion is executed at the end of each
teration
THE FOR STATEMENTS
Example for loop
For(int count=1 ;count<=5; count++){
System.out.println(count);
}
1 Initialization section can be declare the variable
2 Increment section can be performed any calculation
for (int num=100; num > 0; num -= 5){
System.out.println (num);
}
INFINITE LOOP
For example infinite loop
for(int count=1; count<=14; i--){
System.out.println(count);
}
Infinite loop will continue execute until interrupter
(Ctrl-c)
ENDING LOOPING PROGRAM
Break
Break statements result in the terminate of
statements .which applies(Do-while,for,while, switch) . The
break statements gets you out of loop. No meter what the
loop e ending condition. Break immediately says. I am out
here.
Continue
continue skips the statements after the continue
statement and keeps looping.
Continue perform a jump to the next test condition in a loop
the condition statement may be used only in iteration
statements (loop)
THE BREAK
Break example in for loop syntax
for(int i=1; i<=30; i++){ output : 1,2,3
if(i==3){
break;
}
System.out.println(i);
}
Int count=0;
for(int i=1; i<=30; i++){
if(i==16){
break;
}
System.out.println(i);
count++;
}
System.out.println(“Totle=”+count);
CONTINUE EXAMPLE
for(int count=1; count<=30; i++){
if(count==13){
continue;
}
System.out.print(count);
}
int a=0;
for(int count=1; i<=22; count++){
if(count==13){
continue;
}
System.out.oprintln(count);
a++;
}
System.out.println(a);
FIBONACCI SERIES
Enter number 5 then output 1,1,2,3
Scanner ob=new Scanner(System.in);
System.out.println(“Enter the number”);
int i,a=0,b=1,c=0;
int num=ob.nextInt();
for( i=1; i<=num; i++){
System.out.println(c);
a=b;
b=c;
c=a+b;
}
FACTORIAL SERIES
Example factorial number and enter the 5 number t then
output 120..
Scanner bo = new Scanner(System.in);
System.out.println(“Enter the number”);
int fact=1;
int num = ob.nextInt();
for(int i=1; i<=num; i++){
fact=fact*i;
System.out.println(“this factorial number ”+fact);
}
PRIME NUMBER
int n;
int status = 1, num = 3;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the value of n:");
n = scanner.nextInt();
if (n >= 1) {
System.out.println("First "+n+" prime numbers are:");
System.out.println(2); }
for ( int i = 2 ; i <=n ; ) {
for ( int j = 2 ; j <= Math.sqrt(num) ; j++ ) {
if ( num%j == 0 ) {
status = 0;
break; }
}
if ( status != 0){
System.out.println(num);
i++; }
status = 1;
num++; }
NEST FOR LOOP
For loop inside for loop is called nested for loop
A for loop can contain any kind of statement in its body,
including another for loop.
follow syntax
for( outer ){
for(inner){
}
for(----; ---; ){
}
}
practice nest loop
PATRICE NEST LOOP SAME EXAMPLE
class Test{
public static void main(String arg[]){
for(int i=1 ; i=4; i++ ){
f(int j=1; j<=4; j++ ){
}// inner loop close
System.out.println(j);
}// outer loop close;
}
}
another example
for(int i=2 i<5; i++ ){
for(int j=i; j<=5; j++){
System.out.println(i);
}
System.out.println();
}
for( int i=1; i<=10 ; i++){
for(int j=1; j<10; j++ ){
System.out.println(j);
j++;
}
}
for( int i=1; i<=10 ; i++){
for(int j=1; j>10; j++ ){
System.out.println(j);
j++;
}
System.out.println(i);
}
for( int i=1; i>=10 ; i--){
for(int j=1; j<10; j++ ){
System.out.println(j);
j++;
}
}
Nested for loop exercise
What is the output of the following nested for
loop?
for (int i = 1; i <= 6; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
Output:
*
**
***
****
*****
******
for (int i = 1; i <= 6; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i);
}
System.out.println();
}
Output?
What is the output of the following nested for loops?
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= (5 - i); j++) {
System.out.print(" ");
}
for (int k = 1; k <= i; k++) {
System.out.print(i);
}
System.out.println();
}
Answer:
1
22
333
4444
55555
This loop repeats 10 times, with i from 1 to 10.
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 5; j++) { // loop goes 5 times
System.out.print(j); // print the j
}
System.out.println();
}
Better:
// Prints 12345 ten times on ten separate lines.
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 5; j++) {
System.out.print(j);
}
System.out.println(); // end the line of output
}

for loop in java

  • 1.
    ORGANIZED BY MAJIDJHATIAL JAVA PROGRAMMER My majid2936@gmail.com Only for loop exercise and a lot of example in For loop Coming soon upload while-loop do-while loop
  • 2.
    LOOP REPETITION STATEMENTS Repetitionof statements allow us to execute a statements multiple time. Java has three kinds repetition statements 1 for loop 2 While loop 3 Do-while loop
  • 3.
    WHAT IS FORLOOP ? For loop is also known as counter loop There are three part of for loop. following syntax: For(initialization ,condition; increment ) 1 Initialization is executed once before the loop begins 2 The statement is executed until the condition becomes false 3 The increment portion is executed at the end of each teration
  • 4.
    THE FOR STATEMENTS Examplefor loop For(int count=1 ;count<=5; count++){ System.out.println(count); } 1 Initialization section can be declare the variable 2 Increment section can be performed any calculation for (int num=100; num > 0; num -= 5){ System.out.println (num); }
  • 5.
    INFINITE LOOP For exampleinfinite loop for(int count=1; count<=14; i--){ System.out.println(count); } Infinite loop will continue execute until interrupter (Ctrl-c)
  • 6.
    ENDING LOOPING PROGRAM Break Breakstatements result in the terminate of statements .which applies(Do-while,for,while, switch) . The break statements gets you out of loop. No meter what the loop e ending condition. Break immediately says. I am out here. Continue continue skips the statements after the continue statement and keeps looping. Continue perform a jump to the next test condition in a loop the condition statement may be used only in iteration statements (loop)
  • 7.
    THE BREAK Break examplein for loop syntax for(int i=1; i<=30; i++){ output : 1,2,3 if(i==3){ break; } System.out.println(i); } Int count=0; for(int i=1; i<=30; i++){ if(i==16){ break; } System.out.println(i); count++; } System.out.println(“Totle=”+count);
  • 8.
    CONTINUE EXAMPLE for(int count=1;count<=30; i++){ if(count==13){ continue; } System.out.print(count); } int a=0; for(int count=1; i<=22; count++){ if(count==13){ continue; } System.out.oprintln(count); a++; } System.out.println(a);
  • 9.
    FIBONACCI SERIES Enter number5 then output 1,1,2,3 Scanner ob=new Scanner(System.in); System.out.println(“Enter the number”); int i,a=0,b=1,c=0; int num=ob.nextInt(); for( i=1; i<=num; i++){ System.out.println(c); a=b; b=c; c=a+b; }
  • 10.
    FACTORIAL SERIES Example factorialnumber and enter the 5 number t then output 120.. Scanner bo = new Scanner(System.in); System.out.println(“Enter the number”); int fact=1; int num = ob.nextInt(); for(int i=1; i<=num; i++){ fact=fact*i; System.out.println(“this factorial number ”+fact); }
  • 11.
    PRIME NUMBER int n; intstatus = 1, num = 3; Scanner scanner = new Scanner(System.in); System.out.println("Enter the value of n:"); n = scanner.nextInt(); if (n >= 1) { System.out.println("First "+n+" prime numbers are:"); System.out.println(2); } for ( int i = 2 ; i <=n ; ) { for ( int j = 2 ; j <= Math.sqrt(num) ; j++ ) { if ( num%j == 0 ) { status = 0; break; } } if ( status != 0){ System.out.println(num); i++; } status = 1; num++; }
  • 12.
    NEST FOR LOOP Forloop inside for loop is called nested for loop A for loop can contain any kind of statement in its body, including another for loop. follow syntax for( outer ){ for(inner){ } for(----; ---; ){ } } practice nest loop
  • 13.
    PATRICE NEST LOOPSAME EXAMPLE class Test{ public static void main(String arg[]){ for(int i=1 ; i=4; i++ ){ f(int j=1; j<=4; j++ ){ }// inner loop close System.out.println(j); }// outer loop close; } } another example for(int i=2 i<5; i++ ){ for(int j=i; j<=5; j++){ System.out.println(i); } System.out.println(); }
  • 14.
    for( int i=1;i<=10 ; i++){ for(int j=1; j<10; j++ ){ System.out.println(j); j++; } } for( int i=1; i<=10 ; i++){ for(int j=1; j>10; j++ ){ System.out.println(j); j++; } System.out.println(i); } for( int i=1; i>=10 ; i--){ for(int j=1; j<10; j++ ){ System.out.println(j); j++; } }
  • 15.
    Nested for loopexercise What is the output of the following nested for loop? for (int i = 1; i <= 6; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); } Output: * ** *** **** ***** ****** for (int i = 1; i <= 6; i++) { for (int j = 1; j <= i; j++) { System.out.print(i); } System.out.println(); } Output?
  • 16.
    What is theoutput of the following nested for loops? for (int i = 1; i <= 5; i++) { for (int j = 1; j <= (5 - i); j++) { System.out.print(" "); } for (int k = 1; k <= i; k++) { System.out.print(i); } System.out.println(); } Answer: 1 22 333 4444 55555
  • 17.
    This loop repeats10 times, with i from 1 to 10. for (int i = 1; i <= 10; i++) { for (int j = 1; j <= 5; j++) { // loop goes 5 times System.out.print(j); // print the j } System.out.println(); } Better: // Prints 12345 ten times on ten separate lines. for (int i = 1; i <= 10; i++) { for (int j = 1; j <= 5; j++) { System.out.print(j); } System.out.println(); // end the line of output }