Symbianize Forum

Most of our features and services are available only to members, so we encourage you to login or register a new account. Registration is free, fast and simple. You only need to provide a valid email. Being a member you'll gain access to all member forums and features, post a message to ask question or provide answer, and share or find resources related to mobile phones, tablets, computers, game consoles, and multimedia.

All that and more, so what are you waiting for, click the register button and join us now! Ito ang website na ginawa ng pinoy para sa pinoy!

Q. Paano gumagana yung looping statements? (Respect)

anjhelo07

Novice
Advanced Member
Messages
44
Reaction score
0
Points
26
Halos ilang araw na ako nagreresearch tungkol sa looping statements sa C# pero hindi ko pa rin maintindihan yung pattern.
Pwede po ba paki elaborate ng pa-isa-isa. kung ano pinagkaiba ng For, While, Do While loops salamat po sa sasagot
 
haha. . sa java ko sila alam. . .anyway, pareho lang naman ng behavior yan, structures lang ang nagkaiba.

for(int start; end; incre/decre)
{
statement
}

do //execute first then check the conditon
{
statement
}while(condition)

while(conditon) //check condition first before execution
{
statement
}
 
Halos ilang araw na ako nagreresearch tungkol sa looping statements sa C# pero hindi ko pa rin maintindihan yung pattern.
Pwede po ba paki elaborate ng pa-isa-isa. kung ano pinagkaiba ng For, While, Do While loops salamat po sa sasagot

Hindi ba't sa pattern sila magkakaiba? bagamat sila'y mga halimbawa ng Looping statements?

Bigyan kita ng halimbawa. Paano mo maipapalabas eto ng sampung beses?.... "ILANG ARAW NA AKO NARERESEARCH TUNGKOL SA LOOPING STATEMENTS SA C-SHARP, HINDI KO MAINTINDIHAN!"?

Code:
Console.WriteLine("ILANG ARAW NA AKO NARERESEARCH TUNGKOL SA LOOPING STATEMENTS SA C-SHARP, HINDI KO MAINTINDIHAN!"); //1
Console.WriteLine("ILANG ARAW NA AKO NARERESEARCH TUNGKOL SA LOOPING STATEMENTS SA C-SHARP, HINDI KO MAINTINDIHAN!"); //2
Console.WriteLine("ILANG ARAW NA AKO NARERESEARCH TUNGKOL SA LOOPING STATEMENTS SA C-SHARP, HINDI KO MAINTINDIHAN!"); //3
Console.WriteLine("ILANG ARAW NA AKO NARERESEARCH TUNGKOL SA LOOPING STATEMENTS SA C-SHARP, HINDI KO MAINTINDIHAN!"); //4
Console.WriteLine("ILANG ARAW NA AKO NARERESEARCH TUNGKOL SA LOOPING STATEMENTS SA C-SHARP, HINDI KO MAINTINDIHAN!"); //5
Console.WriteLine("ILANG ARAW NA AKO NARERESEARCH TUNGKOL SA LOOPING STATEMENTS SA C-SHARP, HINDI KO MAINTINDIHAN!"); //6
Console.WriteLine("ILANG ARAW NA AKO NARERESEARCH TUNGKOL SA LOOPING STATEMENTS SA C-SHARP, HINDI KO MAINTINDIHAN!"); //7
Console.WriteLine("ILANG ARAW NA AKO NARERESEARCH TUNGKOL SA LOOPING STATEMENTS SA C-SHARP, HINDI KO MAINTINDIHAN!"); //8
Console.WriteLine("ILANG ARAW NA AKO NARERESEARCH TUNGKOL SA LOOPING STATEMENTS SA C-SHARP, HINDI KO MAINTINDIHAN!"); //9
Console.WriteLine("ILANG ARAW NA AKO NARERESEARCH TUNGKOL SA LOOPING STATEMENTS SA C-SHARP, HINDI KO MAINTINDIHAN!"); //10
eh pano, kung 1,000 times at hiindi ka magka-copy-paste?

dito papasok ang looping statements.

e.g. using WHILE.
Code:
            int c;
            c=1;
            while(c<=10)
            {
                Console.WriteLine("ILANG ARAW NA AKO NARERESEARCH TUNGKOL SA LOOPING STATEMENTS SA C-SHARP, HINDI KO MAINTINDIHAN!"); 
                c=c+1;
            }

with comments...
Code:
`          c=1; //dito magsisimula ang bilang. at ito'y bilang isa (1)
            while(c<=10) //habang ang c ay naglalalaman ng bilang na mababa o parehas sa sampo (10). Paglumampas (tulad ng 11), ito'y hindi na ipoproseso dahil wala na sa kondisyon ng WHILE.
            {
                Console.WriteLine("blah-blah-blah!"); 
                c=c+1; //magdadagdag ng isa (1) sa laman ng c. kaya ang paunang laman ng c na isa (1) ay madadagdagan hangga't marating ang bilang sampo (10), kaya kung iisipin mo, parang magbibilang ka lang ng isa, dalawa, tatlo, apat, lima, anim, pito, walo, siyam at sampo.
            }

Nawa'y maunawaan mo na ang konsepto ng looping statement.
 
Last edited:
Commonly ginagamit natin ang looping for counting or increment/decrement na tinatawag natin.
 
Halos ilang araw na ako nagreresearch tungkol sa looping statements sa C# pero hindi ko pa rin maintindihan yung pattern.
Pwede po ba paki elaborate ng pa-isa-isa. kung ano pinagkaiba ng For, While, Do While loops salamat po sa sasagot
I am not just talking about C# in particular but in the Programming as a whole...

Sa For Loop statements:
1) It allows you to initialize a value before looping.
2) The condition is tested at the beginning of the execution.
3) The instruction inside the Bracket will continue to execute and repeat as long as the condition is true....
4) The value is updated first before evaluating the condition if it is true or false. (example for x=0, x<10, x++... it will increment first the value of x and will become x=1 then check if it still pass the condition then execute, else exit)

Example:

for (x=0, x<=10, x++) {

.... LINE of codes to execute

}

x=0 =====> Initial value ng x
x<=10 =====> Condition to be met .; X should be less than or equal to 10
x++ =====> Increment the value of x by 1 (also same as x = x+1)

Sa for loop statement, ganito sequence ng pag execute nya....

(a) increment/decrement the value (depende sa nilagay mo na update value) of x by the value you specify (in these case it is 1)
(b) evaluate the value if it met the condition (if x <=10)
(c) if condition is met, execute the "line of codes", if not, exit.

So for example ang iniitial value kaagad ng x ay more than 10, ndi nya na execute ung "line of codes"... exit na kaagad.

Sa While Loop statements:
1) No initial value required..Scratch this... wrong use of word....
1) It does not SET initial value on its own.
2) The condition is tested at the beginning of the execution.
3) The instruction inside the Bracket will continue to execute and repeat as long as the condition is true....
4) no update done unless you specify in your code inside the bracket

Example:

while (x<=10) {

.... LINE of codes to execute

}

x<=10 =====> Condition to be met .; X should be less than or equal to 10

Sa while loop statement, ganito sequence ng pag execute nya....

(a) evaluate the value if it met the condition (if x <=10)
(b) if condition is met, execute the "line of codes", if not, exit.

Kung napansin mo, wlang initial value ang x na isi-net ang while loop so need mo muna bigyan ng initial value ang x outside the while loop code.... also wala rin siyang update value (x++ in the for loop command) so need mo rin lagyan ng code inside the "line of code para sa update command... And also, same sa for loop, for example ang iniitial value kaagad ng x ay more than 10, ndi nya na execute ung "line of codes"... exit na kaagad.


Sa DO While statements:

Same lang xa sa While statements... ang pagkakaiba lng... the code is executed first before testing... so you are guaranteed a 1 execution even the condition is false....
 
Last edited:
Sa While Loop statements:
1) No initial value required...
.... bracket...

Please clarify your answer..
* AFAIK, a looping statement requires the ff.:
1) initialization
2) condition
3) process (incrementation/decrementation)
In C# as well as C, C++, Java and other C related languages, requires a variable to be declared, and in the case of looping statement, it needs an initial value.

* bracket term/symbol in programming
 
Please clarify your answer..
* AFAIK, a looping statement requires the ff.:
1) initialization
2) condition
3) process (incrementation/decrementation)
In C# as well as C, C++, Java and other C related languages, requires a variable to be declared, and in the case of looping statement, it needs an initial value.

* bracket term/symbol in programming

Yes you are correct po... Initial value is required to evaluate the value of x in this case.... What I am saying is that in while loop, ndi po xa automatic na naiseset ng while loop statement mo not unlike in "FOR LOOP"... so if you will use while loop, you need to initialize the value of x first... before creating the while loop... paki backread nalang po... isinama ko yung explanation.... :)

Bracket meaning Begin and End ng statement inside the loop statement....
 
Last edited:
Yes you are correct po... Initial value is required to evaluate the value of x in this case.... What I am saying is that in while loop, ndi po xa automatic na naiseset ng while loop statement mo not unlike in "FOR LOOP"... so if you will use while loop, you need to initialize the value of x first... before creating the while loop... paki backread nalang po... isinama ko yung explanation.... :)

Bracket meaning Begin and End ng statement inside the loop statement....

How will you explain this?

Code:
int c=1;
for(;;){
 Console.WriteLine(c);
 c++;
 if(c>10){break;}
}

or this one...
Code:
            int c=1;
            for(;c<=10;c++){
                Console.WriteLine(c);
            }

Try to run these separately (C#)...

* with initialization
Code:
            int c=1;
            while(c<=10){
                Console.WriteLine(c);
                c++;
            }

* without initialization
Code:
            int c;
            while(c<=10){
                Console.WriteLine(c);
                c++;
            }


In programming, can you name these symbols?

[]

{}

()

<>
 
Last edited:
How will you explain this?

Code:
int c=1;
for(;;){
 Console.WriteLine(c);
 c++;
 if(c>10){break;}
}

This is suppose to be an infinite LOOP :)

first you initialize the value of c outside of the loop statement... then inside the loop statement you did not initialize it (no need to because c already has value), then no condition and no update value (in the if statement)... some of the codes inside this loop should not have been necessary if you did code it as how it is supposed to....

Condition never becomes false pero since you put a break point... it will break out of loop after c=10... but I think this is not the right way to do this :)

or this one...
Code:
            int c=1;
            for(;c<=10;c++){
                Console.WriteLine(c);
            }

For this one you need not provide an initial value because you just pass what was the value of c that you initialize outside the for Loop command... again not the way to do it... depending on the situation it might be use....

Later ko na sagutin iba....

----------------- UPDATE ---------------------

Let me move one-step backward first...

Ang tanong po ni TS ay kung ano ang pagkakaiba at kung papaano ginagamit ang FOR, WHILE, DO WHILE statements sa Csharp....

That is why I explained the differences and how it is supposed to be use.... Hindi ko po sinasabi na if you do this it will not work or na ito lang ang gamit ng bawat statements na ito... now going back to your challenge:

How will you explain this?

Code:
int c=1;
for(;;){
 Console.WriteLine(c);
 c++;
 if(c>10){break;}
}
This will run. No doubt about it. But the question is, is this the right way to write this code????

I think I need to point out that the reason many would want to use the FOR LOOP statement is because you can effectively eliminate 3 lines of code and convert it to just a 1 liner "statement"....

the above code could have been written:
Code:
for(c=1;c<=10; c++){Console.WriteLine(c); }

You 4 lines of code is reduced to just a 1 liner :)

So what I am saying is that knowing when to use FOR LOOP, WHILE LOOP, and DO WHILE LOOP.... will save you some time, bytes, and MEM in the long run. On the above mentioned code, you could have just use a while loop instead if that was really your intention....

Code:
int c=1;
while(c<=10){
 Console.WriteLine(c);
 c++;
}

And still this is 1 line less from your for loop code :)

In programming, can you name these symbols?

[]

{}

()

<>

I am not gonna honor this question with an answer... What is this???? Are you testing my ability to code???? I don't need to prove anything to you... I am just lending a hand to TS because I think his question is genuine and it warrants an answer...

I am not looking for a contest here.... If you are a good programmer then good for you. Share those talent to the world :)
 
Last edited:
This is suppose to be an infinite LOOP :)

first you initialize the value of c outside of the loop statement... then inside the loop statement you did not initialize it (no need to because c already has value), then no condition and no update value (in the if statement)... some of the codes inside this loop should not have been necessary if you did code it as how it is supposed to....

Condition never becomes false pero since you put a break point... it will break out of loop after c=10... but I think this is not the right way to do this :)



For this one you need not provide an initial value because you just pass what was the value of c that you initialize outside the for Loop command... again not the way to do it... depending on the situation it might be use....

Later ko na sagutin iba....

----------------- UPDATE ---------------------

Let me move one-step backward first...

Ang tanong po ni TS ay kung ano ang pagkakaiba at kung papaano ginagamit ang FOR, WHILE, DO WHILE statements sa Csharp....

That is why I explained the differences and how it is supposed to be use.... Hindi ko po sinasabi na if you do this it will not work or na ito lang ang gamit ng bawat statements na ito... now going back to your challenge:


This will run. No doubt about it. But the question is, is this the right way to write this code????

I think I need to point out that the reason many would want to use the FOR LOOP statement is because you can effectively eliminate 3 lines of code and convert it to just a 1 liner "statement"....

the above code could have been written:
Code:
for(c=1;c<=10; c++){Console.WriteLine(c); }

You 4 lines of code is reduced to just a 1 liner :)

So what I am saying is that knowing when to use FOR LOOP, WHILE LOOP, and DO WHILE LOOP.... will save you some time, bytes, and MEM in the long run. On the above mentioned code, you could have just use a while loop instead if that was really your intention....

Code:
int c=1;
while(c<=10){
 Console.WriteLine(c);
 c++;
}

And still this is 1 line less from your for loop code :)



I am not gonna honor this question with an answer... What is this???? Are you testing my ability to code???? I don't need to prove anything to you... I am just lending a hand to TS because I think his question is genuine and it warrants an answer...

I am not looking for a contest here.... If you are a good programmer then good for you. Share those talent to the world :)

You missed one explanation. Did you try to run my last code (WHILE without initialization)?

Just to clarify... I'm not after for the the use of any looping statements. It's a matter of preference.

The following codes are not a good/common practice. You know that.

Code:
int c=1;
for(;;){
 Console.WriteLine(c);
 c++;
 if(c>10){break;}
}

or this one...
Code:
            int c=1;
            for(;c<=10;c++){
                Console.WriteLine(c);
            }

My point is, a looping statement (correct me if I'm wrong) needs an initial value (initialization) esp. C related languages like C#. Else, it will cause an error. Unless, you are using a BASIC related languages like VB, if OPTION EXPLICIT is not used. This is the reason why I commented on your post. Second thing, I know you know how to interpret codes. I hope you did the same with this one.. BRACKET. I know you are younger than I am. The reason why I asked the symbols, I'm not sure how you learned the names of the symbols and being applied in the world of programming (from English to Programming/Computer Science). In my years of education, I was introduced with these names... [] - brackets, {} braces , () - parentheses & <> - less/greater than/angle brackets(HTML), though these are all brackets in British English. Our system of education is based on US. So when you mentioned bracket, it was not clear which one you're pertaining to. That's why I said, "Please clarify..."

Now tell me, am I testing your ability to code? am I contesting you?
 
Natutuwa ako sa mga comments e :lol: ts labas ka na dyan at sabihin mo na kung hindi mo pa rin gets bago mabahiran ng dugo tong thread mo jk.
 
You missed one explanation. Did you try to run my last code (WHILE without initialization)?

Just to clarify... I'm not after for the the use of any looping statements. It's a matter of preference.

The following codes are not a good/common practice. You know that.

Code:
int c=1;
for(;;){
 Console.WriteLine(c);
 c++;
 if(c>10){break;}
}

or this one...
Code:
            int c=1;
            for(;c<=10;c++){
                Console.WriteLine(c);
            }

My point is, a looping statement (correct me if I'm wrong) needs an initial value (initialization) esp. C related languages like C#. Else, it will cause an error. Unless, you are using a BASIC related languages like VB, if OPTION EXPLICIT is not used. This is the reason why I commented on your post. Second thing, I know you know how to interpret codes. I hope you did the same with this one.. BRACKET. I know you are younger than I am. The reason why I asked the symbols, I'm not sure how you learned the names of the symbols and being applied in the world of programming (from English to Programming/Computer Science). In my years of education, I was introduced with these names... [] - brackets, {} braces , () - parentheses & <> - less/greater than/angle brackets(HTML), though these are all brackets in British English. Our system of education is based on US. So when you mentioned bracket, it was not clear which one you're pertaining to. That's why I said, "Please clarify..."

Now tell me, am I testing your ability to code? am I contesting you?

I guess you are the one who missed my explanation.... please backread ka sa post 6... Sabi ko the variable that you are going to be evaluated needs to have an initial value... Sa for loop statement, you can specify that value inside the loop statement itself, samantalang sa while and do while loop... you need to initialize the value first before running the loop or you will get a run-time error....

About brackets.... I said I am not specific to only CSHARP as programming language kundi sa programming as a whole... different languages uses different BEGIN and END statement... some uses braces, other brackets, others the word begin and end... it does not matter... I am not language specific... no need to fuzz about it... I remember during my PASCAL days that we use the word begin and end....

Regarding age, if your are around 50 then I would say you are older than me, else, I don't think so.... I started my programming days when we are still using PASCAL and CLipper and FOXPRO and BASIC and Assembly language and C.... All accessible using a DOS command and the OS is either DOS or if your PC is high tech, running under Windows 3.1... and FLOPPY Disk is our Hard DRive :)

- - - Updated - - -

Natutuwa ako sa mga comments e :lol: ts labas ka na dyan at sabihin mo na kung hindi mo pa rin gets bago mabahiran ng dugo tong thread mo jk.

Hahaha... I think I am gonna shut up after this.... tumutulong k lng napasama pa...


Anyways.... sana nakatulong explanation ko TS... Happy Coding.
 
Last edited:
Halos ilang araw na ako nagreresearch tungkol sa looping statements sa C# pero hindi ko pa rin maintindihan yung pattern.
Pwede po ba paki elaborate ng pa-isa-isa. kung ano pinagkaiba ng For, While, Do While loops salamat po sa sasagot


Since TS said the above statement, my main concern is this one....

Sa While Loop statements:
1) No initial value required..Scratch this... wrong use of word....
1) It does not SET initial value on its own.
2) The condition is tested at the beginning of the execution.
3) The instruction inside the Bracket will continue to execute and repeat as long as the condition is true....
4) no update done unless you specify in your code inside the bracket

Example:

while (x<=10) {

.... LINE of codes to execute

}

x<=10 =====> Condition to be met .; X should be less than or equal to 10

Sa while loop statement, ganito sequence ng pag execute nya....

(a) evaluate the value if it met the condition (if x <=10)
(b) if condition is met, execute the "line of codes", if not, exit.

Kung napansin mo, wlang initial value ang x na isi-net ang while loop so need mo muna bigyan ng initial value ang x outside the while loop code.... also wala rin siyang update value (x++ in the for loop command) so need mo rin lagyan ng code inside the "line of code para sa update command... And also, same sa for loop, for example ang iniitial value kaagad ng x ay more than 10, ndi nya na execute ung "line of codes"... exit na kaagad.
I think you missed this in Post #10....

* without initialization
Code:
            int c;
            while(c<=10){
                Console.WriteLine(c);
                c++;
            }

...
Sa While Loop statements:
...
...4) no update done unless you specify in your code inside the bracket...

TS is confused on patterns... If he thought bracket is like this ... [ & ] unless he is aware of British English regarding these symbols.

I'm not against your answers. If you will notice, I'm the one who is bringing out your talent to share to the world via questioning. And to clarify, I'm not challenging you and I'm not testing your ability to code. If I'm going to challenge you, why would I challenge you with a simple looping statements (And we're not talking about yet nested loops here), It should be a PROJECT. I'm just putting myself in TS' shoe. I hope our 2 colleagues here will understand also instead of provoking. This will not help the thread starter. And I'm thanking you for your answers. I think, our conversations will help him understand looping statements in C#.

- - - Updated - - -

...Regarding age, if your are around 50 then I would say you are older than me, else, I don't think so.... I started my programming days when we are still using PASCAL and CLipper and FOXPRO and BASIC and Assembly language and C.... All accessible using a DOS command and the OS is either DOS or if your PC is high tech, running under Windows 3.1... and FLOPPY Disk is our Hard DRive :)

Pa-OT lang, just to reminisce...

Kuya, nakakatuwa naman mga nabanggit mo :) Actually I started with DOS (version 5) also. and turbo basic as our introductory PL. and Turbo C as our advanced PL using 386 computer with 4MB RAM (if I recall correctly). We used 3.5" and 5.25" floppy disks as our HDD.
 
Last edited:
Since TS said the above statement, my main concern is this one....


I think you missed this in Post #10....

* without initialization
Code:
            int c;
            while(c<=10){
                Console.WriteLine(c);
                c++;
            }

Actually I did see this and I thought I need not explaining because it was already answered on previous post.... As mentioned variable that you will use in the Loop statement should be initialize....

Code:
int c;

This command actually initializes the value of c (that commands reads, assign "c" as a variable which holds integers with a default value of 0) which is basically the same as saying
Code:
int c=0;

Ref: MSDN
View attachment 305118

So again my statement is correct... YOU HAVE to INITIALIZE first the VARIABLE before using it in WHILE LOOP and DO WHILE LOOP... in FOR LOOP, no NEED because you can initialize it inside the FOR LOOP command itself.... but again, it still needs to have an initial value, else an error will occur....

Pa-OT lang, just to reminisce...

Kuya, nakakatuwa naman mga nabanggit mo :) Actually I started with DOS (version 5) also. and turbo basic as our introductory PL. and Turbo C as our advanced PL using 386 computer with 4MB RAM (if I recall correctly). We used 3.5" and 5.25" floppy disks as our HDD.

So I guess hindi po naglalayo ang Age bracket natin :)

I started with MS-DOS v3 with 386 PC also....
 

Attachments

  • Capture.JPG
    Capture.JPG
    47.7 KB · Views: 1
1.FOR LOOP
ex.
for(int i=0;i<10;i++){
Console.WriteLine(i);
}

output: 0,1,2,3,4,5,6,7,8,9



2.DO WHILE

int a = 10;

do
{
Console.WriteLine(a);
a = a + 1;
}
while (a < 20);

output: 10,11,12,13,14,15,16,17,18,19


3. WHILE

int a = 10;


while (a < 20)
{
Console.WriteLine(a);
a++;
}

output: 10,11,12,13,14,15,16,17,18,19
 
Back
Top Bottom