Conditional Statements and Loops

Requirements

Linux Distribution (obviously)
A text editor :
Vim, Emacs, gedit, kate, atom
A Compiler :
gcc or clang
A Shell:
bash, xterm, sh,

My setup for this tutorial:

Debian GNU/Linux 9.4
Atom (Text editor)
gcc version 6.3.0-18+deb9u1 (Debian’s Release version for Debian 9.1)
bash

Conditional statements are a way for the program to branch and do different
things depending on a variable’s value. We do this through if/else statements
or switch statements.

If/else statements:

If/else statements are used for specific variable condition checking as well
as multiple variable checking. It is declared as such:

 if(condition){
   //code goes here
 }else{
   //other code here
 }

For multiple branching you can do else-if statements

 if(first condition){
   //code goes here
 }else if(second condition){
   //code goes here
 }else{
   //other code here
 }

Also they can be nested as such:

 if(condition){
  if(condition){
   //code goes here
    }else{
    //other code here
   }
 }else{
   //other code here
 }

Lets create go back to our main.c

#include <stdio.h>

int multiply(int a , int b);
void swap(int * a, int * b);

int main(int argc,char * argv[]){

  int foo = 42;
  int bar = 3;

  printf("Hello World!\n");
  if(foo < bar){
  printf("Printing bar: %d\n",bar);
  }else{
  printf("Printing foo: %d\n",foo);
  }
  printf("Multiplying the two: %d\n",multiply(foo,bar));
  swap(&foo,&bar);


  return 0;

}

int multiply(int a , int b){
  return a * b;
}

void swap(int * a , int * b){
  int temp = *a;
  *a = *b;
  *b = temp;
}

If you recompile this, and see the result. It should print :

Hello World
Printing foo: 42
Multiplying the two : 126

This is because it the program skips over the printf("Printing bar: 3")
because foo is greater then bar. As long as the condition within the if statement
returns true, the branch executes.

Switches:

Sometimes you only need to check one variable multiple times. Using an if/
else if/else statemenc can become pretty redundant, so instead you can use a
switch statement. You declare them with the following:

  switch( variable ){
   case VARIABLE_CASE_1 :
    //code goes here
      break;
    case VARIABLE_CASE_2 :
    //code goes here
      break;
    case VARIABLE_CASE_3 :
    //code goes here
    break;
    default case:
    //Default case here
    break;
  }

If variable = VARIABLE_CASE_1, the first block executes until the break
keyword. Lets give a real example:

#define ROCK 1
#define PAPER 2
#define SCISSORS 3

Defining constants like such allow you to use hardcoded constants as case
statements. Since the use of the const keyword doesn’t give a compile-time constant variable ( a variable that can’t change its value through out the lifetime of the program), and case statements require a constant expression, we use these defines as values.

int input;
printf("Pick 1. Rock,2. Paper, or 3. Scissors");
scanf("%d", &input);

switch(input){
  case ROCK:
  printf("You picked Rock")
  break;
  case PAPER:
  printf("You picked Paper");
  break;
  case SCISSORS:
  printf("You picked Scissors");
  break;
  default case:
  printf("You failed to pick a valid input");
  break;
}

The scanf function is a formatted input function. It takes in a const char pointer (in C, const char * is a string), and the address of the variables you want to put values into. The full program:

#include <stdio.h>


#define ROCK 1
#define PAPER 2
#define SCISSORS 3

int main(int argc,char *argv[]){

int input;
printf("Pick 1. Rock,2. Paper, or 3. Scissors");
scanf("%d", &input);

switch(input){
  case ROCK:
  printf("You picked Rock");
  break;
  case PAPER:
  printf("You picked Paper");
  break;
  case SCISSORS:
  printf("You picked Scissors");
  break;
  default:
  printf("You failed to pick a valid input");
  break;
}

  return 0;
}

Depending on your answer , it will pick the specific branch needed, and then break out of the switch statement.

Loops:

Loops are a simple concept in C, you do the required block of code for a certain amount of times. There is multiple ways to do this.

While loops:
these are declared:

    while( condition ){
      //insert code here
    }

If the condition is false , the block will not execute, however if you need
a loop to be ran at least once, you can use the next type of loop

  do{
  //insert code here
  }while( condition )

This loop will run atleast once , and then continue to run until the while
condition is true.

  for(initialization; condition; increment){
  //do code here
  }

The for loop is a little different then the others. While the others require
the variable within the condition to be declared already, the for loop does that
within the parameters. Say i wanted to increment through an array of integers:

  int arr[5] = { 1,2,3,4,5};

I can do something along the lines of:

  for(unsigned int i = 0;i < 5; i++){
    printf("Array value at %d : %d\n", i , arr[i]);
  }

What this will do is set up a temporary unsigned integer named i and assign it
to a value of 0, then print the array at value at i .

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s