Chapitre 3 Java
Chapitre 3 Java
Chapitre 3 Java
Hassan El Bahi
www.elbahihassan.com
Plan
Qu’est ce que java?
Variables / L'affectation
Comparaisons / conversions
Structures de contrôle
Les entrée/Sortie
Tableaux
Hassan El Bahi
www.elbahihassan.com2
Qu’est ce que java?
3
Qu’est ce que java?
4
Compilation d’un programme Java
5
Qu’est ce que java?
6
Cycle de développement
7
variables
8
Déclaration et initialisation de variables
Note : nous verrons plus tard d’autres types : les types composés et
la généralisation des types par la notion de classe
9
Déclaration et initialisation de variables
int val = 2;
double pi = 3.1415;
char c = ’a’;
int j = 2*val+5;
String phrase = "cours système réparti";
10
Déclaration et initialisation de variables
11
Variable Final
• Une telle variable est similaire à une constante dans les autres
langages de programmation
12
Variables = zone mémoire
Exemple :
• int i = 121;
• char car = ’C’;
• double x = 12.98705;
13
Commentaire
14
Instructions, blocs et blancs
15
Point d’entrée d’un programme Java
16
L'affectation
17
L'affectation
18
Les comparaisons :
19
Les comparaisons :
Exemple :
x = ( y < 5 ) ? 4 * y : 2 * y;
Equivalent à:
If ( y < 5 )
x = 4 * y ;
else
x= 2 * y ;
20
Les conversions, ou « cast »
int i = 123;
float j = (float)i;
int i = 123;
double j = (double)i;
21
Les conversions, ou « cast »
double i = 1.23;
double j = 2.9999999;
int k = (int)i; //k vaut 1
k = (int)j; //k vaut 2
int i;
double d;
i = 5 / 2; // Oui, 2
d = 5.0 / 2.0; // Oui, 2.5
d = 5.0 / 2; // Oui, 2.5
d = 5 / 2.0; // Oui, 2.5
d = 5 / 2; // Oui, 2.0 i = 5 / 2.0; // Non
i = (int) (5 / 2.0); // Oui, 2
22
Structures de contrôle
if (expression) instruction;
Ou :
if (expression) {
instruction1;
instruction2;
}
23
Structures de contrôle
if (expression) {
instruction1;
} else {
instruction2;
}
Exemple :
if (a<b) {
min=a;
} else {
min=b;
}
24
Structures de contrôle
if (expression1) {
bloc1;
}
else if (expression2) {
bloc2;
}
else if (expression3) {
bloc3;
} else {
bloc5;
}
25
Structures de contrôle
Switch ( variable ) {
Case valeur1 : instructions1 ; break;
Case valeur2 : instructions2 ; break;
Case valeur3 : instructions2 ; break;
. .
Case valeurN : instructionsN ; break;
Default : instructions ; break;
}
26
Structures de contrôle
Exemple : L'instruction switch
switch (month) {
case 1: System.out.println("January"); break;
case 2: System.out.println("February"); break;
case 3: System.out.println("March"); break;
case 4: System.out.println("April"); break;
case 5: System.out.println("May"); break;
case 6: System.out.println("June"); break;
case 7: System.out.println("July"); break;
case 8: System.out.println("August"); break;
case 9: System.out.println("September"); break;
case 10: System.out.println("October"); break;
case 11: System.out.println("November"); break;
case 12: System.out.println("December"); break;
default: System.out.println("Invalid month.");break;
}
27
Structures de contrôle
Exemple :
28
Structures de contrôle
L’instruction While
while (condition){
Instructions;
}
int i=5;
while (i > 1) {
System.out.print(i + " ");
i = i / 2 ;
}
Réponse :
52
29
Structures de contrôle
L’instruction do .. while
do{
Instructions;
} while (condition)
int i=10;
do{
System.out.print(i + " ");
i = i / 2 ; }
while (i > 1)
Réponse :
10 5 2
30
Structures de contrôle
31
Les entrée/Sortie
System.out.print(…);
Ou bien :
System.out.println(…);
32
Tableaux
Ou
33
Tableaux
34
Tableaux
35
Tableaux
36
Tableaux à plusieurs dimensions
int[][] matrice =
{
{ 0, 1, 4, 3 } , // tableau [0] de int
{ 5, 7, 9, 11, 13, 15, 17 } // tableau [1] de int
};
37
Tableaux à plusieurs dimensions
matrice.length // 2
matrice[0].length // 4
matrice[1].length // 7
int i, j;
for(i=0; i<matrice.length; i++) {
for(j=0; j<matrice[i].length; j++){
//Action sur matrice[i][j]
}
}
38
Merci pour votre attention
39