Jean-Jacques BOURDIN
int fibifif (int n) {
if (n == 0) {
return 1;
}
else if (n == 1) {
return 1;
}
else {
return fibifif(n-1) + fibifif(n-2);
}
}
}
|
int fibsw (int n) {
switch (n) {
case 0:
return 1;
case 1:
return 1;
default:
return fibsw(n - 1) + fibsw(n - 2);
}
}
|
void lecase (int n) {
switch (n) {
case 0:
printf("C est nul\n");
case 1:
printf("C est unique\n");
case 2:
printf("La paire\n");
case 3:
printf("La trinite\n");
}
printf("\n");
}
int main(){
lecase(5);
lecase(3);
lecase(1);
lecase(2);
lecase(0);
return 0;
}
|
neige: a.out La trinite C est unique La paire La trinite La paire La trinite C est nul C est unique La paire La trinite neige: |
Trad. tant que ma guitare pleure doucement
While the condition stands, we do the work. It‘s an iteration.
int somw (int n) {
int i, som;
i = 0;
som = 0;
while (i < n) {
som = som + i;
i = i + 1;
}
return som;
}
|
int somdo (int n) {
int i, som;
i = 0;
som = 0;
do {
som = som + i;
i = i + 1;
} while (i < n);
return som;
}
|
for is almost like while but for initial, and ending parts.
int somf (int n) {
int i, som;
i = 0;
som = 0;
for (/* void_1 */ ; i < n; /* void_2 */ ) {
som = som + i;
i = i + 1;
}
return som;
}
|
int somf2 (int n) {
int i, som;
/*void_1*/
som = 0;
for ( i = 0 ; i < n; i = i + 1 ) {
som = som + i;
/* void_2 */
}
return som;
}
|
Déjà vu.
Return exits from the function when it's executed.
Break exits from a control flow. Examples:
int sombr (int n) {
int i, s;
i = 0;
s = 0;
while (1) {
s = s + i;
i = i + 1;
if (i > n) {
break;
}
}
return s;
}
|
int valeur (int n) {
int s;
s = 0;
switch (n) {
case 0:
s = 0;
case 1:
s = s + 1;
case 2:
s = s + 2;
case 3:
s = s + 3;
default:
s = s + n;
}
return s;
}
|
valeur (2); valeur (3); valeur (1); valeur (5);Autre version, sans doute plus claire :
int valeur (int n) {
int s;
s = 0;
switch (n) {
case 0:
s = 0;
break;
case 1:
s = s + 1;
break;
case 2:
s = s + 2;
break;
case 3:
s = s + 3;
break;
default:
s = s + n;
break;
}
return s;
}
|
#include <stdio.h>
int impair (int n) {
int i, res;
res = 0;
for (i = 0; i < n; i = i + 2)
;
return i - n;
}
int somp (int n) {
int s, i;
s = 0;
for (i=0; i <= n; i = i + 1) {
if (impair(i)) {
printf("%d est impair\n",i);
continue;
}
s = s + i;
}
return s;
}
int main () {
int a;
a = 5;
printf("Somme des pairs jusqu a %d : \t %d\n", a, somp(a));
a = 6;
printf("Somme des pairs jusqu a %d : \t %d\n", a, somp(a));
a = 7;
printf("Somme des pairs jusqu a %d : \t %d\n", a, somp(a));
}
|
Blanc14.local: a.out 1 est impair 3 est impair 5 est impair Somme des pairs jusqu a 5 : 6 1 est impair 3 est impair 5 est impair Somme des pairs jusqu a 6 : 12 1 est impair 3 est impair 5 est impair 7 est impair Somme des pairs jusqu a 7 : 12 |
Don‘t use it except for handling errors.
Le plus souvent utilisé pour les gestions d‘erreurs graves.
/* Just let's have mo'fun */
#include <stdlib.h>
#include <stdio.h>
int funct (int n) {
int a, b, c;
a = 1;
b = 1;
c = 2;
if (c > n) {
return a;
}
cas_general:
a = a + b;
b = a - b;
c = c + 1;
if (c <= n) {
goto cas_general;
}
return a;
}
int fonct (int n) {
int a, b;
a = 1;
b = 1;
do {
a = a + b;
b = a - b;
n = n - 1;
if (1 > n) {
goto cas_sortie;
}
} while(1);
cas_sortie:
return b;
}
int finct (int n) {
int a, b, c;
if (n > 23) {
a = -1;
goto la_sortie;
}
a = 1;
b = 1;
c = 2;
cas_retour:
if (c > n)
goto la_sortie;
a = a + b;
b = a - b;
c = c + 1;
goto cas_retour;
la_sortie:
return a;
}
main () {
int i;
for (i = 0; i < 10; i = i + 1) {
printf("Pour %d on a \t%d\t",i,funct(i));
printf("et\t %d ",fonct(i));
printf("et\t %d\n",finct(i));
}
}
|
Pour 0 on a 1 et 1 et 1 Pour 1 on a 1 et 1 et 1 Pour 2 on a 2 et 2 et 2 Pour 3 on a 3 et 3 et 3 Pour 4 on a 5 et 5 et 5 Pour 5 on a 8 et 8 et 8 Pour 6 on a 13 et 13 et 13 Pour 7 on a 21 et 21 et 21 Pour 8 on a 34 et 34 et 34 Pour 9 on a 55 et 55 et 55 |
Pour l‘instant, les labels et gotos ne fonctionnent que au sein d‘une fonction. Pour passer d‘une fonction à une autre, il faudra utiliser les saut plus longs, les "long jump" (rendez-vous en Programmation Avancée).
Other examples
#include <stdio.h>
float racine (float x) {
float y, pas;
pas = 0.0001;
y = 0.0; // initialisation
while ( 1 ) {
if ( y * y >= x) {
break;
}
y += pas; //incrémentation
}
/*
y = 0.0; // initialisation
while ( y * y < x) { // test
// printf(" %f ", y);
y += pas; //incrémentation
}
for (y = 0.0; y * y < x; y += pas) {
printf(" %f ", y);
}
*/
printf("\n");
return y;
}
int impair (int n) {
while (n > 1) {
n -= 2;
}
if (n==1) {
return 1;
}
return 0;
}
int sommepairs (int n) {
int i, somp;
somp = 0;
for (i = 0; i ≤ n; i++) {
if (impair(i)) {
continue;
}
somp = somp + i;
}
return somp;
}
int sommepairsgt (int n) {
int i, somp;
somp = 0;
i = 0;
debut_boucle:
if (impair(i)) {
goto incrementation;
}
somp += i;
incrementation:
i += 1;
if (i ≤ n) {
goto debut_boucle;
}
return somp;
}
int main () {
float val, rac;
int a, b, c;
val = 2.0;
rac = racine (val);
printf("la racine de %f est %f\n", val, rac);
for (a = 0; a < 10; a++) {
b = sommepairs(a);
c = sommepairsgt(a);
printf("jusqu a %d on a %d ou %d\n", a, b, c);
}
}
|