• Aucun résultat trouvé

Instruction if … else Exemple ! Instruction if

N/A
N/A
Protected

Academic year: 2022

Partager "Instruction if … else Exemple ! Instruction if"

Copied!
3
0
0

Texte intégral

(1)

09/11/2008

1 Informatique S1

Programmation C

Objectifs: réaliser des tests simples et emboités

• Présentation de l’instruction if : ifet if … else

• Usage de tests emboités

• Opérateurs logiques && , ||

Instruction if

• Instruction de contrôle permettant de faire un test

• Format : if (test) {

Bloc d’instructions; }

Bloc d’instructions

Test

Le bloc d’instructions n’est exécuté que si la condition se vérifie VRAI

!

vrai faux

Exemple

Le printfne sera exécuté que si k < 0

Instruction if … else

• Format : if (test) {

Bloc d’instructions 1;

} else {

Bloc d’instructions 2;

}

Bloc d’instructions 2 Test

vrai faux

Bloc d’instructions 1 Exécuté si le test est VRAI

Exécuté si le test est FAUX

(2)

09/11/2008

2

Exemple

Si k < 0, leprintfà la ligne 12 s’affiche (" k est négatif ")

Sinon, leprintfà la ligne 15 s’affiche (" k est positif " )

Instructions if emboitées

• Une instruction if peut contenir d’autres instructions de contrôle

if, while, do…while, for…

if (test 1) { if(test 2)

{ instructions if 2; } else

{ instructions else if 2; }

} else{

instructions else if 1;

}

if (test 1) { instructions if 1;

} else{

if(test 2)

{ instructions if 2; } else

{ instructions else if 2; }

}

Exemple

On n’atteint le 2°ifque si k < 0 est faux

Opérateur logiques

• Opérateurs de la logique booléenne : Operateur && ET (AND)

(a => 0) && (a < 5) Opérateur || OU (OR)

(c == 'o') || (c == 'n') Opérateur ! NON (NOT)

!(c == 'o')

(3)

09/11/2008

3

Opérateurs logiques

&& Vrai Faux

Vrai V F

Faux F F

|| Vrai Faux

Vrai V V

Faux V F

! Vrai Faux

F V

a = 0 , b = 1, c = -1 (a < b) && (b>c) ?

(a < b) || (b < c) ?

! (a < b) ?

Exemple

(k >= 0) && (k<=5) Valeur de k entre 0 et 5

Références

Documents relatifs

1. utiliser TO pour valeurs consécutives.. condition de continuer : parenthèses facultatifs.. E) Boucle de répétition avec

If a single protection domain is not established, then any user who does not have an account on a particular remote machine running TRFS can nonetheless access

If seQ TCP/IP is not installed, if you already have this maintenance supplement on your system, or if you are not in system maintenance mode, custom tenninates at this

tlputchar tl function' will already have been loaded and the one~ in DEFF2.CRL will be ignored... See the carrn;nts in DIO.C for canplete

the Instant-C program, the library source files, a stand-alone version of the Instant-C editor, and screen programs your keyboard for configuring PC have IBM an or compatible, if

Multiple program statements may appear on a single line, if separated by a (:) colon.. When the user types a left-arrow (under-line or RUBOUT on some

exécuter des instructions si la condition est vraie, ce qui s´écrit en pseudo code:.

Cette instruction doit être alignée avec le if correspondant, et elle doit être suivie d’une condition, puis d’un deux-points et après d’instructions indentées, comme pour un