Wednesday, March 11, 2015

Conditional Statements

Previous Topic    Course Outline    Next Topic

Time to write a simple if-else statement.

1
2
3
4
5
if (<condition>) {
    //statements to be executed when condition is true
} else {
    //statements to be executed when condition is false
}

The else block is optional. If no action is required when the condition is false, omit the else block.


Nested-If example:

1
2
3
4
5
6
7
8
if (<condition>) {
    //statement when condition is true
} else {
    //some statements here
    if (<another condition>) {
       //statements to be executed when <another condition> is false
    }
}

Use nested-if when you need to perform some statements outside the inner if-block but if you don't need to put something in //some statements here, you can use the else-if block.

 1
 2
 3
 4
 5
 6
 7
 8
 9
if (<condition1>) {
    //statement1
} else if (<condition2>) {
    //statement2
} else if (<condition3>) {
    //statement3 when condition is true
} else {
    //statements to be executed when all condition fails
}

Only the block under the first condition that equates to true will be executed. So yeah, I'm basically saying that the order is important. It's a good practice to arrange the conditions from the most to the least probable. Even in real life, we first verify the obvious before resorting to a new hypothesis.


Previous Topic    Course Outline    Next Topic

Tuesday, March 10, 2015

Arithmethic and Logical Expressions

Previous Topic    Course Outline    Next Topic

Arithmetic Expressions


One of the basic things you can do in a program is to evaluate arithmetic expressions.

float result = (1+2)-3*2/2;

The program will store the resulting value of this expression in a float variable result. Your programs are intelligent enough to perform the evaluation following the PEMDAS.

Shortcuts

i++; // i = i + 1, adds 1 to the value of the variable and stores it back to the variable
i--; // i = i - 1
i += 2; // i = i + 2, basically, 2 can be any number

Logical Expressions


The next basic and most common thing you should learn is to evaluate logical expressions.

|| for logical OR
&& for logical AND
! for negation (returns 0 for positive values, 1 otherwise)

Logical operations used to evaluate expressions.

1
2
3
4
5
6
int gameOver = 1;
int win = 0;

if (gameOver && !win) {
    printf("Sorry, you lose!");
}

1
2
3
4
int hasBeenKickedOut = 1;
int isSuspended = 1;

bool isNotGoingToSchool = hasBeenKickedOut || isSuspended;

I think that was my first time (if not, then one of the few times) I stored the resulting value of a logical expression in a variable. Usually it is used inside an if or else-if statement or as a return value of a function (your will know more about function later in this course).

1
2
3
if (hasBeenKickedOut || isSuspended) {
    isNotGoingToSchool = 1;
}

Previous Topic    Course Outline    Next Topic

Variables

Previous Topic    Course Outline    Next Topic

Variables are simply where you store data. A variable has a name and data type.

Data Types


Different Data Types in C
intinteger
charcharacter
floatfloating point value; decimal
doubledouble precision floating point value

I only listed the basic but this would be enough to make a simple program.

So yeah, basically data types exist so that the compiler will know what are you trying to store in a specific variable. Error will be raised when you try to store a character in an int variable.

Variable Declaration


Variable declaration consists of the data type and the variable name. Of course, every variable must have a unique name. You have to declare the variable first before using it.


Examples:

1
2
int age;
char gender;

The value of the variables above is null after their declaration but you can also define a default value like:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
/* A variable to determine whether a game is already over. And of
 * course, it has to be false at the start of the game.
 * 
 * By the way, this is the correct way of writing multiple-lined
 * comments in C and in many more programming languages.
 */
int isGameOver = 0;

// I was kind of feminist, you know. 
char gender = 'F';


Previous Topic    Course Outline    Next Topic

Sunday, March 1, 2015

Let's see how we’ll C!

Course Outline   Next Topic

C is a very good language to start learning the art of computer programming. It’s simple and straight forward.  

Without further ado, let’s start writing your first program.

First you need an IDE (integrated development environment, believe it or not, I still have to Google what this acronym stands for but don't worry you don't need to know). I suggest to use Code::Blocks. Well, CodeBlocks simply build your program. You can write code and run/execute them directly here.

Save this as hello.c.

1
2
3
4
5
#include <stdio.h>
     
main() {     
    printf("Hello World!");   
}
    Line 1 just imports the library we needed. Just imagine libraries as collection of codes that let you use commands defined on them. stdio is the standard library for input/output. It’s one of the basic libraries in C.

    In Line 3, we have the main(). The program executes what is inside the main block.

    Line 4 is the code to print a simple text in C. Semicolon (;) is what you used to terminate a statement or command. It's like a period (.) in English and other languages. But take note, your sentence can be understood without a period (like this one) but your C statement can't without a semicolon It will result to a compilation error. 

    So yeah, basically that would be the skeleton of most C programs. 

    After saving, build and run the program using Code::Blocks. 

    There you have it! Your first hello word program! And I’m sure there are still many to come!

    Course Outline   Next Topic

    Introduction to Computer Science

    Yay! Welcome to our first course. 

    As an introduction, I’m not going to rant about the history of computers and microchips nor talk about the latest technologies and mathematical theorems.

    Skip all that crap. All I want you to know is:

    Computing Science is a very interesting field. It’s math. It’s science. It’s technology. It’s logic.

    In this course, you’ll learn how to write your own program and grasp a deeper understanding of what this fascinating field is all about.

    Come on! Let’s jive!

    Course Outline


    Exercises: