View Full Version : Can I use for loops inside switch{} ??
shachi
09-20-2006, 06:31 PM
I think that my title says it all. Well, if you don't get what I mean here's what I mean. Can I use for loops in switch statements like this:
switch(somevariable){
for(var i=0;i<somevalue;i++;j=10;j<someothervalue;j++){
case i:
alert(somearray[j]);
break;
}
}
Thank you for your precious time reading my post.
Not like that, no. The question arises, of course, of why you'd want to. The example you've given can be accomplished with:
for(var i = 0; i < somevalue; ++i)
if(somearray[i + 10])
alert(somearray[i + 10]);
shachi
09-23-2006, 05:23 PM
Thanks Twey, but how do I use that inside a switch statement??
Why would you want to? The loop I provided duplicates the functionality of what you're trying to do with the pseudo-Javascript above. If you tell us what you're trying to do, we can think of a better way to do it.
shachi
09-24-2006, 12:10 PM
I am trying to make a 12 hour clock(maybe there are other easier ways but I want it this way).
d = new Date();
hours = d.getHours();
minutes = d.getMinutes();
seconds = d.getSeconds();
switch(hours){
for(var i=12;i<24;i++;var j=1;j<12;j++){
case i:
hours = j;
break;
}
}
document.getElementById("somediv").innerHTML = hours + minutes + seconds;
setTimeout("somefunc()",1000);
blm126
09-24-2006, 12:57 PM
window.onload = startClock;
function startClock(){
setClock();
setInterval("setClock()",1000);
}
function setClock(){
var d = new Date();;
var hours = check(d.getHours());
var minutes = check(d.getMinutes());
var seconds = check(d.getSeconds());
document.getElementById('clock').innerHTML = hours+':'+minutes+':'+seconds
}
function check(num){
if(num<10){
num = '0'+num;
}
return num;
}
Quick javascript clock for you
window.onload = function() {
var a = function(n) {
return (n.toString().length < 2 ? "0" + n : n);
};
var f = function() {
var d = new Date();
window.document.getElementById("clock").firstChild.nodeValue = a(d.getHours()) + ':' + a(d.getMinutes()) + ':' + a(d.getSeconds());
};
f();
window.setInterval(f, 1000);
};
strikeanywhere
09-24-2006, 03:21 PM
can someone read http://www.dynamicdrive.com/forums/showthread.php?t=13413 and help me out please... its for school
We don't do homework. Why on earth did you decide to post in this thread?
mburt
09-24-2006, 05:12 PM
Quick javascript clock for you
Shachi wanted a *12 hour clock. Not 24 :)
window.onload = function() {
var a = function(n, h) {
return (n.toString().length < 2 ? "0" + n : (h && n > 12 ? n - 12 : n));
};
var f = function() {
var d = new Date();
window.document.getElementById("clock").firstChild.nodeValue = a(d.getHours(), true) + ':' + a(d.getMinutes()) + ':' + a(d.getSeconds());
};
f();
window.setInterval(f, 1000);
};
mburt
09-24-2006, 05:24 PM
Your completely right Twey (here I am, talking to a segment of code...) using a switch{} for a simple clock is pointless. Your way is much more effecient.
mburt
09-24-2006, 05:26 PM
There has to be some initial text in the "clock" container though. If you leave it blank it won't work.
blm126
09-24-2006, 06:40 PM
Shachi wanted a *12 hour clock. Not 24 :)
My bad
True.
window.onload = function() {
var a = function(n, h) {
return (n.toString().length < 2 ? "0" + n : (h && n > 12 ? n - 12 : n));
};
var f = function() {
var d = new Date(),
l = window.document.getElementById("clock");
(l.firstChild || l.appendChild(document.createTextNode(""))).nodeValue = a(d.getHours(), true) + ':' + a(d.getMinutes()) + ':' + a(d.getSeconds());
};
f();
window.setInterval(f, 1000);
};
shachi
09-26-2006, 08:39 AM
Thanks Twey,mburt and blm126 for you code. So I can't use loops inside switch can I??
mwinter
09-26-2006, 11:20 AM
So I can't use loops inside switch can I??
Not in the way you originally posted, no. The loop must be "within" a case statement:
switch (expression) {
case expression:
for (...) {}
}
Mike
shachi
09-26-2006, 03:10 PM
Oh so I can however use loops inside case statements. Thanks mwinter,Twey,mburt and blm126!!!
It should be noted why Mike used quotation marks around "within:" case statements are just labels, and so don't actually have anything within them. The flow of execution jumps to the label then carries on, nothing more.
shachi
09-28-2006, 05:27 PM
Ok, Thanks twey, mwinter, mburt, blm126 and everybody. :)
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.