Selasa, 20 September 2016

Decision Making, Looping, Array, Function

Decision Making

Decision making structures require that the programmer specifies one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.

  1. If, else if.
An if statement consists of a Boolean expression followed by one or more statements. An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. Or for easy way to remember, if the “if condition” was not fulfilled, it will be run with the next condition which is “else if”, and if it is also not fulfilled by “else if” condition, it will be run by the last condition which is “else”.
The syntax for if, else if is:
if (boolean_expression) {
     statement1 /*will execute if the boolean expression is true*/
}
else {
     statement /*will execute when the boolean expression is false*/
} 

note: /* */ is a comment, it will not be show in the program.




2. Switch
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.


What would happen if we erase the "break;" on case 2? It  will happen like this(look at the picture below). The program will execute and it will stop until it find the "break;" That's why the program show two cases.





LOOPING
When a block of code needs to be executed several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.
A loop statement allows us to execute a statement or group of statements multiple times. 
1.     While
A while loop might not execute at all. When the condition is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.
The syntax of a while loop is like this
while (condition) {
      statement(s);

}
For example look at the picture below.


2. Do-while
Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop in C programming checks its condition at the bottom of the loop.
A do...while loop is similar to a while loop, except the fact that it is guaranteed to execute  the statement(s) at least one time, and then it will checks the condition later on.
If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop executes again. This process repeats until the given condition becomes false.
The syntax of a do while loop is like this:
do {
    statement(s);
} while (condition);
For example look at the picture below.

3.  For 
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.The syntax of a for loop is like this.
for (init/start; condition; increment/step) {
     statement(s);
}
For example look at the picture below.




ARRAY
Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A specific element in an array is accessed by an index.
type arrayName [arraySize];



FUNCTION
A function is a group of statements that together perform a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions.
The general form of a function in C programming language:
return_type function_name (parameter list) {
   body of the function
}










REFERENCES

Jumat, 09 September 2016

THE DIFFERENCE BETWEEN IDE and COMPILER


Compiler:  Compiler is a part of IDE which transform the source code that you write into a machine code or computer language.

Integrated Development Environment : IDE is the one that run your compilers, give facilities for software development and it can not run without a compiler. 


How to Install C/C++ and IDE (Cygwin – Netbeans – windows10)

I’m going to show you guys how to install Cygwin and Netbeans on windows 10.
First up Netbeans.
2.  Download the C/C++ as supported technologies by clicking the Download x86 option on the C/C++ column (look at the picture below).

3. Once you click, it will be downloaded automatically.
 4. Open the recently downloaded Netbeans. Run the  program, and click next.




 5. And that’s it. You have installed the Netbeans IDE


Now I’m going to show you guys how to install Cygwin (C/C++).
1.      1. Go to https://cygwin.com/install.html . There are two versions in Cygwin, you can choose between 32 bit versions or 64 bit versions (I am using the 32 bit versions, because netbeans is also 32 bit versions, so the c/c++ (Cygwin) bit versions has to match with the IDE (netbeans)).
 2.      Open and run the Cygwin. And just like installing Netbeans, just keep clicking next.
  3.    After you download it, you have to select packages to download.  Packages that are required to download for Netbeans must be at least gcc-core : Compiler, gcc-g++: C++ compiler, gdb: The GNU Debugger, make: the GNU version of the 'make' utility. You can find all of it on Devel. Download the packages first by clicking the “+” sign beside Devel, then find the gcc-core:Compiler and others, and click the Skip label beside it, which are going to reveal the package version number. Click next.
 4.      Cygwin and the packages are installed.
 5.      Now we have to add the Cygwin compiler to our path to enable Netbeans IDE. How? First open the control panel, and type “var” on the Search Control Panel box. Click “Edit the System Environment Variables”.
6.  Click the “Advandced” tab and the “Environment Variables”
 7.      On the environment variables panel, select Path, and click the Edit. And browse the Cygwin path. And click Ok. Now netbeans and Cygwin are good to go.






How To Compile A Simple Source Code


1. Open Netbeans 8.1. Select the “New Project” on the left top, or you can just press Ctrl+Shift+N to make it fast.
 2.   On the choose project step, select the Samples and then C/C++ categories. After that choose the “Welcome” projects. Click next.
 3.      Write the new project name and browse the location. My new project name is “What’s Up Everyone”. Click finish.
 4. Right click the What’s Up Everyone files on the left side, and select debug

 5.      Now simply change the “Welcome…” to “What’s Up Everyone”.  (from number 37 there are actually more, but for example like this you could just erase those part, because this blog is just to explain the easy (beginner) way).  And then right-click the “What’s Up Everyone” file, and select  Build and right-click again and select Run. It’s done…
 6.      You also can add some other simple words like the pictures below.  




ThankYou....