Programming Language Geek

June 10, 2008

if.. else if.. statement

Filed under: C / C++

Suppose you are trying to set conditions that if ever your condition will be met, then it does the first step, Plan A. If the condition will not be met, then it will execute Plan B. These are just some of the functionalities of the "if.. else if.. statement".

The syntax:

if (condition){
some statements…
}

Note that the statements to be executed should be enclosed within two brackets (open bracket { and close bracket }). The characters above in bold should be strictly followed since this is the actual syntax of the statement. The condition is boolean. If it returns no value, then it’s considered false, while otherwise, it’s true.

Some statements represents the statements to be executed whenever the condition is true. If not, then the statements will be skipped. To handle conditions much more effectively using if statements, you can add the else or else if statement.

if (condition){
some statements…
}
else if (another condition){
some statements…
}
else{
some statements…
}

Now how does the compiler handle these statements? First, it checks the first condition it encounters. If the condition is true, then it will execute the statement enclosed within it. If not, then it searches for an else if statement until it meets the condition which returns true. If not still, then it will look for the else statement. When it reaches the else statement, the codes included within it will right away be executed. Notice that in the else statement, there is no condition required.

Later on, we’ll be discussing some variations of the if.. else if.. statement. Just take note of the concepts since you might be applying this in the future. 

May 26, 2008

C / C++ Variables

Filed under: C / C++

You should be able to remember about variables in Mathematics. Well, they just have the same concept as variables in programming. They act as a temporary storage for constant values like characters and numerics. These variables are declared, and as soon as you do that for your programs, a memory space will automatically be allocated and reserved for an expected value. While they’ll be destroyed whether manually or automatically, depending on the designation of the programming language, whenever they will no longer be of use. Note that these variables consume the random access memory to temporarily allow lots of application functionalities. So, as a programmer, you are solely responsible of how much or how less variables you need to create.

In a C / C++ Programming Language, the format to declare a variable is as follows:

variableType variableName;

variableType is the type of the variable to declare. Here is a list of common C / C++ variable or data types :

Data Type
Origin
Bits Allocated
Description
int
Integer
16 Bits
Stores numbers approximately from -2147483648 to +2147483648.
char
Character
8 Bits
Stores character with ASCII values from 0 to 255.
float
Floating
32 Bits
Stores up to 4 Bytes of floating numbers.
double
Double Floating
64 Bits
Stores up to double the size of float.
bool
Boolean
8 Bits
Stores values whether True or False.

variableName is the name you will be defining for the variable. Which means, you decide what you call it. One good technique upon declaring variable names is through relating it with its purpose. In this way, you won’t easily forget the name of the variable. Also, as a rule for naming variables, you have to consider that: it should not start with a number; it should not contain any spaces and other special symbols such as #, $, %, * and more; it must not conflict with default C / C++ terms such as "if", "switch", "enum", and more.

Since variables are able to store values, depending on its nature, you can either assign a constant value for it or a value from another variable of the same data type. So, if the variable type is int, then you may only assign integer values for it.

To assign a constant value to a variable:

int num;
num = 15;

To assign a value from another variable:

int numVal;
numVal = num;

Remember that variables are very important in programming. It is always fully recommended to use variables most of the time. These help lessen confusion, prevent mistracking of values, and strengthen the construction of algorithms, which in the end improve the quality of programming.

Note: In the above C / C++ declarations, they all end in a character symbol ";". This tells the compiler that a statement has been ended or closed. So, if these won’t appear with each statement, it would most likely produce a compilation error. The compiler will always search for this symbol.

Get free blog up and running in minutes with Blogsome
Theme designed by Jay of onefinejay.com