C If … Else

Conditions and If Statements

You have already learned that C supports the usual logical conditions from mathematics:

  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b
  • Equal to a == b
  • Not Equal to: a != b

You can use these conditions to perform different actions for different decisions.

C has the following conditional statements:

  • Use if to specify a block of code to be executed, if a specified condition is true
  • Use else to specify a block of code to be executed, if the same condition is false
  • Use else if to specify a new condition to test, if the first condition is false
  • Use switch to specify many alternative blocks of code to be executed

Continue reading C If … Else

C Boolean Examples

Real Life Example

Let’s think of a “real life example” where we need to find out if a person is old enough to vote.

In the example below, we use the >= comparison operator to find out if the age (25) is greater than OR equal to the voting age limit, which is set to 18: Continue reading C Boolean Examples

C Booleans

Booleans

Very often, in programming, you will need a data type that can only have one of two values, like:

  • YES / NO
  • ON / OFF
  • TRUE / FALSE

For this, C has a bool data type, which is known as booleans.

Booleans represent values that are either true or false.


Continue reading C Booleans

C Operators

Operators

Operators are used to perform operations on variables and values.

In the example below, we use the + operator to add together two values:

Example

int myNum = 100 + 50;

Although the + operator is often used to add together two values, like in the example above, it can also be used to add together a variable and a value, or a variable and another variable. Continue reading C Operators

C Constants

Constants

If you don’t want others (or yourself) to change existing variable values, you can use the const keyword.

This will declare the variable as “constant”, which means unchangeable and read-only: Continue reading C Constants

C Type Conversion

Type Conversion

Sometimes, you have to convert the value of one data type to another type. This is known as type conversion.

For example, if you try to divide two integers, 5 by 2, you would expect the result to be 2.5. But since we are working with integers (and not floating-point values), the following example will just output 2: Continue reading C Type Conversion

C Data Types Examples

Real-Life Example

Here’s a real-life example of using different data types, to calculate and output the total cost of a number of items. Continue reading C Data Types Examples

C The sizeof Operator

Get the Memory Size

We introduced in the data types article that the memory size of a variable varies depending on the type:

Data Type Size
int 2 or 4 bytes
float 4 bytes
double 8 bytes
char 1 byte

The memory size refers to how much space a type occupies in the computer’s memory. Continue reading C The sizeof Operator

C Decimal Precision

Set Decimal Precision

You have probably already noticed that if you print a floating point number, the output will show many digits after the decimal point : Continue reading C Decimal Precision

C Numeric Data Types

Numeric Types

Use int when you need to store a whole number without decimals, like 35 or 1000, and float or double when you need a floating point number (with decimals), like 9.99 or 3.14515. Continue reading C Numeric Data Types