0% found this document useful (0 votes)
33 views

COMP 002 - Reviewer - Basic Program Structure

Reviewer

Uploaded by

jisoo0186
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

COMP 002 - Reviewer - Basic Program Structure

Reviewer

Uploaded by

jisoo0186
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

COMP 002 Reviewer: Basic Program Structure COMP 002

IBITS Year 17 | A.Y. 2023-2024 First Semester Midterms

C PROGRAM STRUCTURE ● Character (char)


○ A character is a single letter, digit, punctuation mark, or
● Documentation control symbol recognized by the computer. It is
○ Documentation provides information about the purpose, represented within single quotation marks, while literal
author, and date of creation. It often includes comments at strings are enclosed in double quotation marks. A
the beginning of the code enclosed in /* */ or //. character may be assigned an integer value between -128
and +127, and an unsigned char data type may be assigned
● Header Files an integer value from 0 to 255.
○ Header files, included using #include, provide declarations
for functions and macros used in the program. They act as ● Void
a way to import external code. ○ Void is a keyword in programming that denotes the
absence of a specific type or the lack of a value. It is often
● Main Function associated with functions, indicating that they do not
○ The main function is the entry point of the program. It return any value.
contains executable statements that define the program's ○ Three Uses of Void:
logic. ■ To declare explicitly a function as returning no value
■ To declare a function having no parameters
● Functions ■ To create generic pointers
○ Functions break down the program into modular pieces,
making the code more organized and readable. They can VARIABLE
be called from the main function or other functions.
● Variables
● Variables ○ In computer programming, variables act as named
○ Variables store data used in the program. They must be containers designed to store and manage values within a
declared with a type (e.g., int, float) before use. program. These identifiers can be assigned various data
types and are composed of 32 valid characters,
DATA TYPES encompassing letters, digits, and underscores.

● Integer (int) ● Variable Declaration


○ An integer is a whole number that may include an optional ○ In programming, every variable must undergo a two-step
sign (+ or -), followed by a sequence of digits. It does not process: declaration and definition. In C, a declaration
permit commas and is commonly utilized for controlling assigns a symbolic name to an object, such as a variable,
loops and conditional statements in programming. while a definition creates the actual object in memory.
Essentially, the declaration provides a name, and the
● Float (float) definition allocates memory for the variable.
○ A float is a numeric data type characterized by an optional
sign (+ or -), one or more digits, a decimal point, and one ● Variables Initialization
or more additional digits. It occupies 4 bytes of memory ○ Initialization is the act of assigning initial values to
and supports an optional exponent, ranging from 3.4 E to variables. It's crucial to note that variables are not
-38 to 3.4 E +38. automatically initialized; upon definition, they typically
hold arbitrary data, often referred to as "garbage." To
● Double (double) ensure meaningful data, it's necessary to explicitly initialize
○ A double is a specialized float with the capability to store variables or store values in them before accessing their
more significant digits and a longer exponent. It occupies 8 contents.
bytes in memory and spans a range from 1.7 E – 308 to 1.7
E +308.

COMP 002: Computer Programming Basic Program Structure 1


COMP 002 Reviewer: Basic Program Structure COMP 002

IBITS Year 17 | A.Y. 2023-2024 First Semester Midterms

○ Several ways to initialize a Variable: ● Declaration and Syntax


■ By assigning an assignment statement (=) may be ○ Using # define TOTAL 5
used to assign values to variables of any types. ○ Using const <data-type> variable = 5
eg. x = 5;
■ By using function scanf INPUT / OUTPUT STATEMENTS
eg. scanf(“%d”, &x);
■ By assigning values during declaration:
● Output Statement
eg. int x = 5;
○ The printf() function in C is used for formatted output.
While technically not part of the C Language itself, it
GLOBAL AND LOCAL VARIABLES belongs to the C system and is accessible through a library.
Programmers need to declare the function before use, and
● Global Variables ANSI C introduced function prototypes for more organized
○ Global variables undergo initialization exclusively at the declarations. The prototypes for printf() and scanf() are
beginning of the program. If no other initializer is specified, found in the stdio.h header file, so including it is advisable
all global variables are automatically set to zero. These when using these functions in a C program.
variables retain their values throughout the entire program
execution, providing a consistent state that can be ● Input Statement
accessed from any part of the code. ○ In C, the scanf() function is used for reading input from the
standard input (typically the keyboard) and assigning
● Local Variables values to variables. It allows you to interactively receive
○ In contrast, local variables are initialized every time the data from the user or read from other input sources.
function in which they are declared is entered. Unlike
global variables, their values are specific to the scope of ● Format Specifies
the function. This means that each time the function is ○ Character (“%c”)
invoked, local variables start with a fresh initialization, ○ String (“%s”)
ensuring that their values are specific to the current ○ Integer (“%d”)
execution context within the function. ○ Float (“%f”)
○ Double (“%lf”)
CONSTANTS
EXPRESSION
● Constants in Turbo C refer to fixed values that remain unaltered
throughout the program's execution. These constants can ● In programming, an expression is a combination of values,
belong to any of the basic data types and are often referred to variables, operators, and function calls that, when evaluated,
as Literal Constants. Unlike variables, which can change during results in a single value. Expressions can represent
the program's runtime, constants maintain a fixed value, computations, comparisons, or assignments. They are the
providing stability to specific values used in the code. building blocks of more complex statements and algorithms in a
programming language.
○ Character Constants
eg. ‘A’, ‘c’, ‘Z’
○ String Constants
eg. ‘PUP’, ‘BSIT’, ‘Zoo’
○ Integer Constants
eg. 10, 100, 1000
○ Floating Constants
eg. 3.14, 2.98, 1.50

COMP 002: Computer Programming Basic Program Structure 2


COMP 002 Reviewer: Basic Program Structure COMP 002

IBITS Year 17 | A.Y. 2023-2024 First Semester Midterms

ARITHMETIC RELATIONAL

● Multiplication ● Greater than (>)


○ The multiplication operation involves combining two or ○ This operator compares two values and evaluates to true if
more values to obtain their product. It is denoted by the the value on the left is greater than the value on the right.
asterisk (*) symbol.
● Less than (<)
● Division ○ The less than operator compares two values and results in
○ Division is the process of distributing a value by another to true if the value on the left is less than the value on the
obtain the quotient. It is represented by the forward slash right.
(/) symbol.
● Greater than or Equal to (>=)
● Addition ○ This operator returns true if the value on the left is greater
○ Addition combines two or more values to produce a sum. than or equal to the value on the right.
It is denoted by the plus (+) symbol.
● Less than or Equal to (<=)
● Subtraction ○ The less than or equal operator evaluates to true if the
○ Subtraction involves deducting one value from another to value on the left is less than or equal to the value on the
yield the difference. It is represented by the minus (-) right.
symbol.
● Equal to (==)
● Negation ○ The equal to operator checks if the values on both sides
○ Negation is the unary operation of reversing the sign of a are equal and returns true if they are, and false otherwise.
value. It is typically denoted by a minus sign before a single
value. ● Not Equal to (!=)
○ The not equal to operator determines if the values on both
● Modulus Division sides are not equal and results in true if they are different,
○ Modulus division returns the remainder when one value is false if they are equal.
divided by another. It is denoted by the percent sign (%).
LOGICAL
● Increment
○ Increment involves increasing the value of a variable by a ● And
specified amount, often one. It is denoted by the double ○ The "and" operator, represented by &&, combines two
plus sign (++). conditions and evaluates to true only if both conditions are
true. If either or both conditions are false, the overall
● Decrement result is false.
○ Decrement decreases the value of a variable by a specified ● Or
amount, often one. It is represented by the double minus ○ The "or" operator, denoted by ||, combines two conditions
sign (--). and evaluates to true if at least one of the conditions is
true. It results in false only if both conditions are false.
● Parenthesis ● Not
○ Parentheses are used to group expressions and control the ○ The "not" operator, symbolized by !, negates a given
order of operations in arithmetic. They are represented by condition. If the condition is true, applying the "not"
the opening and closing brackets, ( and ). operator makes it false, and vice versa.

COMP 002: Computer Programming Basic Program Structure 3


COMP 002 Reviewer: Basic Program Structure COMP 002

IBITS Year 17 | A.Y. 2023-2024 First Semester Midterms

OTHER OPERATORS

● Ternary ( ? true:false )
○ The ternary operator, also known as the conditional
operator, is a shorthand way of writing an if-else statement
in a single line. It evaluates the "condition" and returns the
value of "expression_if_true" if the condition is true, and
the value of "expression_if_false" otherwise.

● Comma
○ The comma operator allows multiple expressions to be
grouped together. It evaluates each expression from left to
right and returns the value of the last expression.

○ It is often used in situations where multiple expressions


need to be executed sequentially, such as in a for loop.

● Compound

○ Addition Assignment
■ The += operator adds the value on the right-hand side
to the variable on the left-hand side and assigns the
result to the variable on the left-hand side. It is a
shorthand for writing variable = variable + value.

○ Subtraction Assignment
■ The -= operator subtracts the value on the right-hand
side from the variable on the left-hand side and
assigns the result to the variable on the left-hand
side. It is a shorthand for writing variable = variable -
value.

○ Multiplication Assignment
■ The *= operator multiplies the variable on the
left-hand side by the value on the right-hand side and
assigns the result to the variable on the left-hand
side. It is a shorthand for writing variable = variable *
value.

○ Division Assignment
■ The /= operator divides the variable on the left-hand
side by the value on the right-hand side and assigns
the result to the variable on the left-hand side. It is a
shorthand for writing variable = variable / value.

COMP 002: Computer Programming Basic Program Structure 4

You might also like