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

05 Control Structure Looping With The While Statement

The document discusses using the while loop statement in C++. It provides examples of programs that use while loops to perform input validation and repeatedly request login credentials until the user enters the correct username and password. The key aspects covered are: - The while loop repeats statements as long as a test condition is true, unlike the if statement which only executes once. - In the input validation example, the variable is initialized to -1 so it will loop while the input is negative. - The login example loops while the username or password do not match the required values, guaranteeing both are correct. - Loops allow programs to perform repetitive tasks like input validation and requesting login credentials.

Uploaded by

Justine Flores
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

05 Control Structure Looping With The While Statement

The document discusses using the while loop statement in C++. It provides examples of programs that use while loops to perform input validation and repeatedly request login credentials until the user enters the correct username and password. The key aspects covered are: - The while loop repeats statements as long as a test condition is true, unlike the if statement which only executes once. - In the input validation example, the variable is initialized to -1 so it will loop while the input is negative. - The login example loops while the username or password do not match the required values, guaranteeing both are correct. - Loops allow programs to perform repetitive tasks like input validation and requesting login credentials.

Uploaded by

Justine Flores
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

ACTIVITY NO.

__
Control Structure – Looping with the “while” Statement

I. INTRODUCTION
There are instances wherein a certain task has to be repeated when a given condition is
satisfied. This repetitive task or iterative task is known as loop.
In c++, there are 3 statements that are used to accomplish a loop: while, do-while and for
statements. These statements are categorized as control structure statements.
The more general structure for looping is the while statement. The syntax is straight-forward
and easy to comprehend. Its flow of execution is very similar to the if statement. However, in the case of
the while statement, the flow goes back to the testing of the condition instead of finishing the execution
of the statement. It keeps on looping as long as its test condition is true. The following diagrams show
the comparison between the if statement and the while statement.

II. OBJECTIVES
1. To learn how to loop works
2. To use the while statement in solving repetitive task
3. To write a program that uses the while statement to solve a problem that requires input
validation

III. MATERIALS/APPARATUS/EQUIPMENT
1. Computer unit
2. Codeblock IDE with gcc mingw c++ compiler
IV. PROCEDURE
1. Turn on the computer unit and run Codeblock IDE.
2. Create a new file and save it with the filename “InputValidationUsing_while.cpp.”
3. Write the following program code, compile (build) and run.

Output:

4. Create a new file and save it with the filename “securityCodesUsing_while.cpp.”


5. Write a program that keeps on asking the user for the username and the password until the user
enters the valid inputs whereby it displays the message “INVALID CREDENTIALS!” every time any
of the inputs provided is wrong. Then the program displays the message “ACCESS GRANTED”
when the user has finally entered the correct inputs. The username should be “Admin” and the
valid password is your firstname_followed_by_your_birthday (example: Delgado0229). The
Sample Output is as follows:
Enter username: admin
Enter password: Delgado0229
INVALID CREDENTIALS!

Enter username: Admin


Enter password: delgado0229
INVALID CREDENTIALS!

Enter username: Admin


Enter password: Delgado229
INVALID CREDENTIALS!

Enter username: Admin


Enter password: Delgado0229
ACCESS GRANTED!

Declare your variables as String so that you can easily compare the variable to a string literal like
ngalan == “Admin” as well as sekreto == “Delgado0229”. Use a variable name related to the data it
holds. As a general rule, use a variable name germane to its purpose.

6. Save, compile (build) and run.

Write Your Program Code below(securityCodesUsing_while.cpp):


V. DISCUSSION
1. What is the main difference of if statement and while statement as regards the flow of
execution of the statements?

The while loop is, as its name suggests, a loop, while the if is a one-time branching action. Meaning that
an if statement provides you the choice of doing something or not once (or something else). While a
while loop only executes commands while the condition is true.

2. Why is it that the variable numberOfMales is initialized to -1 in the program


InputValidationUsing_while.cpp? Relate it to the test condition used in the while statement.

The variable is set to -1 so that the statement will loop while the variable's value is a negative integer.
By doing this, the user's input will be verified as a positive number before the code is continued.

3. Discuss the test condition you used in securityCodesUsing_while.cpp that guarantees the
validity of both username and password inputs.

while (username != "Admin" || password != "Dennis0106"); It means that if the user or password values
do not match the required input, the code will loop.

Write down below some of the outputs of your program in securityCodesUsing_while.cpp.

cout << "Enter username: ";


cin >> username;
cout << "Enter password: ";
cin >> password;

if (username != "Admin" || password != "Dennis0106")


{
cout << "Invalid Credentials" << endl;
}
else
{
cout << "Access Granted" << endl;
}
}
while (username != "Admin" || password != "Dennis0106");

VI. CONCLUSION
We focused more on loops. In addition to the last lesson, we learned which is good decision making, we
can apply what we learned into loops. So we can make the program do repetitive tasks like making login
credentials or multiplying numbers.

You might also like