Share
Go down
avatar
Admin
Posts : 42
Join date : 2018-07-24
Age : 20
Location : Dasmarinas City of Cavite
View user profilehttp://daniella.forumotion.asia

Using Loop Control Variable

on 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.


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.
avatar
Guru
Posts : 12
Join date : 2018-07-31
View user profile

Re: Using Loop Control Variable

on Tue Jul 31, 2018 5:15 pm
Thank you mam sa paggawa ng forum na to Smile Magegets ko na din lesson sa pld whahaha Smile

Sent from Topic'it App
Posts : 2
Join date : 2018-07-31
View user profile

Re: Using Loop Control Variable

on Tue Jul 31, 2018 6:53 pm
Thank you for creating this, I finally know how to use the loop control properly
Guru
Posts : 15
Join date : 2018-07-27
View user profile

Re: Using Loop Control Variable

on 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
Guru
Posts : 10
Join date : 2018-07-31
View user profile

Re: Using Loop Control Variable

on Tue Jul 31, 2018 10:47 pm
Pwede po bang sabay na gamitin ang counter at sentinel?
Guru
Posts : 15
Join date : 2018-07-27
View user profile

Re: Using Loop Control Variable

on Tue Jul 31, 2018 11:01 pm
Emie Laide wrote:Pwede po bang sabay na gamitin ang counter at sentinel?
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 .

Sent from Topic'it App
Guru
Posts : 11
Join date : 2018-08-01
View user profile

Re: Using Loop Control Variable

on Wed Aug 01, 2018 10:35 am
Emie Laide wrote:Pwede po bang sabay na gamitin ang counter at sentinel?
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 lang 😅

Sent from Topic'it App
Guru
Posts : 11
Join date : 2018-08-01
View user profile

Re: Using Loop Control Variable

on 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
Guru
Posts : 11
Join date : 2018-07-31
View user profile

Re: Using Loop Control Variable

on Wed Aug 01, 2018 11:02 am
Yes may panibagong topic

Salamat po mam sa panibagong topic
Guru
Posts : 11
Join date : 2018-07-31
View user profile

Re: Using Loop Control Variable

on Wed Aug 01, 2018 11:04 am
Mam bakit po sa exam namin hindi na sya naginitialize,alter and tested?
Guru
Posts : 11
Join date : 2018-08-01
View user profile

Re: Using Loop Control Variable

on Wed Aug 01, 2018 11:09 am
airojames wrote:Mam bakit po sa exam namin hindi na sya naginitialize,alter and tested?
Nangyari yun. Di naman magluloop kung di mangyayari yun e.

Sent from Topic'it App
Guru
Posts : 25
Join date : 2018-08-01
View user profile

Re: Using Loop Control Variable

on 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.
Guru
Posts : 25
Join date : 2018-08-01
View user profile

Re: Using Loop Control Variable

on Wed Aug 01, 2018 6:29 pm
Does anyone have a problem where a definite loop is used even if a user is present?
Guru
Posts : 10
Join date : 2018-08-01
View user profile

Re: Using Loop Control Variable

on Wed Aug 01, 2018 6:59 pm
Maam yung nested loop ba kelangan nasa module ? Very Happy or pwede na wala sa module pero may loop sa isang loop ? askin' lang po
Guru
Posts : 11
Join date : 2018-08-01
View user profile

Re: Using Loop Control Variable

on 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
jander
Guest

srgaezhrtzhtrjhsjuhjyt

on 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
Guru
Posts : 11
Join date : 2018-08-01
View user profile

Re: Using Loop Control Variable

on 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
Guru
Posts : 10
Join date : 2018-08-01
View user profile

Re: Using Loop Control Variable

on Wed Aug 01, 2018 7:13 pm
Ay surry akala ko po si maam daniella lang Very Happy salamat sa po Very Happy Naguguluhan po kase ako kasi sabi ang nested loop ay may loop sa isang loop pero sabi nung iba kelangan nasa module Very Happy salamat po naliwanagan rin Very Happy
Guru
Posts : 25
Join date : 2018-08-01
View user profile

Re: Using Loop Control Variable

on Wed Aug 01, 2018 7:21 pm
Lunsszxc wrote:Ay surry akala ko po si maam daniella lang Very Happy salamat sa po Very Happy Naguguluhan po kase ako kasi sabi ang nested loop ay may loop sa isang loop pero sabi nung iba kelangan nasa module Very Happy salamat po naliwanagan rin Very Happy

@Lunsszxc. Pwede mo ilagay ang loop s loob ng module. Pwede den mag lagay ng module s loob ng module.
Guru
Posts : 25
Join date : 2018-08-01
View user profile

Re: Using Loop Control Variable

on 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.
Guru
Posts : 11
Join date : 2018-08-01
View user profile

Re: Using Loop Control Variable

on 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
Guru
Posts : 25
Join date : 2018-08-01
View user profile

Re: Using Loop Control Variable

on 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.
avatar
Admin
Posts : 42
Join date : 2018-07-24
Age : 20
Location : Dasmarinas City of Cavite
View user profilehttp://daniella.forumotion.asia

Re: Using Loop Control Variable

on 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 Sad
avatar
Admin
Posts : 42
Join date : 2018-07-24
Age : 20
Location : Dasmarinas City of Cavite
View user profilehttp://daniella.forumotion.asia

Re: Using Loop Control Variable

on Thu Aug 02, 2018 9:05 am
myramonique wrote:
airojames wrote:Mam bakit po sa exam namin hindi na sya naginitialize,alter and tested?
Nangyari yun. Di naman magluloop kung di mangyayari yun e.

Sent from Topic'it App

Right! Kailangan mo ng mag cocontrol sa loop mo , kung hindi garbage lang program mo.
avatar
Admin
Posts : 42
Join date : 2018-07-24
Age : 20
Location : Dasmarinas City of Cavite
View user profilehttp://daniella.forumotion.asia

Re: Using Loop Control Variable

on 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 Smile
Sponsored content

Re: Using Loop Control Variable

Back to top
Permissions in this forum:
You cannot reply to topics in this forum