CP1 Reviewer 1stfinals Full
CP1 Reviewer 1stfinals Full
CP1 Reviewer 1stfinals Full
+, - Left to right
CONTROL STRUCTURE
● Analyzes and chooses in which direction a program <, <=, >, >= Left to right
flows based on certain parameters or conditions.
==, != Left to right
● Way to specify flow of control in programs.
● Any algorithm or program can be clearer and more && Left to right
understood if they use control structures as self-
contained modules.
|| Left to right
● Three types of logic/flow of control:
o Sequence logic/flow =, +=, -=, *=, /=, etc. Right to left
o Selection logic/conditional flow
o Iteration logic/repetitive flow
• ! operator is unary (operator with only one operand).
FLOW OF CONTROL
• All other relational, equality and logical operators
are binary.
SEQUENTIAL FLOW OF CONTROL o 0 or 0.0 or \0 or NULL = false.
● Statements in a program are normally executed one o Any non-zero = true.
after another. COMPOUND STATEMENT
● Often it is desirable to alter the sequential flow of control
● Series of declarations and statements surrounded by
to provide for a choice of action, or repetition of action.
braces. It is also known as block.
● By means of if, if-else, and switch statement, a
● Purpose:
selection among alternative actions is made.
o Grouping statements into an executable unit.
● By means of while, for and do statements, interactive o Achieve the desired flow of control in if, if-else,
actions can be taken. while, for, and switch statements.
● Syntax:
RELATIONAL, EQUALITY, LOGICAL OPERATORS compound_statement{{declaration}{statement}}
EQUALITY OPERATORS
CONDITIONAL STATEMENTS
== Equal expr == expr The if statement
● Form:
!= Not equal expr != expr if (expr)
statement;
EQUALITY OPERATORS ● Action: If expr is non-zero (true), then statement is
executed; otherwise statement is skipped control
! (unary) Negation !expr passes to the next statement.
● Example:
&& Logical “end” expr && expr if (grade >= 90)
printf(“Congratulations!);
|| Logical “or” expr || expr printf(“Your grade is %d\n”, grade);
The if-else statement ● continue; Causes the current iteration of the loop to
● Form: stop and causes the next iteration of the loop to begin
if (expr) immediately. Occurs inside for, while, and do loops.
statement; ● Example:
else do {
statement2; scanf(“%d”,&num);
● Action: If expr is non-zero (true), then statement1 is
if (x < 0 )
continue;
executed and statement2 is skipped; if expr is 0 (false), printf(“%d”,x);
then statement1 is skipped and statement2 is executed. } while (x != 100);
If both cases are 0, it will pass to the next statement.
● Example: An if-else statement can be used as the statement
if (a > b) { part of another if statement, this is what you
b++; call nested if.
printf(“Value is smaller %d\n”, b);
} ● Example:
else if(x == 50) {
printf(“You got a bigger value %d\n”, a); if(y >= 120) {
sum = x + y;
An if-else statement can be used as the statement printf(“the sum of x and y is %d”, sum);
}
part of another if statement, this is what you
else {
call nested if. diff = x – y;
printf(“the difference between x and y is %d”, diff);
● Example: }
if(x == 50) { else {
if(y >= 120) { printf(“Next time”);
sum = x + y; }
printf(“the sum of x and y is %d”, sum); LOOPS
}
else { ● Repetition of action in computers where a looping
diff = x – y; control mechanism executes specific statements, which
printf(“the difference between x and y is %d”, diff); is convenient for fixing large amounts of data.
}
else { The while statement
printf(“Next time”);
}
● Form:
while(expr)
The switch statement statement;
● Form: next statement;
switch(expression) {
case <constant expression>:
● Action: Expr is evaluated. If expr is non-zero (true), then
statement is executed and passed back at the beginning
statement;
of loop; when it reaches expr as 0 (false), the control will
break;
execute the next statement.
case <constant expression>:
statement; ● Example:
break; while(number != 0) {
default: scanf (“%d”,&number);
statement; sum += number;
}
● Action: Evaluates to an int, conditional, char, or double printf(“ the sum of all numbers is %d”,sum);
value.
- Case list: consists of constants whose type matches
The for statement
that of the switch expression. (no var/expr) ● Form:
- Break: at the point, execution should jump to the end for(initialization; condition; increment)
of the switch statement. statement;
- Default: specifies an action to be taken if the value ● Action: Allows many variants, but there are three parts:
of the switch expression does not match any of the o Initialization – assignment statement that is used to
listed values. set the loop control variable.
o Condition – expression that determines when the
Unconditional Transfer Statement loop will exit.
● break(); Used to terminate a case in a switch statement; o Increment - defines how the loop control variable will
terminates loop through bypassing, but program control change each time the loop is repeated.
resumes at the next statement. ● Example:
● Example: for(x = 100; x != 65; x += 5) {
while(1) { z = sqrt(x);
scanf(“%lf”, &x); printf(“The square root of %d is %f”,x, z);
if(x < 0.0) }
break;
printf(“%f\n”, sqrt(x)); An important point above for loop is that conditional
} test is always performed at the top of the loop. This
means that the code inside the loop may not be executed
at all if the condition is FALSE to begin with. For loops
can be also nested.
1ST SEMESTER: FINALS EXAMINATION
CP1 – COMPUTER PROGRAMMING (COMP 002)
Geuel John D. Rivera | BSCS 1-5 | Prof. Arnie Fabregas
INITIALIZATION
● They are defined in the same manner as ordinary ● Initialization of all elements in an array can be done at
variables, except that each array name must be the time of declaration and definition, just as with
accompanied by a size specification (number of variables.
elements).
● For each element in the array we provide a value. The
● For one dimensional array, the size is specified by a only difference is that the values must be enclosed
positive integer expression enclosed in square brackets. in braces and, if there are more than one, separated
● General Form: by commas.
storage class data-type array[expression]; ● It is a compile error to specify more values than there
are elements n the array. The initial values must appear
on the order in which they will be assigned to the
1ST SEMESTER: FINALS EXAMINATION
CP1 – COMPUTER PROGRAMMING (COMP 002)
Geuel John D. Rivera | BSCS 1-5 | Prof. Arnie Fabregas
● Local variables - any variables declared in the body of ● ANSI C provides a new function declaration syntax
function. called functional prototype.
● Global variables – declared external to the function. ● It tells the compiler the number and the type of
arguments that are to be passed to the function and the
Local variables… type of the value that is to be returned by the function.
● Syntax:
● Are referred to as automatic variables or the keyword type function_name(parameter type list);
auto. ● Example:
● Can be referenced only by statements that are inside the double sqrt(double);
block in which the variables are declared.
● Not known outside their own code. A block of code is Tells the compiler that sqrt() is a function that takes a single
begun and terminated when an opening and closing argument of type double and returns a double.
curly brace ( { } ) is encountered.
● Exists only while the block code in which they are ● Function prototype allows the compiler to check the
declared is executing. They are created upon entry into code more thoroughly.
its block and destroyed upon exit. ● Also, values passed to function are properly coerced.
GLOBAL VARIABLE TO A FUNCTION NOTE: In C++, function prototypes are required and the
● They are known throughout the entire program and use of void in the parameter type list in both function
maybe used in any piece of code. prototypes and the function definition is optional.
● They hold their values during the entire execution of the
program. ● Example:
● They are created by declaring them outside of any
Void func(); [equivalent to void func(void);]
function. They maybe accessed by any expression
regardless of what function that expression is in. PARAMETER LISTS
● The storage for global variables is in fixed region of ● A function header identified the parameters which are to
memory set aside for this purpose by the compiler. be passed to the function.
● Helpful when the same amount of data is used in many ● In the header and in the actual body of the function, the
functions in your program. parameters used are FORMAL parameters which are
replaced by the ACTUAL parameter when the function
AVOIDING UNNECESSARY GLOBAL VARIABLES is called.
● Since the FORMAL parameters are eventually replaced,
1. They take up memory the entire time of your program is they are not program variable.
executing not just when they are needed. ● In particular, you can assign to FORMAL parameters the
2. Relying to global variables instead of local variables. It names of program variable which will eventually replace
makes the function less general because it relies on a them.
variable outside of it.
3. Overuse of global variables that can lead to program FUNCTION INVOCATION AND CALL BY VALUE
error because of unknown and unwanted side effects. ● A program consists of one or more function definitions,
THE return STATEMENT including main(), which serves as the entry point for
● May or may not include in an expression (void data type) program execution.
● Syntax: ● When encountering a function name, program control
return; // return(expression); transfers to that function, known as invocation or
● Example: calling.
float f(char a, char b, char c){ ● Upon completing its tasks, control returns to the calling
int i; environment for further execution.
…
return i; /*the value will be converted to float*/ ● Functions are invoked by specifying their names
} followed by arguments enclosed in parentheses.
● There can be zero or more return statements in a ● Arguments provided during invocation should match in
function. If there is no return statement, then control number and type (or compatible type) with the
variable is passed back to the calling environment when parameters defined in the function.
the closing brace of the body is encountered (“falling off ● Type compatibility is enforced by the compiler when
the end”). function prototypes are employed.
● Arguments are passed "call by value," implying that
● Example (program segment): each argument's value is locally used in place of the
double value(double x){ corresponding formal parameter.
if (x >= 0.0) ● Function calls can originate from either the main
return x;
else
program or within other functions, with the function
return -x; initiating the call termed the calling function or calling
} environment.
● Value parameter – copy of a variable which is private to
FUNCTION PROTOTYPES the function. The function is given a copy of the actual
● Functions should be declared before they are used. parameter to manipulate. However, the manipulation
does not affect any of the variables in the calling
1ST SEMESTER: FINALS EXAMINATION
CP1 – COMPUTER PROGRAMMING (COMP 002)
Geuel John D. Rivera | BSCS 1-5 | Prof. Arnie Fabregas
program.
● Example:
#include <stdio.h>
// Global declaration
int sum;
void func_sample(int y);
func_sample(int y){
y *= 3;
printf(“The new value of y is %d”, y);
}