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.
- 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.
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.
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
}
The general form of a function in C programming language:
return_type function_name (parameter list) {
body of the function
}
REFERENCES