la boucle FOR en java : Exercice Corrigé boucle for
Syntaxe générale des trois parties d’une boucle for
Quels résultats fournit le programme suivant ?
public class ExoII8{
public static void main (String[] args)
{ int i, n ;
for (i=0, n=0 ; i<5 ; i++) n++ ;
System.out.println ("A : i = " + i + ", n = " + n) ;
for (i=0, n=0 ; i<5 ; i++, n++) {}
System.out.println ("B : i = " + i + ", n = " + n) ;
for (i=0, n=50 ; n>10 ; i++, n-= i ) {}
System.out.println ("C : i = " + i + ", n = " + n) ;
for (i=0, n=0 ;
i<3 ; i++, n+=i, System.out.println ("D : i = " + i + ", n = " + n)) ;
System.out.println ("E : i = " + i + ", n = " + n) ;
}
}
A : i = 5, n = 5
B : i = 5, n = 5
C : i = 9, n = 5
D : i = 1, n = 1
D : i = 2, n = 3
D : i = 3, n = 6
E : i = 3, n = 6
Quels résultats fournit le programme suivant ?
public class ExoII8{
public static void main (String[] args)
{ int i, n ;
for (i=0, n=0 ; i<5 ; i++) n++ ;
System.out.println ("A : i = " + i + ", n = " + n) ;
for (i=0, n=0 ; i<5 ; i++, n++) {}
System.out.println ("B : i = " + i + ", n = " + n) ;
for (i=0, n=50 ; n>10 ; i++, n-= i ) {}
System.out.println ("C : i = " + i + ", n = " + n) ;
for (i=0, n=0 ;
i<3 ; i++, n+=i, System.out.println ("D : i = " + i + ", n = " + n)) ;
System.out.println ("E : i = " + i + ", n = " + n) ;
}
}
Solution
A : i = 5, n = 5
B : i = 5, n = 5
C : i = 9, n = 5
D : i = 1, n = 1
D : i = 2, n = 3
D : i = 3, n = 6
E : i = 3, n = 6
Article plus récent Article plus ancien