While loop will be executed only the specified conditions atisfies. Do-While loop executes atleast once before checking for condition. So even a condition fails, the loop will be executed once.
While: If we can use while statement it will check the condition then proceed further loop statement.DoWhile: If we use dowhile, first execute loop statement then check the condition.
the test condition will be checked first after wards the body of the loop will be excuted in while statement and the the do while statement represented the body of the loop will be executed first and then the test condition will checked next
"do statement while (...);" is a loop which does at least one iteration even if the condition after while is false. When, for instance, "while(...) statement" does not iterate at all if the condition after while is false.
The do ..while loop is executed at least once, whereas the while loop may not be executed even once.
recursion is always slower than iteration
No, they are equivalient. DO something WHILE condition; does the same thing as DO something UNTIL NOT condition;
A do-while loop checks its termination condition before each iteration, including the first; a do-until checks after each iteration, so that the first iteration occurs before the first check. The C language uses the word "while" for both types of loop, using the placement of the condition to control its timing:C do-while:while (condition) { /* condition checked before first loop iteration */... loop contents}C do-until:do {... loop contents} while (condition); /* condition not checked until after first loop iteration */
A DO-WHILE loop will always execute at least one iteration of the loop body. This is because the condition that controls the loop comes at the end of the loop, rather than at the beginning as per a WHILE or FOR loop.
The difference between "do while" and "do until" is that a "do while" loops while the test case is true, whereas "do until" loops UNTIL the test case is true (which is equivalent to looping while the test case is false).The difference between a "do ...while" loop and a "while {} " loop is that the while loop tests its condition before execution of the contents of the loop begins; the "do" loop tests its condition after it's been executed at least once. As noted above, if the test condition is false as the while loop is entered the block of code is never executed. Since the condition is tested at the bottom of a do loop, its block of code is always executed at least once.To further clear your concept on this, understand the syntax and description of the two loop types:whileThe while loop is used to execute a block of code as long as some condition is true. If the condition is false from the start the block of code is not executed at al. The while loop tests the condition before it's executed so sometimes the loop may never be executed if initially the condition is not met. Its syntax is as follows.while (tested condition is satisfied){block of code}In all constructs, curly braces should only be used if the construct is to execute more than one line of code. The above program executes only one line of code so it not really necessary (same rules apply to if...else constructs) but you can use it to make the program seem more understandable or readable.Here is a simple example of the use of the while loop. This program counts from 1 to 100.#include int main(void){int count = 1;while (count
Well, hehe its quite simple. You must be a dumb little nerd if you cant find it out. I suggest taking Pre IB computers and eating cherrios for breakfast to clear your mind. Maybe after a hot shower with your rabbit youll be able to brainstorm through the ultimate collage of confusion!
SELECTION CONTROL STRUCTURES AND THEIR USEA selection control structure is used to make a choice between two or more actions, depending on whether a condition is true or false. This condition is expressed with one of the relational operators like less than, greater than, equal to (,=,,)etc. Adding selection structures makes the program a lot more structured and also gives the programmer a lot of flexibility. There are mainly four different kinds of selection control structures.1. Simple selection2. Simple selection with null false branch3. Combined selection4. Nested selectionLet us go through the control structures one by one relating them to real life examples.1>Simple selection (Simple If statement):Simple selection occurs when a choice is made between two alternate paths, depending on the result of a condition being true or false. The important keywords used are IF, THEN, ELSE and ENDIF.Let us take the example of a city where we want to count the sex ratio. For that we need to calculate the number of males and females. A pseudocode to solve the problem using a simple selection structure would be as followsIF Gender = "M"THENIncrease Male-Count by oneELSEIncrease Female-Count by oneENDIF2. Simple Selection with Null False Branch (null ELSE statement):These kind of selection structures are used when we a task is performed only when a particular condition is true. If the mentioned condition is false, then there is no execution or processing and the IF statement is skipped or bypassed. The ELSEclause is not used. Thus the selection is between executing or bypassing an action. The important keywords used are IF, THEN, ELSE and ENDIF.Let us take an example where we want to count the number of adults in a city. A pseudocode to solve the problem using a simple selection with null false branch would be as followsIF AGE > 18THENIncrease Adult Count by oneENDIF3. Combined Selection(combined IF statement):A combined IFstatement is one that contains multiple conditions, each connected with the logical operators AND or OR.If the connector AND is used to combine two conditions, then both conditions must be true for the combined condition to be true.First let us take an example of combined selection using AND. Say we want to count the number of females above the age of 18.The pseudocode would beIF Age > 18 AND Gender = "F"THENIncrease Female-Adult-Count by oneENDIFIf the connector ORis used to combine two conditions, then only one of the conditions needs to be true for the combined condition to be considered true.Say we want to determine the tickets that can be priced at half rate for children and senior people. The pseudocode would beIF Age < 12 OR Age > 65THENAdd 1 to Half-Price-CountENDIF4. Nested Selection (nested IF statement) A nested selection occurs when the word IF appears more than once within an IFstatement. A nested selection (IF) occurs when either the true of false branch of one If has another IF statement embedded within it.This kind of structure is used when there are multiple conditions to choose from. For example while calculating tax the tax rate varies according to the gross salary.A simple pseudocode to count number of male and female adults will beIF Age > 18IF Gender = "F"THENIncrease Female-Adult-Count by oneELSEIF Gender = "M"THENIncrease Male-Adult-Count by oneENDIFENDIFREPITITION CONTROL STRUCTURES AND THEIR USEThis type of control structures specify a block of one or more statements that are repeatedly executed until a condition is satisfied. These are also called iteration control structures. There are three different ways that a set of instructions can be repeated, and each way is determined by where the decision to repeat is placed:- at the beginning of the loop (leading decision loop)- at the end of the loop (trailing decision loop)- a counted number of times (counted loop)1. Leading Decision Loop:In such loops the testing of the condition is at the beginning of the loop. Examples are DOWHILE loops. The only way to terminate the loop is to render the DO WHILE loop false. Consider a program to sell tickets. The program should sell tickets while there are tickets available. In such types of loops Leading Decision loops can be used to sell tickets until tickets are 0 which can be used as the termination statement.2. Trailing Decision Loop:In such structures the testing of condition is done at end of loop except that the logic to keep on repeating is same as in case of other repetition structures. REPEAT…UNTIL is an example of trailing decision loop. In trailing loops the statements are executed at least once before the condition is tested. These loops are sued in almost all kinds of computer program for e.g to print student records, to continue taking input until a certain limit, etc.3. Counted Loop:These are also known as DO or FOR loops. In such kind of repetition structures the programmer knows in advance the exact number of loop iterations. The loop execution is controlled by an important variable called the loop index. Say a programmer wants to input the salary record for 20 individuals. He will give the counted loops a high priority because he knows hp many times he wants to repeats the loop.Finally we can conclude that selection control structures and repetition control structures find their use in almost any application. Using them wisely and judiciously depends on the person who is going to use these loops. Proper implementation of loop structures can reduce program complexity and cost a lot.