Algoritma & Pemrograman – Pertemuan 3 (Review)

Selection

> Basically, an instruction or set of one that may or may not be executed, with a predetermined condition as the deciding factor.

> Syntax type:

  • If
  • If – Else
  • Switch-case

> Syntax:

  1. If
  • if (boolean expression) statement;

or

  • if (boolean expression){

    statement1;

    statement2;

    ……

    }

If the boolean expression is true, then a statement or set of statements will be executed.

2. If – Else

  • if (boolean expression) statement1; else statement2;

or

  • if (boolean expression){statement1;

    statement2;

    ……

    }

    else {

    statement3;

    statement4;

    }

If boolean expression is true, then statement 1 or set of statement 1 will be executed. If false, then statement 2 or set of statement 2 will be executed.

3. Nested-If

  • if (boolean expression) statement1;if (boolean expression) statement2;

    if (boolean expression) statement3;

or

  • if (boolean expression) statement1;else

    if (boolean expression) statement2;

    else

    if (boolean expression) statement3;

  • Only occurs when the word IF appeared more than once within IF statement.

> Switch-case

  • Basically, only use this if the numbers in if-else is too much of a pain for you to calculate.
  • If matches with a case constant value, then it will execute related statement/s.
  • If nothing matches, it’ll execute default statement/s

Syntax:

  • switch (expression) {

    case constant1 : statements1; break;

    case constant2 : statements2; break;

    default : statements;

    }

> ?:

  • Similar to IF statement, but returns value.

Syntax:

  • condition ? then-expression : else-expression

Example:

  • if(a > b)

    max_value = a;

    else

    max_value = b;

    as

    max_value = (a > b) ? a : b;

Error Type

  1. Compile-Time Error
  • Caused by Syntax Error

2. Link-Time Error

  • Successfully compile, but caused link error
  • Caused by no object code at link time

3. Run-Time Error

  • Successfully compiled, but error at runtime.
  • Usually caused  by numerical operation such as: overflow, floating point underflow, division by zero, etc.

4. Logical Error

  • Wrong result caused by incorrect flow / algorithm.

 

For examples on Errors, please try coding with wrong codes at Visual Express C++, or you can search for screenshots of examples at Google. I’m too lazy to give one to you. It’s a pain.

This entry was posted in BINUS University, Lessons, Review. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *