Mind and Integrity College, Inc.
San Cristobal, Calamba City
In collaboration with
Department of Education
Region IV – CALABARZON
JAVA 1
Quarter 2 – Module 5:
FUNCTIONS, LOOPS, AND SWITCH – CASE
STATEMENT
SELF LEARNING MODULE
GRADE 11
Development Team:
Writer: Randy T. Mercado
Reviewer: Marife P. De Castro
Layout: Laurence E. Leonen
Management: Dr. Edwin T. Casila, MCL – Principal
Christian D. Manalansan – President
Mind and Integrity College, Inc.
Selina-Liz Bldg. National Hi-way, San Cristobal, Calamba City, Laguna
©2020 Mind and Integrity College, Inc. All Rights Reserved Page 1 of 11
Contact #: 049-531-1604 / 0908-965-0010
Email Address: [email protected]
©2020 Mind and Integrity College, Inc. All Rights Reserved Page 2 of 11
Lesson 5: FUNCTIONS, LOOPS, AND SWITCH – CASE
STATEMENT
What I Need to Know
SWITCH – CASE STATEMENTS
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 case.
SYNTAX PATTERN:
The following rules apply to a switch statement:
The expression used in a switch statement must have an integral or enumerated type, or be of a
class type in which the class has a single conversion function to an integral or enumerated type.
You can have any number of case statements within a switch. Each case is followed by the value to be
compared to and a colon.
The constant-expression for a case must be the same data type as the variable in the switch, and it
must be a constant or a literal.
When the variable being switched on is equal to a case, the statements following that case will
execute until a break statement is reached.
When a break statement is reached, the switch terminates, and the flow of control jumps to the next
line following the switch statement.
Not every case needs to contain a break. If no break appears, the flow of control will fall through to
subsequent cases until a break is reached.
A switch statement can have an optional default case, which must appear at the end of the switch.
The default case can be used for performing a task when none of the cases is true. No break is needed
in the default case.
©2020 Mind and Integrity College, Inc. All Rights Reserved Page 3 of 11
This would produce the following result:
What Is It
FUNCTIONS
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. You can
divide up your code into separate functions. How you divide up your code among different functions
is up to you, but logically the division usually is so each function performs a specific task.
FUNCTION DEFINITION
A function declaration tells the compiler about a function's name, return type, and parameters. A
function definition provides the actual body of the function.
©2020 Mind and Integrity College, Inc. All Rights Reserved Page 4 of 11
A C++ function definition consists of a function header and a function body. Here are all the parts of a
function:
Return Type: A function may return a value. The return_type is the data type of the value the
function returns. Some functions perform the desired operations without returning a value. In this
case, the return_type is the keyword void.
Function Name: This is the actual name of the function. The function name and the parameter list
together constitute the function signature.
Parameters: A parameter is like a placeholder. When a function is invoked, you pass a value to the
parameter. This value is referred to as actual parameter or argument. The parameter list refers to the
type, order, and number of the parameters of a function. Parameters are optional; that is, a function
may contain no parameters.
Function Body: The function body contains a collection of statements that define what the function
does.
Following is the source code for a function called max. This function takes two parameters num1 and num2
and returns the maximum between the two:
A function declaration tells the compiler about a function name and how to call the function. The
actual body of the function can be defined separately.
A function declaration has the following parts:
For the above defined function max, following is the function declaration:
Parameter names are not important in function declaration only their type is required, so following is
also valid declaration:
Function declaration is required when you define a function in one source file and you call that function in
another file. In such case, you should declare the function at the top of the file calling the function.
CALLING A FUNCTION
While creating a C++ function, you give a definition of what the function has to do. To use a function,
you will have to call or invoke that function.
When a program calls a function, program control is transferred to the called function. A called
function performs defined task and when its return statement is executed or when its function
ending/closing brace is reached, it returns program control back to the main program.
To call a function, you simply need to pass the required parameters along with function name, and if
function returns a value, then you can store returned value. For example:
©2020 Mind and Integrity College, Inc. All Rights Reserved Page 5 of 11
FUNCTION ARGUMENTS
If a function is to use arguments, it must declare variables that accept the values of the arguments.
These variables are called the formal parameters of the function.
The formal parameters behave like other local variables inside the function and are created upon
entry into the function and destroyed upon exit.
While calling a function, there are two ways that arguments can be passed to a function:
By default, C++ uses call by value to pass arguments. In general, this means that code within a
function cannot alter the arguments used to call the function and above mentioned example while
calling max function used the same method.
DEFAULT VALUES FOR PARAMETERS
When you define a function, you can specify a default value for each of the last parameters. This value
will be used if the corresponding argument is left blank when calling to the function.
This is done by using the assignment operator and assigning values for the arguments in the function
definition. If a value for that parameter is not passed when the function is called, the default given
value is used, but if a value is specified, this default value is ignored and the passed value is used
instead. Consider the following example:
©2020 Mind and Integrity College, Inc. All Rights Reserved Page 6 of 11
When the above code is compiled and executed, it produces the following result:
LOOPS
In computer programming, loops are used to repeat a block of code.
For example, let's say we want to show a message 100 times. Then instead of writing the print statement 100
times, we can use a loop.
That was just a simple example; we can achieve much more efficiency and sophistication in our programs by
making effective use of loops.
There are 3 types of loops in C++:
1. for Loop
Here,
initialization - initializes variables and is executed only once.
condition - if true, the body of for loop is executed; if false, the for loop is terminated
update - updates the value of initialized variables and again checks the condition. This is where the
increment/decrement operator takes place.
FOR LOOP FLOWCHART
©2020 Mind and Integrity College, Inc. All Rights Reserved Page 7 of 11
EXAMPLE:
Write a program that prints numbers from 1 to 5.
Output:
Here is how this program works
2. while loop
©2020 Mind and Integrity College, Inc. All Rights Reserved Page 8 of 11
Here,
A while loop evaluates the condition
If the condition evaluates to true, the code inside the while loop is executed.
The condition is evaluated again.
This process continues until the condition is false.
When the condition evaluates to false, the loop terminates.
EXAMPLE: Write a program that find the sum of positive number.
©2020 Mind and Integrity College, Inc. All Rights Reserved Page 9 of 11
In this program, the user is prompted to enter a number, which is stored in the variable number.
In order to store the sum of the numbers, we declare a variable sum and initialize it to the value of 0.
The while loop continues until the user enters a negative number. During each iteration, the number entered
by the user is added to the sum variable.
When the user enters a negative number, the loop terminates. Finally, the total sum is displayed.
3. do – while loop
The do...while loop is a variant of the while loop with one important difference: the body of do...while loop is
executed once before the condition is checked.
Here,
The body of the loop is executed at first. Then the condition is evaluated.
If the condition evaluates to true, the body of the loop inside the do statement is executed again.
The condition is evaluated once again.
If the condition evaluates to true, the body of the loop inside the do statement is executed again.
This process continues until the condition evaluates to false. Then the loop stops.
©2020 Mind and Integrity College, Inc. All Rights Reserved Page 10 of 11
EXAMPLE: Write a program that display numbers from 1 to 5
Here is how the program works.
SUMMARY
Functions While loop Function arguments
Switch – Case Statement Do – while loop Calling a functions
For loop Function parameters Loop
©2020 Mind and Integrity College, Inc. All Rights Reserved Page 11 of 11