Wednesday , 25 November 2020
Breaking News
while loop

While Loop in C/C++ with example


Notice: Undefined index: tie_hide_meta in /home1/adnanonl/public_html/wp-content/themes/sahifa/framework/parts/meta-post.php on line 3

Notice: Trying to access array offset on value of type null in /home1/adnanonl/public_html/wp-content/themes/sahifa/framework/parts/meta-post.php on line 3
Spread the love

While Loop in C/C++

while loopIn C language, to write a while statement (repetitive while) the syntax is used:
while (<logical_expression>)
{
<block_of_instructions>
}
When the <instruction_block> contains only one instruction, the characters open key ({) and close key (}) are optional.

On the other hand, as in the double and simple alternative instructions, the <logical_expression> of a while repetitive statement is also called a condition.

For the <instruction_block> to be executed, the condition must be true. Conversely, if the condition is false, the <instruction_block> is not executed.

Therefore, when the flow of a program reaches a while loop, there are two possibilities:
If the condition evaluates to false, the instruction block is not executed, and the while loop ends without performing any iteration.
If the condition evaluates to true, the instruction block itself is executed and then the condition is re-evaluated, to decide, again, whether the instruction block is executed again or not. And so on, until, the condition is false.
The <instruction_block> of a while loop can be executed zero or more times (iterations). If the <instruction_block> is executed at least once, it will continue to run repeatedly, as long as the condition is true. But, you have to be careful that the loop is not infinite.

When the condition of a while loop is always evaluated to true, it is said that an infinite loop has occurred, since the program never ends. An infinite loop is a logical error.

It is important to emphasize the fact that, in a while loop, the condition is first evaluated and, if it is true, then the instruction block is executed. We will see that, in the do-loop, the procedure is the other way around. In it, the instruction block is executed first and then the condition is evaluated.

For a while loop to be non-infinite, something must happen in the instruction block before the condition ceases to be true. In most cases, the condition becomes false when changing the value of a variable.

In short, a repetitive while statement allows you to execute, repeatedly (zero or more times) a block of instructions, while a certain condition is true.

Example 1: You want to write a program that displays the first ten natural numbers on the screen:

1 2 3 4 5 6 7 8 9 10
In C language, to solve the problem of this example you can write:
#include <stdio.h>

int main ()
{
int counter;

printf (“\ n”);

counter = 1; / * Counter initialization * /
while (counter <= 10) / * Condition * /
{
printf (“% d”, counter); /* Departure */
counter ++; / * Counter increment * /
}

   return 0;
}
Example 2: You want to write a program that:

1º) Ask for keyboard the note (real data) of a subject.
2nd) In the event that the note is incorrect, show the message on the screen:
“ERROR: Incorrect note, it must be> = 0 and <= 10”.
3rd) Repeat steps 1 and 2, while, the note entered is incorrect.
4th) Show on screen:
“APPROVED”, in the event that the grade is greater than or equal to 5.
“SUSPENDED”, in the event that the grade is less than 5.
Onscreen:

Enter note (real): 12.4

ERROR: Incorrect note, it must be> = 0 and <= 10

Enter note (real): -3.3

ERROR: Incorrect note, it must be> = 0 and <= 10

Enter note (actual): 8.7

APPROVED
The proposed code is:
#include <stdio.h>

int main ()
{
float note;

printf (“\ n Enter note (real):”);
scanf (“% f”, & note);

/ * If the first note entered by the
user is correct,
the loop does not iterate any time. * /

while (note <0 || note> 10)
{
printf (“\ n ERROR: Incorrect note, it must be> = 0 and <= 10 \ n”);
printf (“\ n Enter note (real):”);
scanf (“% f”, & note);
}

/ * While the user enters a
wrong note, the loop will iterate.
And when you enter a correct note,
the loop will end. * /

if (note> = 5)
printf (“\ n APPROVED”);
else
printf (“\ n SUSPENDED”);

return 0;
}
In the program, the while loop has been used to validate the note entered by the user. In programming, it is very common to use the while loop to validate (filter) data. The loop used to validate one or more data is also known as a filter.

Leave a Reply

Your email address will not be published. Required fields are marked *