Big Tuto: Program with the C language

From theory to practice

   

Chapter 8: Initialization and Conditions

    

Tutorial presented by: The Ludovyc and Jérémie F. Bellanger (Jay)
  
The Ludovyc
Publication: June 9, 2014
Last revision: March 9, 2025

 

   

   Now that we have seen the instructions in Chapter 7, let's see how to initialize them and what conditions to use.

   Here we go! smiley

   

Initialization

   

   It is a process that allows you to assign a value to a variable.

   For example :

   

int i = 0;   //This is the declaration and initialization of the variable i 
                //More precisely: int i is the declaration 
                // and i = 0; is the initialization
            
              //We can also dissociate the 2 and write: 
int i; 
i = 0;

   
    This code declares the variable named i, of type int and initializes it to zero.

 

   

The conditions

         

   I invite you to copy the following test programs, to avoid copying/pasting as much as possible, because we also learn by doing and copying.wink 

   Replace the [values] with those of your choice, remembering to match the designated type as closely as possible. You can also replace the values ​​of the variables initialized at the beginning of the program (for example, you can change "int i = 0" to "int i = 2"). It is by programming, testing, modifying, and bugging programs (yes, yes cheeky), that you will become a programmer! smiley

    

The condition of equality

 

[variable name] == [value or variable name]
 
Examples : 
a == 2 
b == c

 

The condition is true if the value of the variable on the left is equal to the value / or the value of the variable on the right.

Here is a small test program: wink

   

# include  <stdio.h>
 
int  main ( ) 
{

   int i1 = 0; 
   int i2 = 0;

   if ( i1 == [value] ) 
   { 
         printf ( "i1 is equal to [value]\n" ) ; 
   }
 
   else  if (i1 == i2) 
   { 
          printf ( "i1 is equal to i2\n" ) ; 
   }

          printf ( "End of equality test program.\n" ) ;

}

    

   

The condition of non-equality

   
[variable name] != [value or variable name]
 
Examples : 
a != 2 
b != c

   

   The condition is true if the value of the variable on the left is not equal to the value / or the value of the variable on the right.

   Here is a small test program: 

   
# include  <stdio.h>
 
int  main ( ) 
{

   int i1 = 0; 
   int i2 = 0;

   if ( i1 != [value]) 
   { 
         printf ("i1 is not equal to [ value ]\n"); 
   }
 
   else if (i1 != i2) 
   { 
          printf ("i1 is not equal to i2\n"); 
   }

          printf ("End of non-equality test program.\n");

}

    

   

The condition of strict inferiority

    
[variable name] < [value or variable name]
 
Examples : 
a < 2 
b < c
   

   The condition is true if the value of the variable on the left is strictly less than the value / or the value of the variable on the right.

   Here is a small test program: 

   

# include  <stdio.h>
 
int main ( ) 
{

   int i ;
 
   for ( i = 0; i < [value]; i ++ ) 
   { 
         printf ("Hello number: %d !\n", i) ; 
   }
 
          printf("End of strict inferiority test program.\n");

}

    

   

The condition of strict superiority

   
[variable name] > [value or variable name]
 
Examples: 
a > 2 
b > c
    

   The condition is true if the value of the variable on the left is strictly greater than the value / or the value of the variable on the right.

   Here is a small test program: 

   

# include <stdio.h>
 
int main ( ) 
{

   int i = 3;
 
   do 
   { 
         printf("End of program in : %d !\n", i); 
         i -- ; 
   }
 
          printf("End of strict superiority test program.\n");

}
 

    

   

The condition of less than or equal

    
[variable name] <= [value or variable name]
 
Examples: 
a <= 2 
b <= c
   

   This is a variant of strict less than. The condition is true if the value of the variable on the left is less than or equal to the value / or the value of the variable on the right.

    

The condition of greater than or equal

    
[variable name] >= [value or variable name]
 
Examples: 
a >= 2 
b >= c
   

   Same principle. The condition is true if the value of the variable on the left is greater than or equal to the value / or the value of the variable on the right.

 
   

The anti condition

   
!(condition1)
   

   The condition is true if condition1 is false and vice versa.

   Here is a small test program: 

   

# include <stdio.h>
 
int main( ) 
{

   int i = 0;   //Change the value of i to test
 
   if  (!(i == 0))    
   { 
         printf ("i is not equal to zero.\n"); 
   }
 
   else     
   { 
         printf ("i is equal to zero.\n"); 
   }
 
          printf("End of anti-condition test program.\n");

}

    

   

The multiple AND condition: &&

    
minimum code : condition1 && condition2
 
code : condition1 && condition2 && condition3 && . . .
   

   The condition is true if condition1 and all others separated by the && symbols are true.

   Note for code optimization: the program first analyzes condition1 then condition2 and so on. If one of them is false, it stops the analysis to move on to the next step. If they are all true, the program continues its execution normally.

   Here is a small test program: 

   

# include <stdio.h>
 
int main( ) 
{

   char c = 'a'; 
   char c1 = 97;
 
   //Code without &&

   if ( c == 'a' ) 
   { 
        if ( c == 97 ) 
        { 
             if ( c == c1 ) 
            { 
                 printf ( "true" ) ; 
            } 
            else 
           { 
                 printf ( "false\n" ) ; 
           } 
       } 
       else 
      { 
            printf ( "false\n" ) ; 
       } 
   } 
   else 
  { 
        printf ( "false\n" ) ; 
  }
 
   //Code with && (works the same as the previous code without &&)

   if ( c == 'a' && c == 97 && c == c1 ) 
   { 
        printf ( "true\n" ) ; 
   } 
   else 
   { 
        printf ( "false\n" ) ; 
   } 
}

   


The multiple OR condition: || 

   
minimum code : condition1 || condition2
 
code : condition1 || condition2 || condition3 || . . .
  

   The condition is true if condition1 is true or if one of the following conditions, separated by || is true.

   Note for code optimization:  the program first analyzes condition1 then condition2 and so on. If one of them is true, it stops the analysis to continue its execution normally. If they are all false, then the program goes to the next step.

   Here is a small test program: 

   

# include <stdio.h>
 
int main( ) 
{

   char c = 'a' ; 
   char c1 = 97 ;
 
   //Code without ||

   if ( c == 'a' ) 
   { 
         printf ( "true\n" ) ; 
   } 
   else  if ( c == 97 ) 
   { 
         printf ( "true\n" ) ; 
   } 
    else  if ( c == c1 ) 
   { 
         printf ( "true\n" ) ; 
   } 
   else 
  { 
        printf ( "false\n" ) ; 
  }
 
 
   //Code with || (works the same as the previous code without ||)

   if ( c == 'a' || c == 97 || c == c1 ) 
   { 
        printf ( "true\n" ) ; 
   } 
   else 
   { 
        printf ( "false\n" ) ; 
   } 
}

   

    

Example of a mix between the && and || condition 

    
(condition1 && condition2) || condition3
   

   The condition is true if condition1 and condition2 are true or if condition1 and condition2 are false and condition3 is true.

    
condition1 && (condition2 || condition3)
   

   The condition is true if condition1 is true and either condition2 or condition3 is true.

   In C, False is 0 and True is any other value, but we will use the value 1 instead to recall the binary system.

   So here is a little test program: 

 

# include <stdio.h>
 
int main( ) 
{
 
    //I invite you to change these values ​​for your own tests 
   char condition1 = 0 ;  //condition1 is false 
   char condition2 = 0 ;  //condition2 is false 
   char condition3 = 1 ;  //condition3 is true
 
   if ( ( condition1 && condition2 ) || condition3 ) 
   { 
        printf ( "true\n" ) ; 
   } 
    else 
   { 
        printf ( "false\n" ) ; 
   }
       
}

   

  

Infinite loops

   This programmer's obsession that is triggered when you don't check your algorithm makes your program loop interminably. Your program therefore never stops, which is really very problematic. But in some software that requires constant refreshing, such as video games for example, you will be forced to use them, and they will then have this form:

   

while (1) 
{ 
   . . . 
}
 
//Or
 
do 
{ 
    . . . 
} while (1) ;
 
//or even
 
for (  ;  ;  ) 
{ 
    . . . 
}

  
   To be used with caution therefore, by checking that there is always a break instruction present in the contained code and which can be executed, to exit the loop (and, for example, quit the video game wink).


Example of Switch

# include <stdio.h>
 
int main( ) 
{
 
    int i = 0;
 
    switch(i) 
    { 
          case 0 : 
                printf("i is equal to 0.\n"); 
                i ++ ; 
                break;

          case  1 : 
                printf("i is equal to 1.\n"); 
                i ++ ; 
                break;
 
          case  2 : 
                printf("i is equal to 2.\n"); 
                break;
 
          default : 
                 printf("Default.\n"); 
                 i ++ ; 
                // the break is optional because the default is at the end of the switch 
    }

  printf ( "Result = %d.\n", i);

}       

   

   

Challenge exercise

   

   Transcode into C language the pseudo-program of the empty rectangle seen in the previous chapter 7. Only the Do-While loop did not have an example in this chapter but you will have no difficulty using it.wink

   That's a big chapter finished! smileyNow that you have the basics of C programming, you can move on to the Big Tuto C / SDL to put all that into practice, by creating your first video game! 😃

    @ Soon !

         Jay and the Ludovyc. 

 

This site uses cookies to enable you to log in. We do not store or sell any personal data. By continuing to use this website, you agree to their use. Thanks!