Using Loop Control Variable
+19
Ferre
Rfrancis03
Vann Lewis
Jericho
Rhei
Pox
Asdfghjkl
iramae
judedominguiano
Louise Sebastiane Yagi
Lunsszxc
justjoking10
airojames
myramonique
Emie Laide
Sean
Luwiezzzz
James Navarro
daniella
23 posters
Page 1 of 3 • 1, 2, 3
Using Loop Control Variable
Wed Jul 25, 2018 7:52 am
To make a while loop end correctly, you can declare a loop control variable to manage the number of repetitions a loop performs.
Three separate actions should occur using a loop control variable:
-The loop control variable is initialized before entering the loop.
-The loop control variable’s value is tested, and if the result is true, the loop body is
entered.
-The loop control variable is altered within the body of the loop so that the tested
condition that follows while eventually is false.
Commonly, you can control a loop’s repetitions in one of two ways:
-Use a counter to create a definite, counter-controlled loop.
-Use a sentinel value to create an indefinite loop.
A counter is any numeric variable that counts the number of times an event has occurred.
The illustration below shows a loop that displays Hello four times. The loop is a counted loop, or counter-controlled loop , because theprogram keeps track of the number of loop repetitions by counting them.
A counted while loop that outputs Hello four times
1. The loop control variable, count , is initialized to 0.
2. The while expression compares count to 4.
3. The value of count is less than 4, and so the loop body executes. The loop body shown in Figure consists of two statements that display Hello and then add 1 to count .
4. The next time the condition count < 4 is evaluated, the value of count is 1, which is still less than 4, so the loop body executes again. Hello is displayed a second time and count is incremented to 2, Hello is displayed a third time and count becomes 3, then Hello is displayed a fourth time and count becomes 4.
Consider an interactive program that displays Hello repeatedly as long as the user wants to continue. The loop is indefinite because each time the program executes, the loop might be performed a different number of times.
An indefinite while loop that displays Hello as long as the user wants to continue
In the program in Figure , the loop control variable is shouldContinue . The program executes as follows:
1. The first input shouldContinue statement in the application in Figure is a priming input statement. In this statement, the loop control variable is initialized by the user’s first response.
2. The while expression compares the loop control variable to the sentinel value Y .
3. If the user has entered Y , then Hello is output and the user is asked whether the program should continue. In this step, the value of shouldContinue might change.
4. At any point, if the user enters any value other than Y , the loop ends. In most programming languages, simple comparisons are case sensitive, so any entry other than Y, including y, will end the loop.
Typical executions of the program in Figure 5-4 in two environments.
Three separate actions should occur using a loop control variable:
-The loop control variable is initialized before entering the loop.
-The loop control variable’s value is tested, and if the result is true, the loop body is
entered.
-The loop control variable is altered within the body of the loop so that the tested
condition that follows while eventually is false.
Commonly, you can control a loop’s repetitions in one of two ways:
-Use a counter to create a definite, counter-controlled loop.
-Use a sentinel value to create an indefinite loop.
Using a Definite Loop with a Counter
A counter is any numeric variable that counts the number of times an event has occurred.
The illustration below shows a loop that displays Hello four times. The loop is a counted loop, or counter-controlled loop , because theprogram keeps track of the number of loop repetitions by counting them.
A counted while loop that outputs Hello four times
1. The loop control variable, count , is initialized to 0.
2. The while expression compares count to 4.
3. The value of count is less than 4, and so the loop body executes. The loop body shown in Figure consists of two statements that display Hello and then add 1 to count .
4. The next time the condition count < 4 is evaluated, the value of count is 1, which is still less than 4, so the loop body executes again. Hello is displayed a second time and count is incremented to 2, Hello is displayed a third time and count becomes 3, then Hello is displayed a fourth time and count becomes 4.
Using an Indefinite Loop with a Sentinel Value
Consider an interactive program that displays Hello repeatedly as long as the user wants to continue. The loop is indefinite because each time the program executes, the loop might be performed a different number of times.
An indefinite while loop that displays Hello as long as the user wants to continue
In the program in Figure , the loop control variable is shouldContinue . The program executes as follows:
1. The first input shouldContinue statement in the application in Figure is a priming input statement. In this statement, the loop control variable is initialized by the user’s first response.
2. The while expression compares the loop control variable to the sentinel value Y .
3. If the user has entered Y , then Hello is output and the user is asked whether the program should continue. In this step, the value of shouldContinue might change.
4. At any point, if the user enters any value other than Y , the loop ends. In most programming languages, simple comparisons are case sensitive, so any entry other than Y, including y, will end the loop.
Typical executions of the program in Figure 5-4 in two environments.
- James NavarroGuru
- Posts : 12
Join date : 2018-07-31
Re: Using Loop Control Variable
Tue Jul 31, 2018 5:15 pm
Thank you mam sa paggawa ng forum na to Magegets ko na din lesson sa pld whahaha
Sent from Topic'it App
Sent from Topic'it App
- Luwiezzzz
- Posts : 2
Join date : 2018-07-31
Re: Using Loop Control Variable
Tue Jul 31, 2018 6:53 pm
Thank you for creating this, I finally know how to use the loop control properly
- SeanGuru
- Posts : 15
Join date : 2018-07-27
Re: Using Loop Control Variable
Tue Jul 31, 2018 10:01 pm
thank you po sa pagturo ma'am . mas naintindihan po namin lalo yung loop po . kung di po namin naintindihan to siguro po yung loop namin walang katapusan hehehe
Sent from Topic'it App
Sent from Topic'it App
- Emie LaideGuru
- Posts : 10
Join date : 2018-07-31
Re: Using Loop Control Variable
Tue Jul 31, 2018 10:47 pm
Pwede po bang sabay na gamitin ang counter at sentinel?
- SeanGuru
- Posts : 15
Join date : 2018-07-27
Re: Using Loop Control Variable
Tue Jul 31, 2018 11:01 pm
sa pagkakaintindi ko or opinyon ko lang hindi ata pwede na sabay gamitin kasi yung Counter loop is para maka create ng definite loop while yung isa using sentinel value is para maka create ng indefinite loop . Bale two ways siya on how to control a loop's repetitions .Emie Laide wrote:Pwede po bang sabay na gamitin ang counter at sentinel?
Sent from Topic'it App
- myramoniqueGuru
- Posts : 11
Join date : 2018-08-01
Re: Using Loop Control Variable
Wed Aug 01, 2018 10:35 am
Ang alam ko hindi din. Kasi kapag gumamit ka na ng counter, may definite loop ka na, alam na agad kung hanggang saan lang. Unlike sentinel, nakadepende pa kung eof na. Opinyon ko langEmie Laide wrote:Pwede po bang sabay na gamitin ang counter at sentinel?
Sent from Topic'it App
- myramoniqueGuru
- Posts : 11
Join date : 2018-08-01
Re: Using Loop Control Variable
Wed Aug 01, 2018 10:37 am
THANKYOU PO MA'AM! Gets na po namin yung loop, lalo na po yung while loop. Onting example nalang po talaga ng for loop
Sent from Topic'it App
Sent from Topic'it App
- airojamesGuru
- Posts : 11
Join date : 2018-07-31
Re: Using Loop Control Variable
Wed Aug 01, 2018 11:02 am
Yes may panibagong topic
Salamat po mam sa panibagong topic
Salamat po mam sa panibagong topic
- airojamesGuru
- Posts : 11
Join date : 2018-07-31
Re: Using Loop Control Variable
Wed Aug 01, 2018 11:04 am
Mam bakit po sa exam namin hindi na sya naginitialize,alter and tested?
- myramoniqueGuru
- Posts : 11
Join date : 2018-08-01
Re: Using Loop Control Variable
Wed Aug 01, 2018 11:09 am
Nangyari yun. Di naman magluloop kung di mangyayari yun e.airojames wrote:Mam bakit po sa exam namin hindi na sya naginitialize,alter and tested?
Sent from Topic'it App
- justjoking10Guru
- Posts : 25
Join date : 2018-08-01
Re: Using Loop Control Variable
Wed Aug 01, 2018 6:28 pm
Based on the explanation. Definite loops should be used if no user is present and Indefinite Loops if there will be a user.
- justjoking10Guru
- Posts : 25
Join date : 2018-08-01
Re: Using Loop Control Variable
Wed Aug 01, 2018 6:29 pm
Does anyone have a problem where a definite loop is used even if a user is present?
- LunsszxcGuru
- Posts : 10
Join date : 2018-08-01
Re: Using Loop Control Variable
Wed Aug 01, 2018 6:59 pm
Maam yung nested loop ba kelangan nasa module ? or pwede na wala sa module pero may loop sa isang loop ? askin' lang po
- myramoniqueGuru
- Posts : 11
Join date : 2018-08-01
Re: Using Loop Control Variable
Wed Aug 01, 2018 7:02 pm
justjoking10 wrote:Based on the explanation. Definite loops should be used if no user is present and Indefinite Loops if there will be a user.
Yah yah yah. Kasi kapag gumamit ka ng indefinit loop tapos walang user. Non sense din
Sent from Topic'it App
- janderGuest
srgaezhrtzhtrjhsjuhjyt
Wed Aug 01, 2018 7:04 pm
Ang dami ko pong natutunan ma'am, maraming salamat, ang galing nyo pong teacher, how to be you po
- myramoniqueGuru
- Posts : 11
Join date : 2018-08-01
Re: Using Loop Control Variable
Wed Aug 01, 2018 7:09 pm
@Lunsszxc Hindi ako si ma'am pero sa tingin ko pwedeng nasa module siya or pwede namang hindi. Basta ang nested loop is loop na may loop skl
Sent from Topic'it App
Sent from Topic'it App
- LunsszxcGuru
- Posts : 10
Join date : 2018-08-01
Re: Using Loop Control Variable
Wed Aug 01, 2018 7:13 pm
Ay surry akala ko po si maam daniella lang salamat sa po Naguguluhan po kase ako kasi sabi ang nested loop ay may loop sa isang loop pero sabi nung iba kelangan nasa module salamat po naliwanagan rin
- justjoking10Guru
- Posts : 25
Join date : 2018-08-01
Re: Using Loop Control Variable
Wed Aug 01, 2018 7:21 pm
Lunsszxc wrote:Ay surry akala ko po si maam daniella lang salamat sa po Naguguluhan po kase ako kasi sabi ang nested loop ay may loop sa isang loop pero sabi nung iba kelangan nasa module salamat po naliwanagan rin
@Lunsszxc. Pwede mo ilagay ang loop s loob ng module. Pwede den mag lagay ng module s loob ng module.
- justjoking10Guru
- Posts : 25
Join date : 2018-08-01
Re: Using Loop Control Variable
Wed Aug 01, 2018 7:26 pm
myramonique wrote:justjoking10 wrote:Based on the explanation. Definite loops should be used if no user is present and Indefinite Loops if there will be a user.
Yah yah yah. Kasi kapag gumamit ka ng indefinit loop tapos walang user. Non sense din
Sent from Topic'it App
@myramonique. What if ang purpose ng program mo ay to repeat something that the user had input.
- myramoniqueGuru
- Posts : 11
Join date : 2018-08-01
Re: Using Loop Control Variable
Wed Aug 01, 2018 8:29 pm
justjoking10 wrote:myramonique wrote:justjoking10 wrote:Based on the explanation. Definite loops should be used if no user is present and Indefinite Loops if there will be a user.
Yah yah yah. Kasi kapag gumamit ka ng indefinit loop tapos walang user. Non sense din
@myramonique. What if ang purpose ng program mo ay to repeat something that the user had input.
Kailangan gumamit ng sentinel value para matapos yung loop? hehehe tama ba?
Sent from Topic'it App
- justjoking10Guru
- Posts : 25
Join date : 2018-08-01
Re: Using Loop Control Variable
Wed Aug 01, 2018 8:41 pm
myramonique wrote:justjoking10 wrote:myramonique wrote:justjoking10 wrote:Based on the explanation. Definite loops should be used if no user is present and Indefinite Loops if there will be a user.
Yah yah yah. Kasi kapag gumamit ka ng indefinit loop tapos walang user. Non sense din
@myramonique. What if ang purpose ng program mo ay to repeat something that the user had input.
Kailangan gumamit ng sentinel value para matapos yung loop? hehehe tama ba?
Sent from Topic'it App
And clarify ko ahh. Based s explanation DEFINITE LOOP = NO USER. INDEFINITE LOOP = WITH USER. D ako sure ehh. Wala kc akong maisip n problem to pseudo code. Pero meron atang ganung program. Antay nlng tayo ng response ni maam.
Re: Using Loop Control Variable
Thu Aug 02, 2018 9:02 am
Sean wrote:thank you po sa pagturo ma'am . mas naintindihan po namin lalo yung loop po . kung di po namin naintindihan to siguro po yung loop namin walang katapusan hehehe
Sent from Topic'it App
HAHAHA! Tapusin na natin ... sasakit lang ulo natin kung paulit ulit nalang
Re: Using Loop Control Variable
Thu Aug 02, 2018 9:05 am
myramonique wrote:Nangyari yun. Di naman magluloop kung di mangyayari yun e.airojames wrote:Mam bakit po sa exam namin hindi na sya naginitialize,alter and tested?
Sent from Topic'it App
Right! Kailangan mo ng mag cocontrol sa loop mo , kung hindi garbage lang program mo.
Re: Using Loop Control Variable
Thu Aug 02, 2018 9:09 am
justjoking10 wrote:myramonique wrote:justjoking10 wrote:myramonique wrote:justjoking10 wrote:Based on the explanation. Definite loops should be used if no user is present and Indefinite Loops if there will be a user.
Yah yah yah. Kasi kapag gumamit ka ng indefinit loop tapos walang user. Non sense din
@myramonique. What if ang purpose ng program mo ay to repeat something that the user had input.
Kailangan gumamit ng sentinel value para matapos yung loop? hehehe tama ba?
Sent from Topic'it App
And clarify ko ahh. Based s explanation DEFINITE LOOP = NO USER. INDEFINITE LOOP = WITH USER. D ako sure ehh. Wala kc akong maisip n problem to pseudo code. Pero meron atang ganung program. Antay nlng tayo ng response ni maam.
Basta may kinalaman kay USER , sentinel agad, indefinite kasi nag iintay ka ng response, unlike sa counter DEFINITE, FIXED, Programmer ang gagawa, example nun clock, wala naman tayong binabago, except kungmay lalagay ka ng alaram clock at timer , papasok doon yung database at sentinel
Page 1 of 3 • 1, 2, 3
Permissions in this forum:
You cannot reply to topics in this forum
|
|