Warning: Cannot modify header information - headers already sent by (output started at /mnt/112/sdb/a/6/biotrans/includes/fonctions.php:1) in /mnt/112/sdb/a/6/biotrans/includes/conf.php on line 165

Warning: Cannot modify header information - headers already sent by (output started at /mnt/112/sdb/a/6/biotrans/includes/fonctions.php:1) in /mnt/112/sdb/a/6/biotrans/includes/conf.php on line 166

Warning: Cannot modify header information - headers already sent by (output started at /mnt/112/sdb/a/6/biotrans/includes/fonctions.php:1) in /mnt/112/sdb/a/6/biotrans/includes/conf.php on line 167

Warning: Cannot modify header information - headers already sent by (output started at /mnt/112/sdb/a/6/biotrans/includes/fonctions.php:1) in /mnt/112/sdb/a/6/biotrans/includes/conf.php on line 177

Warning: Cannot modify header information - headers already sent by (output started at /mnt/112/sdb/a/6/biotrans/includes/fonctions.php:1) in /mnt/112/sdb/a/6/biotrans/includes/conf.php on line 178

Warning: Cannot modify header information - headers already sent by (output started at /mnt/112/sdb/a/6/biotrans/includes/fonctions.php:1) in /mnt/112/sdb/a/6/biotrans/includes/conf.php on line 179
Bio Trans

See another lesson

Retour au menu

Quiche Team

Lesson 3 - The conditions

 


1) Conditional statement
2)
Boolean or logical operators:'||', '&&' and '!'
3) Branches control
4)
Another condition: the ‘?:’ operator



        The conditions allow you to select a part of code according to the value of some variables. It is very interesting, for instance, for a space invader game; you have to erase enemies when the gamer kills one.


1) Conditional statement

        The syntax is:
    if (expression)
    statement;

        or more usually,
    if (expression) {
    statement

    }
        In fact, the compiler evaluates the expression between brackets and if it is true and only if, then the compiler runs the code: statement. The evaluated expression may make a comparison between the value of a variable and another value of variable or a number. This comparison is done with the symbols:
- '==' means equal to, you must be careful because you can confuse with '=': the symbol of assignment.
- '>' means greater than.
- '<' means less than.
- '<=' means less than or equal to.
- '>=' means greater than or equal to.
- '!=' means different to.
        But you do not need to put one of these symbols if the expression is a boolean, which means it takes the value true or false.
        There is another condition: 'else' and here the syntax:
    if (expression) {
    statement1
    } else {
    statment2
    }


        In fact, the compiler checks the expression and if it is true then it runs the code of statment1 otherwise it runs the code of statment2. Let us illustrate this point. This example is stupid but you can see how it works.


                         


  // C Source File
  // Created 07/06/2002; 23:49:06

  #define USE_TI89              // Produce .89z File
  #define USE_TI92PLUS          // Produce .9xz File

  #define OPTIMIZE_ROM_CALLS    // Use ROM Call Optimization

  #define SAVE_SCREEN           // Save/Restore LCD Contents

  #include <tigcclib.h>         // Include All Header Files

  // Main Function
  void _main(void)
  {
    int a=25,b=35;
    int n=1; // it's a boolean variable

    clrscr();

    if (a==25) {
      printf("The condition1 is true\n"); // it displays at the screen
    }
    if (a==b) {
      printf("The condition2 is true\n"); // it doesn't display at the screen
    }
    if (a<b) {
      printf("The condition3 is true\n"); // it displays at the screen
    }
    if (a>32) {
      printf("The condition4 is true\n"); // it doesn't display at the screen
    } else {
      printf("The condition4 is false\n"); // it displays at the screen
    }
    if (b!=a) {
      printf("The condition5 is true\n"); // it displays at the screen
    }
    if (n) {
      printf("The condition6 is true\n"); // it displays at the screen
    }
    printf("the value of boolean variable is %d\n",n);
    n=!n; /* a boolean variable take two values: TRUE=1 and FALSE=0 
             this assignement is able to swap its current value for the other value */
    printf("the new value of boolean variable is %d\n",n);
    if (n) {
      printf("The condition7 is true\n"); // it doesn't display at the screen
    }

    ngetchx();

  }

 

2) Boolean or logical operators:'||', '&&' and '!'

        Their meanings are '||' = or (inclusive or), '&&' = and, '!' = not. In fact, '&&' and '||' are binary operators and '!' is an unary operator, in other words, an unary operator applies to one expression. These operators allow you to make more complex conditions instead of testing several if conditions. For example, we can see how to use the operator: '&&' instead of two if conditions.
    if (condition1) {
    if (condition2) {
    statement
    }
    }

        That is equivalent to:
    if ((condition1) && (condition2)) {
    statement
    }

        These two codes are equivalent but the second is clearer and it is made quicker. In fact, the compiler run statement if and only if the condtion1 is true and the condition2 is true also otherwise the compiler jump to after '}'.
    if ((condition1) || (condition2)) {
    statement
    }

        The operator: '||' means either the condition1 is true, either the condition2 is true or the twice are true.
Even if I do not tell you, you can use 'else'. After summing up thes operators, we will see an example.
    if (condtion1) operator (condition2) {
    statement
    }

 

 &&

condition2
condition1 TRUE FALSE
TRUE TRUE FALSE
FALSE FALSE FALSE

 

 ||

condition2
condition1 TRUE FALSE
TRUE TRUE TRUE
FALSE TRUE FALSE

 

expression

TRUE FALSE
!(expression) FALSE TRUE


        As I discussed before, FALSE is a constant, which is equal to 0, and TRUE is a constant, which is not equal to 0, we usually assume that its value is 1.


                         


  // C Source File
  // Created 08/06/2002; 16:46:30

  #define USE_TI89              // Produce .89z File
  #define USE_TI92PLUS          // Produce .9xz File

  #define OPTIMIZE_ROM_CALLS    // Use ROM Call Optimization

  #define SAVE_SCREEN           // Save/Restore LCD Contents

  #include <tigcclib.h>         // Include All Header Files

  // Main Function
  void _main(void)
  {

    int a=1, b=2;
    int t=TRUE, f=FALSE; // these are boolean variables

    clrscr();

    if ((a == 1) & (b <= 5)) {
      printf("the condition1 is true\n"); // it displays at the screen
    }
    if ((a >= 0) & (b < 2)) {
      printf("the condition2 is true\n"); // it doesn't display at the screen
    } else {
      printf("the condition2 is false\n"); // it displays at the screen
    }
    if ((a > 17) | t) {
      printf("the condition3 is true\n"); // it displays at the screen
    }
    if ((a < 13) | t) {
      printf("the condition4 is true\n"); // it displays at the screen
    }
    if ((a < 13) ^ t) {
      printf("the condition5 is true\n"); // it doesn't display at the screen
    }
    if (f ^ t) printf("the condition6 is true\n"); // it displays at the screen

    ngetchx();

  }

 

3) Branches control

        The aim of 'switch' function is to avoid writing several lines with 'if' and especially it is clearer. The syntax is:
    switch (variable)
    {
    case value1:
        statment1
        break;
    case value2:
        statment2
        break;
    case value3:
        statment3
    case valueA:
        statment4
    case valueB:
    case valueC:
    case valueD:
        statment5
        break;
    default:
        statment6
}


        This is equivalent to:

if (variable==value1) {
statment1
}
if (variable==value2) {
statment2
}
if (variable==value3) {
statment3
}
if (variable==valueA) {
statment4
}
if ((variable==value3) || (variable==valueA) || (variable==valueB) || (variable==valueC)
|| (variable==valueD) {
statment5
}
if ((variable!=value1) && (variable!=value2) && (variable!=value3) && (variable!=valueA)
&& (variable!=valueB) && (variable!=valueC) && (variable!=valueD) {
statment6 // it is the default case
}


        I think if you have understood the 'if' condition, you should understand the branch controls. In fact, you use one variable, which can take very different values, and each value you make a case. You note the case is ended by 'break' and if there is not this key word, one part of this case continue in another case. The key word: 'default' is the case if the value of variable is not equal to any case, it is the default case.
 

4) Another condition: the ‘?:’ operator

        This other condition seems like a ‘if-then-else’ condition without statement whose the aim is to assign a variable according the condition. The ‘?:’ operator is a ternary operator. The syntax is:
    name = condition ? value1 : value2;
        ‘name’ is the name of your variable which is assigned by the value which value1 if the condition is true or value2 if the condition is false. The type of your variable and values are the same, it is better to work. :-) Let us simplify this point with a brief example.


                         


  // C Source File
  // Created 16/03/2003; 18:05:27

  #define USE_TI89              // Compile for TI-89
  #define USE_TI92PLUS          // Compile for TI-92 Plus
  #define USE_V200              // Compile for V200

  #define OPTIMIZE_ROM_CALLS    // Use ROM Call Optimization

  #define MIN_AMS 100           // Compile for AMS 1.00 or higher

  #define SAVE_SCREEN           // Save/Restore LCD Contents

  #include <tigcclib.h>         // Include All Header Files

  // Main Function
  void _main(void)
  {
    int a = 13, b = 17;
    int min, max;

    clrscr();
    if (a < b)
      min = a;
    else
      min = b;
    printf ("min(%d;%d) = %d\n", a, b, min);

    // this way is a little shorter
    if (a > b)
      printf ("max(%d;%d) = %d\n", a, b, a);
    else
      printf ("max(%d;%d) = %d\n", a, b, b);

    printf("\n");

    max = (a < b)? b : a;
    printf ("max(%d;%d) = %d\n", a, b, max);

    printf ("min(%d;%d) = %d\n", a, b, (a < b)? a : b);

    ngetchx();
  }





See another lesson
Retour au menu