0

the loop

what is the different between IF, WHILE, FOR, DO and MULTIPLE CONDITION?

28th Apr 2017, 10:09 AM
agung pramudji
agung pramudji - avatar
3 Answers
+ 1
If statements are used if you want to provide multiple paths in a program but only want to go down one of them. Ill use one of my programs for an example. I have a shape generator and I ask the user to type in 1, 2, or 3 depending on the shape they want. I use an if statement to determine what function will be called to build the shape they picked, but I dont want all 3 functions to be called because they only picked one shape. While and do while loops are almost similar except that do while loops will execute one time no matter what, but in a while loop the conditions are checked first before entering the loop. Example for a while loop would be if you asked the user to enter in a positive number for a count down, but you dont want to count down unless they entered a positive number. So you use a while loop that says while Num > 0 if the number was negative the loop wouldn't be entered. Example of a do while loop would be when you want input from the user. Do while Num < 0. You keep on looping until the user puts in a positive number but you use the do while because you want to enter the loop at least one time. Opposed to the while loop where you only want to enter if the number is positive first. For loops is an all in one package kind of loop. You declare variables, conditions, and increments all in one area. For loops are really made to make loops neater. Matter of fact some coders call for loops for the sake of neatness. Note you dont even need to declare a variable, condition or an increment. You can simply have "for (;;)" which is valid. You can almost think of a for loop as a neater version of a while loop. Multiple conditions are very help full in reducing the number of ifs and loops that you have to create. Example going back to the count down program. You ask the user to enter in a positive number in the range of 0-99. In stead of creating two loops where one says while Num > 0 then another loop that says Num <= 99. To Be Continued...
28th Apr 2017, 2:35 PM
Ty Ler
Ty Ler - avatar
0
they all have their different uses. To give you just two examples most games use IF when a gun is fired whereas a game like pacman where the character keeps moving until a new direction is chosen would use a WHILE loop. It all depends on the context.
28th Apr 2017, 11:24 AM
Jason Hoffman
Jason Hoffman - avatar
0
Ran out of room. Part 2 You would combine them all into one loop and say Num > 0 && Num <=99. Also note when using &&, if the first condition is false the second part wont even be checked. Like wise in || if the first part is true the second part wont be checked. For instance if the user entered in -5 and your condition was Num > 0 && Num <=99. The program would look at the first part and see the condition is false without looking at the second part. If the condition was Num <=99 && Num > 0. The program would see the first part is true and check the second part to see that the second part is false. Another idea to note with conditions. In some cases when you have a loop inside of a loop you can possibly combine the loops to make an && statement. While also when you have a loop then another loop right after you can maybe combine them to make an || statement. Im not saying this is always possible but in some cases you can do this to reduce redundancy.
28th Apr 2017, 2:40 PM
Ty Ler
Ty Ler - avatar