You are on page 1of 6

ICCT COLLEGES FOUNDATION INC.

V.V Soliven Avenue II., Cainta, Rizal

Assignment in CC02

Submitted to
Mr. Ricardo Pilon Barcinas

Submitted By
Nica Claudine G. Mazo

First year BSCS


M10

March 25, 2020


 THE WHILE STATEMENT

 INTERACTIVE WHLE LOOP

-Sentinels
-Break and Continue Statements
-The Null Statement

 THE FOR STATEMENT


-Interactive For Loop
-Nested Loop

 THE DO-WHILE STATEMENT


-Validity Checks

 COMMON PROGRAMMING ERRORS


THE WHILE STATEMENT
A while statement is a general repetition statement that can be used in a variety of
programming situations. It has this general form:

while (expression)
statement;

The expression in parentheses is evaluated in exactly the same manner as one in an if-else
statement; the difference is in how the expression is used. As you have seen, when the expression in an
if-else statement is true (has a non-zero value), the statement following the expression is executed
once. In a while statement, the statement following the expression is executed repeatedly as long as the
expression evaluates to a non-zero value. Naturally, this means that somewhere in the while statement
must be a statement altering the tested expression’s value. As you’ll see, this is indeed the case. For
now, however, considering just the expression and the statement following the parentheses, the
computer uses this process in evaluating a while statement:

1. Test the expression


2. If the expression has a non-zero (true) value
a. execute the statement following the parentheses
b. go back to Step 1
else
exit the while statement and execute the next executable statement following the while
statement
Notice that Step 2b forces program control to be transferred back to Step 1. This transfer of
control back to the start of a while statement to reevaluate the expression is what forms the program
loop. The while statement literally loops back on itself to recheck the expression until it evaluates to
zero (becomes false). This rechecking means the loop must contain a provision that permits altering the
tested expression’s value. As you’ll see, this provision is indeed made.

THE INTERACTIVE WHILE LOOP


Combining interactive data entry with the repetition capabilities of the while statement
produces adaptable and powerful programs. To understand the concept, take a look at Program 5.5, in
which a while statement is used to accept and then display four user-entered numbers, one at a time.
Although the program uses a simple idea, it highlights the flow of control concepts needed to produce
more useful programs.
The following is a sample run of Program 5.5. The bolded numbers were input in response to the
prompts:
This program will ask you to enter 4 numbers.

Enter a number: 26.2


The number entered is 26.2
Enter a number: 5
The number entered is 5
Enter a number: 103.456
The number entered is 103.456
Enter a number: 1267.89
The number entered is 1267.89

The first message displayed is caused by execution of the first cout statement. This statement is
outside and before the while statement, so it’s executed once, before any statement in the while loop
After the while loop is entered, the statements in the compound statement are executed while
the tested condition is true. The first time through the compound statement, the message Enter a
number: is displayed. The program then calls cin, which forces the computer to wait for a number to be
entered at the keyboard. After a number is typed and the Enter key is pressed, the cout object displays
the number. The variable count is then incremented by 1. This process continues until four passes
through the loop have been made and the value of count is 5. Each pass causes the message Enter a
number: to be displayed, causes one call to cin to be made, and causes the message
Theƒnumberƒenteredƒis to be displayed. Figure 5.2 shows this flow of control.
Instead of simply displaying the entered numbers, Program 5.5 can be modified to use the
entered data. For example, you can add the numbers entered and display the total. To do this, you must
be careful about how you add the numbers because the same variable, num, is used for each number
entered. For this reason, the entry of a new number in Program 5.5 automatically causes the previous
number stored in num to be lost. Therefore, each number entered must be added to the total before
another number is entered. This is the required sequence:

Enter a number
Add the number to the total

How do you add a single number to a total? A statement such as total = total + num; does the
job perfectly. It’s the accumulation statement introduced in Section 3.1. After each number is entered,
the accumulating statement adds the number to the total,

Sentinels
In programming, data values used to signal the start or end of a data series are called sentinels.
Sentinel values must, of course, be selected so as not to conflict with legitimate data values. For
example, if you’re constructing a program to process a student’s grades, and assuming no extra credit is
given that could produce a grade higher than 100, you could use any grade higher than 100 as a sentinel
value.

Break and Continue Statements


Two useful statements in connection with repetition statements are the break and continue
statements. You encountered the break statement in Section 4.4 when learning about the switch
statement. This is the format of the break statement:
break;
A break statement, as its name implies, forces an immediate break, or exit, from the switch, while, for,
and do-while statements.

The continue statement is similar to the break statement but applies only to loops created with while,
do-while, and for statements. This is the general format of a continue statement:

continue;

When continue is encountered in a loop, the next iteration of the loop begins immediately. For while
loops, this means execution is transferred automatically to the top of the loop, and reevaluation of the
tested expression is initiated. Although the continue statement has no direct effect on a switch
statement, it can be included in a switch statement, which is also contained in a loop. The effect of
continue is the same: The next loop iteration begins.

The Null Statement

All statements must be terminated by a semicolon. A semicolon with nothing preceding it is also a valid
statement, called the null statement, as shown:

It’s a do-nothing statement used where a statement is required syntactically, but no action is called for.
Typically, null statements are used with while or for statements.

THE FOR STATEMENT


-Interactive For Loop
-Nested Loop
THE DO-WHILE STATEMENT
-Validity Checks
COMMON PROGRAMMING ERRORS

You might also like