What is the function of the last else statement in the if else if else structure?

In particular, the program is able to make decisions on what it should do next. And those decisions are based on the state of certain pre-defined conditions you set.

Show

The program will decide what the next steps should be based on whether the conditions are met or not.

The act of doing one thing if a particular condition is met and a different thing if that particular condition is not met is called control flow.

For example, you may want to perform an action under only a specific condition. And you may want to perform another action under an entirely different condition. Or, you may want to perform another, completely different action when that specific condition you set is not met.

To be able to do all of the above, and control the flow of a program, you will need to use an

#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
6 statement.

In this article, you will learn all about the

#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
6 statement – its syntax and examples of how to use it so you can understand how it works.

You will also learn about the

#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
8 statement – that is the
#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
9 statement that is added to the
#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
6 statement for additional program flexibility.

In addition, you will learn about the

#output

Please enter your age: 16
You need to be over 18 years old to continue
1 statement for when you want to add more choices to your conditions.

Here is what we will cover:

  1. What is an
    #include <stdio.h>
    
    int main(void) {
        // variable age
       int age;
    
       // prompt user to enter their age
       printf("Please enter your age: ");
    
       // store user's answer in the variable
       scanf("%i", &age);
    
        // check if age is less than 18
        // if it is, then and only then, print a message to the console
        
       if (age < 18) {
           printf("You need to be over 18 years old to continue\n");
       }
    }
    
    6 statement in C?
    1. How to create an
      #include <stdio.h>
      
      int main(void) {
          // variable age
         int age;
      
         // prompt user to enter their age
         printf("Please enter your age: ");
      
         // store user's answer in the variable
         scanf("%i", &age);
      
          // check if age is less than 18
          // if it is, then and only then, print a message to the console
          
         if (age < 18) {
             printf("You need to be over 18 years old to continue\n");
         }
      }
      
      6 statement in C
    2. What is an example of an
      #include <stdio.h>
      
      int main(void) {
          // variable age
         int age;
      
         // prompt user to enter their age
         printf("Please enter your age: ");
      
         // store user's answer in the variable
         scanf("%i", &age);
      
          // check if age is less than 18
          // if it is, then and only then, print a message to the console
          
         if (age < 18) {
             printf("You need to be over 18 years old to continue\n");
         }
      }
      
      6 statement?
  2. What Is An
    #include <stdio.h>
    
    int main(void) {
        // variable age
       int age;
    
       // prompt user to enter their age
       printf("Please enter your age: ");
    
       // store user's answer in the variable
       scanf("%i", &age);
    
        // check if age is less than 18
        // if it is, then and only then, print a message to the console
        
       if (age < 18) {
           printf("You need to be over 18 years old to continue\n");
       }
    }
    
    8 Statement in C?
    1. What is an example of an
      #include <stdio.h>
      
      int main(void) {
          // variable age
         int age;
      
         // prompt user to enter their age
         printf("Please enter your age: ");
      
         // store user's answer in the variable
         scanf("%i", &age);
      
          // check if age is less than 18
          // if it is, then and only then, print a message to the console
          
         if (age < 18) {
             printf("You need to be over 18 years old to continue\n");
         }
      }
      
      8 statement?
  3. What is an
    #output
    
    Please enter your age: 16
    You need to be over 18 years old to continue
    
    1 statement?
    1. What is an example of an
      #output
      
      Please enter your age: 16
      You need to be over 18 years old to continue
      
      1 statement?

What Is An #include <stdio.h> int main(void) { // variable age int age; // prompt user to enter their age printf("Please enter your age: "); // store user's answer in the variable scanf("%i", &age); // check if age is less than 18 // if it is, then and only then, print a message to the console if (age < 18) { printf("You need to be over 18 years old to continue\n"); } } 6 Statement In C?

An

#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
6 statement is also known as a conditional statement and is used for decision-making. It acts as a fork in the road or a branch.

A conditional statement takes a specific action based on the result of a check or comparison that takes place.

So, all in all, the

#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
6 statement makes a decision based on a condition.

The condition is a Boolean expression. A Boolean expression can only be one of two values – true or false.

If the given condition evaluates to

#output

Please enter your age: 28
2 only then is the code inside the
#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
6 block executed.

If the given condition evaluates to

#output

Please enter your age: 28
4, the code inside the
#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
6 block is ignored and skipped.

How To Create An #include <stdio.h> int main(void) { // variable age int age; // prompt user to enter their age printf("Please enter your age: "); // store user's answer in the variable scanf("%i", &age); // check if age is less than 18 // if it is, then and only then, print a message to the console if (age < 18) { printf("You need to be over 18 years old to continue\n"); } } 6 statement In C – A Syntax Breakdown For Beginners

The general syntax for an

#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
6 statement in C is the following:

if (condition) {
  // run this code if condition is true
}

Let's break it down:

  • You start an
    #include <stdio.h>
    
    int main(void) {
        // variable age
       int age;
    
       // prompt user to enter their age
       printf("Please enter your age: ");
    
       // store user's answer in the variable
       scanf("%i", &age);
    
        // check if age is less than 18
        // if it is, then and only then, print a message to the console
        
       if (age < 18) {
           printf("You need to be over 18 years old to continue\n");
       }
    }
    
    6 statement using the
    #include <stdio.h>
    
    int main(void) {
        // variable age
       int age;
    
       // prompt user to enter their age
       printf("Please enter your age: ");
    
       // store user's answer in the variable
       scanf("%i", &age);
    
        // check if age is less than 18
        // if it is, then and only then, print a message to the console
        
       if (age < 18) {
           printf("You need to be over 18 years old to continue\n");
       }
    }
    
    6 keyword.
  • Inside parentheses, you include a condition that needs checking and evaluating, which is always a Boolean expression. This condition will only evaluate as either
    #output
    
    Please enter your age: 28
    
    2 or
    #output
    
    Please enter your age: 28
    
    4.
  • The
    #include <stdio.h>
    
    int main(void) {
        // variable age
       int age;
    
       // prompt user to enter their age
       printf("Please enter your age: ");
    
       // store user's answer in the variable
       scanf("%i", &age);
    
        // check if age is less than 18
        // if it is, then and only then, print a message to the console
        
       if (age < 18) {
           printf("You need to be over 18 years old to continue\n");
       }
    }
    
    6 block is denoted by a set of curly braces,
    #include <stdio.h>
    
    int main(void) {
        // variable age
       int age;
    
       // prompt user to enter their age
       printf("Please enter your age: ");
    
       // store user's answer in the variable
       scanf("%i", &age);
    
        // check if age is less than 18
        // if it is, print a message to the console
        
       if (age < 18) {
           printf("You need to be over 18 years old to continue\n");
       }
       
       // check if age is greater than 18
       // if it is, print a message to the console
       
      if (age > 18) {
          printf("You are over 18 so you can continue \n");
      }
      
       }
    
    3.
  • Inside the
    #include <stdio.h>
    
    int main(void) {
        // variable age
       int age;
    
       // prompt user to enter their age
       printf("Please enter your age: ");
    
       // store user's answer in the variable
       scanf("%i", &age);
    
        // check if age is less than 18
        // if it is, then and only then, print a message to the console
        
       if (age < 18) {
           printf("You need to be over 18 years old to continue\n");
       }
    }
    
    6 block, there are lines of code – make sure the code is indented so it is easier to read.

What Is An Example Of An #include <stdio.h> int main(void) { // variable age int age; // prompt user to enter their age printf("Please enter your age: "); // store user's answer in the variable scanf("%i", &age); // check if age is less than 18 // if it is, then and only then, print a message to the console if (age < 18) { printf("You need to be over 18 years old to continue\n"); } } 6 Statement?

Next, let’s see a practical example of an

#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
6 statement.

I will create a variable named

#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
   
   // check if age is greater than 18
   // if it is, print a message to the console
   
  if (age > 18) {
      printf("You are over 18 so you can continue \n");
  }
  
   }
7 that will hold an integer value.

I will then prompt the user to enter their age and store the answer in the variable

#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
   
   // check if age is greater than 18
   // if it is, print a message to the console
   
  if (age > 18) {
      printf("You are over 18 so you can continue \n");
  }
  
   }
7.

Then, I will create a condition that checks whether the value contained in the variable

#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
   
   // check if age is greater than 18
   // if it is, print a message to the console
   
  if (age > 18) {
      printf("You are over 18 so you can continue \n");
  }
  
   }
7 is less than 18.

If so, I want a message printed to the console letting the user know that to proceed, the user should be at least 18 years of age.

#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}

I compile the code using

#output

Please enter your age: 28
You are over 18 so you can continue 
0, where
#output

Please enter your age: 28
You are over 18 so you can continue 
1 is the name of the C compiler and
#output

Please enter your age: 28
You are over 18 so you can continue 
2 is the name of the file containing the C source code.

Then, to run the code I type

#output

Please enter your age: 28
You are over 18 so you can continue 
3.

When asked for my age I enter

#output

Please enter your age: 28
You are over 18 so you can continue 
4 and get the following output:

#output

Please enter your age: 16
You need to be over 18 years old to continue

The condition (

#output

Please enter your age: 28
You are over 18 so you can continue 
5) evaluates to
#output

Please enter your age: 28
2 so the code in the
#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
6 block executes.

Then, I re-compile and re-run the program.

This time, when asked for my age, I enter

#output

Please enter your age: 28
You are over 18 so you can continue 
8 and get the following output:

#output

Please enter your age: 28

Well... There is no output.

This is because the condition evaluates to

#output

Please enter your age: 28
4 and therefore the body of the
#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
6 block is skipped.

I have also not specified what should happen in the case that the user's age is greater than 18.

I could write another

#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
6 statement that will print a message to the console if the user's age is greater than 18 so the code is a bit clearer:

#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
   
   // check if age is greater than 18
   // if it is, print a message to the console
   
  if (age > 18) {
      printf("You are over 18 so you can continue \n");
  }
  
   }

I compile and run the code, and when prompted for my age I enter again 28:

#output

Please enter your age: 28
You are over 18 so you can continue 

This code works. That said, there is a better way to write it and you will see how to do that in the following section.

What Is An #include <stdio.h> int main(void) { // variable age int age; // prompt user to enter their age printf("Please enter your age: "); // store user's answer in the variable scanf("%i", &age); // check if age is less than 18 // if it is, then and only then, print a message to the console if (age < 18) { printf("You need to be over 18 years old to continue\n"); } } 8 Statement in C?

Multiple

#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
6 statements on their own are not helpful – especially as the programs grow larger and larger.

So, for that reason, an

#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
6 statement is accompanied by an
#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
9 statement.

The

#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
8 statement essentially means that "
#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
6 this condition is true do the following thing,
#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
9 do this thing instead".

If the condition inside the parentheses evaluates to

#output

Please enter your age: 28
2, the code inside the
#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
6 block will execute. However, if that condition evaluates to
#output

Please enter your age: 28
4, the code inside the
#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
9 block will execute.

The

#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
9 keyword is the solution for when the
#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
6 condition is false and the code inside the
#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
6 block doesn't run. It provides an alternative.

The general syntax looks something like the following:

if (condition) {
  // run this code if condition is true
} else {
  // if the condition above is false run this code
}

What Is An Example Of An #include <stdio.h> int main(void) { // variable age int age; // prompt user to enter their age printf("Please enter your age: "); // store user's answer in the variable scanf("%i", &age); // check if age is less than 18 // if it is, then and only then, print a message to the console if (age < 18) { printf("You need to be over 18 years old to continue\n"); } } 8 Statement?

Now, let's revisit the example with the two separate

#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
6 statements from earlier on:

#include <stdio.h>

int main(void) {
   int age;

   printf("Please enter your age: ");


   scanf("%i", &age);

   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
  if (age > 18) {
      printf("You are over 18 so you can continue \n");
  }
  
   }

Let's re-write it using an

#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
8 statement instead:

#include <stdio.h>

int main(void) {
   int age;

   printf("Please enter your age: ");

   scanf("%i", &age);

 
    // if the condition in the parentheses is true the code inside the curly braces will execute
    // otherwise it is skipped
    // and the code in the else block will execute
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   } else {
      printf("You are over 18 so you can continue \n");
  }
  
   }

If the condition is

#output

Please enter your age: 28
2 the code in the
#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
6 block runs:

#output

Please enter your age: 14
You need to be over 18 years old to continue

If the condition is

#output

Please enter your age: 28
4 the code in the
#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
6 block is skipped and the code in the
#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
9 block runs instead:

#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
0

What Is An #output Please enter your age: 16 You need to be over 18 years old to continue 1 Statement?

But what happens when you want to have more than one condition to choose from?

If you wish to chose between more than one option and want to have a greater variety in actions, then you can introduce an

#output

Please enter your age: 16
You need to be over 18 years old to continue
1 statement.

An

#output

Please enter your age: 16
You need to be over 18 years old to continue
1 statement essentially means that "If this condition is true, do the following. If it isn't, do this instead. However, if none of the above is true and all else fails, finally do this."

The general syntax looks something like the following:

#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
1

What Is An Example Of An #output Please enter your age: 16 You need to be over 18 years old to continue 1 Statement?

Let's see how an

#output

Please enter your age: 16
You need to be over 18 years old to continue
1 statement works.

Say you have the following example:

#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
2

If the first

#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
6 statement is true, the rest of the block will not run:

#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
3

If the first

#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
6 statement is false, then the program moves on to the next condition.

If that is true the code inside the

#output

Please enter your age: 16
You need to be over 18 years old to continue
1 block executes and the rest of the block doesn't run:

#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
4

If both of the previous conditions are all false, then the last resort is the

#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
9 block which is the one to execute:

#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
5

Conclusion

And there you have it – you now know the basics of

#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
6,
#include <stdio.h>

int main(void) {
    // variable age
   int age;

   // prompt user to enter their age
   printf("Please enter your age: ");

   // store user's answer in the variable
   scanf("%i", &age);

    // check if age is less than 18
    // if it is, then and only then, print a message to the console
    
   if (age < 18) {
       printf("You need to be over 18 years old to continue\n");
   }
}
8, and
#output

Please enter your age: 16
You need to be over 18 years old to continue
1 statements in C!

I hope you found this article helpful.

To learn more about the C programming language, check out the following free resources:

  • C Programming Tutorial for Beginners
  • What is The C Programming Language? A Tutorial for Beginners
  • The C Beginner's Handbook: Learn C Programming Language basics in just a few hours

Thank you so much for reading and happy coding :)

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT


What is the function of the last else statement in the if else if else structure?
Dionysia Lemonaki

Learning something new everyday and writing about it


If this article was helpful, tweet it.

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

What is the last statement in an if/then else block?

If no elseifcondition evaluates to True , or if there are no ElseIf statements, the statements following Else are executed. After executing the statements following Then , ElseIf , or Else , execution continues with the statement following End If .

What is the purpose of else in an if

Use else to specify a block of code to be executed, if the same condition is false.

What is the structure of if

The If-Else statement The general form of if-else is as follows: if (test-expression) { True block of statements } Else { False block of statements } Statements; n this type of a construct, if the value of test-expression is true, then the true block of statements will be executed.

What is the function of the If Then endif statement?

Purpose: Use the IF/THEN/ELSE/ENDIF statement construct to conditionally execute job steps within a job. The IF statement is always followed by a relational-expression and a THEN clause. Optionally, an ELSE clause can follow the THEN clause.