Using while loops instead of for loops in arrays?
I am new to java and doing a java tutorial on using arrays and for loops. What im trying to find out is how it is possible to use a while loop instead.
I would be very grateful if anyone can suggest a way of changing my code to use a while loop.
The code I have written is:
public class DetermineGrade
public static void main(String[]args)
final char[] mark = {'A','B' ,'C' ,'D' ,'F' };
char [] grade = new char[5];
int [] Score ={95,35,65,75};
for (int i=0; i<4; i++)
if (Score >= 90)
grade= mark[0] ;
else if (Score >=80)
grade = mark[1] ;
else if (Score >=70)
grade = mark[2] ;
else if (Score >=60)
grade = mark[3];
else grade = mark[4];
for (int i=0; i<4; i++)
System.out.println("grade = " + Score + ", " + grade);
Thankyou LB
Im trying to underdstand the differences in the
different loops. I can grasp the for loop but I dont
see how a while loop works, or how it id different
from the other?
for (j = INIT; j < MAX; j++) {
// do stuff
}is the same as
j = INIT;
while (j < MAX) {
// do stuff
j++;
}Lee
Similar Messages
-
How to use the select statement in for loop
Hi All,
my question is can we use select statement in for loop like as follows .
for key in select key from one_table.
when i am using this am getting an error like Found select invalid i identifier
how to make use of select statement in for loop
please suggest me .
Thanks
SreeSQL>set serveroutput on;
SQL> DECLARE
BEGIN
FOR Cur_Rec IN (SELECT dname FROM dept) LOOP
DBMS_OUTPUT.PUT_LINE(Cur_Rec.dname);
END LOOP;
END;
SQL>
ACCOUNTING
RESEARCH
SALES
OPERATIONSAs per your requirement always filter the Query beforehand
Like
FOR Cur_Rec IN (SELECT key FROM <table> WHERE key=1) LOOP
END LOOP;Edited by: Lokanath Giri on १ दिसंबर, २०११ ३:५६ अपराह्न -
While loop in a for loop,Is it possible?
Is It possible to call a while loop a particular number of time's using a for loop.
for example :
for(.....){
while(true)
Is this technically possible?
Thanks,Hans9777 wrote:
Based on the code you wrote , the program will enter the for loop and then the while loop . It will never exit the while loop as while(true) means loop infinite times. normally while do-while and for loop can be immplemted for the same function therefore they are rarely used together.Obviously his code only showed the loop structures themselves with no loop bodies. Supposedly there may be a 'break' in the while loop somewhere, so it is not necessarily an infinite loop.
And your remark about them rarely being used together is nonsense. Loops within loops are written for legitimate logic purposes all the time, regardless of what kind of loops they are. -
How to use no of iteration of for loop as the input for the for loop
hi all,
i wanna need some help here..
i'm using for loop to iterate to ceratin no of iteration.. then, i would like to use back the no of iteration as the input of 'N' for the next iteration..
in other word, eg:
1st run:
i insert a control to 'N', let say 80
then the iteration run until 80-1 = 79..
2nd run:
i need to insert the no of "N", let say a control of 120.. i want the loop run for "120 - 79", where the 79 is the no of previous run..
easy to say,
how can i connect the "i' as a control to ''N''
n how to make a "run' or ' GO' and ''stop'' button in the front panel and block diagram after the first run without using the stop and run from the labview window
thanks..Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
Attachments:
Untitled 2.vi 9 KB -
Using CONTINUE-WHEN statement in FOR loop
I'm trying to use CONTINUE-WHEN statement inside of a FOR loop. For example;
FOR cnt IN 1 .. securityTable.COUNT LOOP
CONTINUE WHEN cnt = 3;
--do the job here...
END LOOP;
... but it's not working. It gives me this error;
"PLS-00103: Encountered the symbol "WHEN" when expecting one of the following:
:= . ( @ % ;"
What can be the problem?In the first link there was "CONTINUE WHEN inside of the inner for loop" link and the reference gives this example
<<outer_loop>>
FOR i IN 1..10 LOOP
-- Process data here
FOR j IN 1..10 LOOP
FETCH c1 INTO v_employees;
CONTINUE outer_loop WHEN c1%NOTFOUND;
-- Process data here
END LOOP;
END LOOP outer_loop;
well; I'm not trying to "continue" the outer loop: I just want to "continue" the loop where CONTINUE-WHEN statement belongs to.
I'm using Oracle 10g. -
How to use String Tokeniser inside this for loop?
[i][i]hi there..
i am reading all the file inside a directory and get the latest modified file than i have to tokenise it and juz read the number. For example my file name that is last modified will be abc_123.txt but i just want it to appear 123.
i manage to do it separately how can i put the tokeniser inside my for loop?
my directory
for (int i = 0; i < files.length; i++)
if(files[i].isFile())
if(lastModifiedFile == null){
lastModifiedFile = files[i];
String mylastModifiedFile = lastModifiedFile.toString();
StringTokenizer tokens = new StringTokenizer (mylastModifiedFile,".");
String modified = tokens.nextToken().substring(modified.length-4);*/
if(lastModifiedFile != null)
if(lastModifiedFile.lastModified() < files[i].lastModified())
lastModifiedFile = files[i];
my tokeniser is this
String mylastModifiedFile = lastModifiedFile.toString();
StringTokenizer tokens = new StringTokenizer (mylastModifiedFile,".");
String modified = tokens.nextToken().substring(modified.length-4);*/
}[i][i][i]hi there..
i am reading all the file inside a directory and get ...
tokens.nextToken().substring(modified.length-4);*/
}[/i][i][/i][/i][/i]Is there some reason for this baroque formatting? Please use the
code tags as explained in one of your other threads on this topic:
http://forum.java.sun.com/thread.jspa?threadID=793441&messageID=4510858#4510858 -
How to pass the sequence number of current loop in a for loop in FPGA vi to the host
PCI-7830R
LV 8.2
What I was trying to do is to use multiple DIO to generate pulse at different sequence. Mode one is to automatically sweep from DIO1 to DIO20; mode 2 is several DIOs generate pulse simoutaneously. So I use a case structure to make the selection, in the mean time, I set up two for loop in each case so that I can use multiple pulse generations. For example, in scanning mode, if I set 2 exposures, it sweeps from 1 to 20 then do it again.
Then I need to get the loop sequence number, i of each scenario. So I put an indicator within the first loop, and create a local variable of it and put in the second one. Running the FPGA vi alone, I can see the indicator change in each case from 0 to N-1, N being the for loop time.But in the host vi, I tried to add this indicator as an element in the read/write invoke method, in the debugging mode, I could only see it directly jump to N-1 without all the changes I saw in FPGA vi.
Is it possible to get this number passed correctly from FPGA vi to the host vi? ThanksThanks for the reply Jared.
Excuse me if it looks incorrect, but I'm new to FPGA programming, so I would have to look into the FIFO you referred to. I used local variables because for one thing I have several different cases containing for loop in each of them, and I only want one indicator for the "i". If I put the indicator out of any for loop, it's only gonna show the last number which is N-1. For the other thing, it seems like property nodes are not allowed in FPGA vi. And by doing this, I can see the i number changing from 0 to N-1 in each case, well, in FPGA vi's front panel. But while I ran the host vi with everything, the indicator in host vi's front panel only showed the last number N-1. It may be the reason you said, it happened too fast before the indicator in host vi can catch it.
What I want to realize is to group the data I collect in host vi, for example, when I choose multiple exposure in each mode, and the FPGA runs 1 through 20 then do it again, I want the data stored in two groups using the loop sequence number as the seperator in file name. So it goes like 1-1, 2-1.......20-1; then 1-2, 2-2,.....20-2. -
Java For-each loop to JavaFX for loop
Hi
I have a the following Java for-each loop :
private int w = 250;
private int h = 100;
private int targetPixel[];
private final static int a = 0xff000000;
for (int i=w*h-1;i>=0;i--)
targetPixel=a;
My problem is since in JavaFX their is no for each loop how do you convert this java for-each loop to a JavaFX loop? Could you please provide me a solution with a sample code.
Thank you.How to resolve this code to java fx?
int particles = 10;
int textures = 32;
Random rnd =new Random();
IDX particle=new IDX [particles];
for(int i=0;i<particles;i++)
particle=new IDX (textures*i/particles,rnd);
Here new IDX is a class like the following class IDX
double x;
double y;
double z;
int xx;
int yy;
int index; //Texture index
public IDX (int textureID,Random rnd)
x=rnd.nextDouble()*2-1;
y=rnd.nextDouble()*2-1;
z=rnd.nextDouble()*2-1;
index=textureID;
my problem is how do you pass values to a java fx class constructor if javafx class has constructors. please help?
Edited by: R34GTR on May 14, 2009 8:29 AM -
Is it possible to execute all the iterations of the inside for loop for each iteration of the outside for loop? I have attached a screenshot of my program if it helps. If it is possible could you tell me how to do it plz?
Solved!
Go to Solution.
Attachments:
Capture.PNG 53 KBOne thought I have is that I think you want those feedback nodes reinitializing for each iteration of the outter loop. Personally, I would change them to be shift registers (it would clean up your diagram a little bit).
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines -
Using CONNECT BY LEVEL with For Loop Doesn't Work
The procedure listed below inserts only one record in table whereas i need 10 records to be inserted in table.
this is just a test procedure..
CREATE OR REPLACE PROCEDURE P_TEST
AS
BEGIN
FOR I IN (SELECT LEVEL num FROM dual CONNECT BY LEVEL <= 10)
LOOP
INSERT INTO TEMP_VMS VALUES(I.num);
END LOOP;
END;
END;
/Salim Chelabi wrote:
Or with 9ir2
INSERT INTO TEMP_VMS
SELECT COLUMN_VALUE
FROM TABLE (SYS.dbms_debug_vc2coll (24, 34, 25));
SELECT *
FROM TABLE (SYS.dbms_debug_vc2coll (24, 34, 25));
COLUMN_VALUE
24
34
25
3 rows selected.
http://laurentschneider.com/wordpress/2007/12/predefined-collections.html
That doesn't split strings...
SQL> ed
Wrote file afiedt.buf
1 with t as (select 'a,b,c,d,e' str from dual)
2 --
3 SELECT *
4* FROM t, TABLE (SYS.dbms_debug_vc2coll (t.str))
SQL> /
STR
COLUMN_VALUE
a,b,c,d,e
a,b,c,d,e... it only defines a table of values -
A 'For' loop within another 'For' loop
This is the loop:
for (int i = 1; i<=5; i++) {
for (int k = 1; k<=3; k++) {
System.out.println("i is " + i + ", k is " + k);
How does it execute? After it goes through the loop i = 1, and k = 1, apparently it then doesn't go to i = 2, but instead it increments k to 2, and i remains as 1 until k is greater than 3. I'm confused how this works. Does the first loop execute first, and then the second loop executes, but then the program exits out of the second loop only when k is greater than 3?
Much appreciate your help.
YashTry to imagine expanding the loops. In the example you provided, the loops aren't that big, so it's pretty easy.
for (int i = 1; i<=5; i++) {
for (int k = 1; k<=3; k++) {
System.out.println("i is " + i + ", k is " + k);
}becomes
int i = 1;
for (int k = 1; k<=3; k++) {
System.out.println("i is " + i + ", k is " + k);
i = 2;
for (int k = 1; k <= 3; k++) {
System.out.println("i is " + i + ", k is " + k);
i = 3;
for (int k = 1; k <= 3; k++) {
System.out.println("i is " + i + ", k is " + k);
i = 4;
for (int k = 1; k <= 3; k++) {
System.out.println("i is " + i + ", k is " + k);
i = 5;
for (int k = 1; k <= 3; k++) {
System.out.println("i is " + i + ", k is " + k);
}which becomes
int i = 1;
int k = 1;
System.out.println("i is " + i + ", k is " + k);
k = 2;
System.out.println("i is " + i + ", k is " + k);
k = 3;
System.out.println("i is " + i + ", k is " + k);
i = 2;
k = 1;
System.out.println("i is " + i + ", k is " + k);
k = 2;
System.out.println("i is " + i + ", k is " + k);
k = 3;
System.out.println("i is " + i + ", k is " + k);
i = 3;
k = 1;
System.out.println("i is " + i + ", k is " + k);
k = 2;
System.out.println("i is " + i + ", k is " + k);
k = 3;
System.out.println("i is " + i + ", k is " + k);
i = 4;
k = 1;
System.out.println("i is " + i + ", k is " + k);
k = 2;
System.out.println("i is " + i + ", k is " + k);
k = 3;
System.out.println("i is " + i + ", k is " + k);
i = 5;
k = 1;
System.out.println("i is " + i + ", k is " + k);
k = 2;
System.out.println("i is " + i + ", k is " + k);
k = 3;
System.out.println("i is " + i + ", k is " + k); -
Attached please find my test vi(i use while loop instead of for loop,cause my labview version is 8.2, which don't have interrupt condition item in for loop. )
i wanted to stop the vi while the vi was running, such as i pressed the stop button when i was 4, the vi wouldn't stop immediately,it first finished the current cycle, gave me the 5th random number and time delay, then it ran the next cycle and gave me the 6th random number and stop, so i had to wait some time(time delay+the 6 cycle duration)since i pressed the button, now i hope someone can give me some advice that don't let the next cycle happen and stop the vi immediately.
Solved!
Go to Solution.
Attachments:
test.vi 13 KB413332985 wrote:
to altenbach:
thanks for your help, it's very helpful. But i still have a small problem, your example vi don't creat the first random number immediately, it waits 500 million seconds. If i change the number "500" to a much bigger number, i have to wait a longer time to get the first number, so how can i get the first number immediately while others wait?
No, that's not correct. The initial timeout is 0, so it generates the first random number immediately. Try it once more.
Here's a modification that shows it more clearly. Once the program is started, you need to press another button to start with the random numbers. As you see, the first one appears immediately.
LabVIEW Champion . Do more with less code and in less time .
Attachments:
testtimeout2.vi 16 KB -
While loop and for loop condition terminal
Hello friends,
I am using labview 8.6. The condition terminal of the while loop and conditional for loop is behaving in just the opposite way.
When i wire a true to the condition terminal of my loop, the while loop continues to run when I click on run. when I wire a FALSE, it stops.
Is there any setting change that I have to make it to get it work properly.
Please suggest on this.
Thanks and regards,
HerokPlease do NOT attach .bmp images with the extension changed to .jpg to circumvent the forum filters!
Herok wrote:
I am sending you the VI. I am not sure if this would help you because only in 2 computers this behaviour is seen. In others, it works as it is supposed to work.
Whatever you are seeing must be due to some corruption or folding error. It all works fine here.
To make sure there are no hidden objects, simply press the cleanup button which would reveal any extra stuff (which is obviously not there). Does it fix itself if you click the termination terminal an even number of times? What if you remove the bad loop and create a new one?
Could it be you have some problems with the graphics card and the icon of the conditional terminal does not update correctly?
Whay happens if you connect a control instead of a diagram constant?
What is different on the computers where it acts incorrectly (different CPU (brand, model), #of cores, etc.)
LabVIEW Champion . Do more with less code and in less time . -
Geting ORA 936 -while building dynamic query in for loop.
HI,
I hav written a SP and its compiled and giving me the results for the data I am quering but giving ORA936 - missing expressin and giving ORA 6512 at 2 lines. Please help me me out where I am going wrong.
CREATE OR REPLACE TYPE LIST as VARRAY(5000) of NUMBER;
CREATE OR REPLACE PROCEDURE test( V_id IN LIST,
v-fdate IN DATE,
v-tdate IN DATE,
v_flg IN CHAR)
AS
TYPE opfld IS RECORD (
FID TAB.ID%TYPE,
FCURR TAB.CURR %TYPE,
FCNT TAB.CNT%TYPE,
FTOT TAB.TOT%TYPE );
TYPE OPLIST IS REF CURSOR;
Rc oplist;
Edtab opfld;
Selstr VACHAR2(200);
Whstr1 VACHAR2(200);
Whstr2 VACHAR2(200);
Whstr3 VACHAR2(200);
Whstr VACHAR2(200);
Grpstr VACHAR2(200);
Andstr VACHAR2(200);
Fullstr VACHAR2(200);
BEGIN
Selstr := ‘SELECT col1,col2,sum(col3),sum(col4) from tab1 ’;
Whstr1 := ‘ WHERE tdate BETWEEN TO_DATE(‘||’’’’||vfdate||’’’’||’)’;
Whstr2: = ‘ AND TO_DATE(‘||’’’’||vtdate||’’’’||’)’;
Whstr := whstr1||whstr2;
Grpstr := ‘GROUP BY col1,col2’;
IF v_flg IS NOT NULL THEN
Whstr3 := whstr||’AND FLAG = ‘||v_flg;
else
Whstr3 := whstr ;
END IF;
FOR I IN 1..id.COUNT LOOP
BEGIN
Andstr := ‘ AND id =’|| v_id(i);
Whstr := whstr3 || andstr ;
Fullstr := selstr||whstr||grpstr;
OPEN rc for fullstr;
LOOP
FETCH rc into edtab
EXIT when rc%NOTFOUND;
END LOOP;
Andstr := ‘’;
Whstr :=’’;
END; ---BEGIN of FOR loop
END LOOP; -- for end
CLOSE rc;
END; -- proc end
Created above SP and its compiled. When I try to execute the SP with below code I am getting--
DECLARE
V_t LIST;
BEGIN
V_t:= LIST();
V_t.EXTEND(10);
V_t(1) := 10;
V_t(2) := 20;
Test(v_t,’25-APR-2012’,’30-APR-2012’,’Y’);
END;
The SP is building correct dynamic querry and its getting executed for id = 10 and 20, giving the correct results but though I defined only 2 ids while executing and written FOR lOOP upto ID.COUNT its building query for third time and that time its not getting id, so for the third time the last part of where clause is built like
SELECT.....
WHERE....
AND id = (its taking blank here)
GROUP BY ...;
and thus its giving me ORA 936 missing expression and ORA 06152 at line of RECORD TYPE declaration and line of OPEN rc for fullstr;
ERROR at line 1
ORA 00936 misssing expresiion
ORA 006152 at line 9
ORA 006512 at line 87
I hav put DBMS_OUTPUT statements @ every build of dynamic wuery and its correct. here I have not copid the code I hav typed hereso not wriiten DBMS_OUTPUT.
I tried lot of ways redefining Andstr := ‘’;
Whstr :=’’; after for endloop but its giving the same error. is this because I am using varray? If am providing 2 ids in anonymous block then how its building query for third blank ID value.
Please help me out here..
Thanks a lot..>
I hav written a SP and its compiled and giving me the results for the data I am quering
>
Then you should have posted the one that compiles and gives you results.
The code you posted will not compile without errors. Edit your original post and provide the corrected code.
When you do please add \ tags on the line before and after the code to preserve formatting. See the FAQ for details. -
Why use cursor and for loop?
Hi All
So in general why would we use a cursor and a for loop to do update in a stored procedure?
Why wouldnt we just use a single update statement ?
is there compelling reason for using a cursor and a for loop: I am reading some code from a co-worker that the business logic for the select (set need to be updated) is complex but the update logic is simple (just set a flag to (0 or 1 or 2 or 3 or 4).
But eventually the select come down to a key (row_id) so I re-write it using just a single sql statement.
The size of the main table is about 2.6 to 3million rows
Any thoughts on that??
The code below I just do a google for cursor for update example in case for something to play with
-Thanks for all your input
create table f (a number, b varchar2(10));
insert into f values (5,'five');
insert into f values (6,'six');
insert into f values (7,'seven');
insert into f values (8,'eight');
insert into f values (9,'nine');
commit;
create or replace procedure wco as
cursor c_f is
select a,b from f where length(b) = 5 for update;
v_a f.a%type;
v_b f.b%type;
begin
open c_f;
loop
fetch c_f into v_a, v_b;
exit when c_f%notfound;
update f set a=v_a*v_a where current of c_f;
end loop;
close c_f;
end;
exec wco;
select * from f;
drop table f;
drop procedure wco;
Joining multiple tables
create table numbers_en (
id_num number primary key,
txt_num varchar2(10)
insert into numbers_en values (1, 'one' );
insert into numbers_en values (2, 'two' );
insert into numbers_en values (3, 'three');
insert into numbers_en values (4, 'four' );
insert into numbers_en values (5, 'five' );
insert into numbers_en values (6, 'six' );
create table lang (
id_lang char(2) primary key,
txt_lang varchar2(10)
insert into lang values ('de', 'german');
insert into lang values ('fr', 'french');
insert into lang values ('it', 'italian');
create table translations (
id_num references numbers_en,
id_lang references lang,
txt_trans varchar2(10) not null
insert into translations values (1, 'de', 'eins' );
insert into translations values (1, 'fr', 'un' );
insert into translations values (2, 'it', 'duo' );
insert into translations values (3, 'de', 'drei' );
insert into translations values (3, 'it', 'tre' );
insert into translations values (4, 'it', 'quattro');
insert into translations values (6, 'de', 'sechs' );
insert into translations values (6, 'fr', 'six' );
declare
cursor cur is
select id_num,
txt_num,
id_lang,
txt_lang,
txt_trans
from numbers_en join translations using(id_num)
left join lang using(id_lang)
for update of translations.txt_trans;
rec cur%rowtype;
begin
for rec in cur loop
dbms_output.put (
to_char (rec.id_num , '999') || ' - ' ||
rpad (rec.txt_num , 10 ) || ' - ' ||
rpad(nvl(rec.txt_trans, ' '), 10 ) || ' - ' ||
rec.id_lang || ' - ' ||
rpad (rec.txt_lang , 10 )
if mod(rec.id_num,2) = 0 then
update translations set txt_trans = upper(txt_trans)
where current of cur;
dbms_output.put_line(' updated');
else
dbms_output.new_line;
end if;
end loop;
end;
/Edited by: xwo0owx on Apr 25, 2011 11:23 AMAdding my sixpence...
PL/SQL is not that different from a SQL perspective than any other SQL client language like Java or C# or C/C++. PL/SQL simply integrates the 2 languages a heck of a lot better and far more transparent than the others. But make no mistake in that PL/SQL is also a "client" language from a SQL perspective. The (internal) calls PL/SQL make to the SQL engine, are the same (driver) calls made to the SQL engine when using Java and C and the others.
So why a cursor and loops in PL/SQL? For the same reason you have cursors and loops in all these other SQL client languages. There are the occasion that you need to pull data from the SQL engine into the local language to perform some very funky and complex processing that is not possible using the SQL language.
The danger is using client cursor loop processing as the norm - always pulling rows into the client language and crunching it there. This is not very performant. And pretty much impossible to scale. Developers in this case views the SQL language as a mere I/O interface for reading and writing rows. As they would use the standard file I/O read() and write() interface calls.
Nothing could be further from the truth. SQL is a very advance and sophisticated data processing language. And it will always be faster than having to pull rows to a client language and process them there. However, SQL is not Turing complete. It is not the procedural type language that most other languages we use, are. For that reason there are things that we cannot do in SQL. And that should be the only reason for using the client language, like PL/SQL or the others, to perform row crunching using a client cursor loop.
Maybe you are looking for
-
How do I get "Now Playing" Cover Artwork to show in bottom/left of screen? This has mysteriously disappeared. Thank you!
-
Web dynpro Application session parameter
Need pointers for the following queries: 1.Can I fetch Web Dynpro java application session parameter ? 2.Is it possible to pass the fetched session parameter of a web Dynpro java application to a URL while creating an external window ? Regards Ra
-
Canon MX 922 Faxes not being picked up
Having trouble with my new Canon MX922 not picking up all faxes. Some come through ok, others it doesn't pick up. I have it set for DRPD since my fax line is a double ring on my regular phone line. My old Canon MX850 had no trouble with this same
-
Layer question???
Hi all, I am a relatively novice photoshop user and received photoshop elements with the purchase of my Canon DSLR in 2004. Right now I am trying to add another layer to my base layer and i want to add another one of my photos(jpeg file). I am able
-
Guide: Automatic Pause Music Player when Unpluggin...
/t5/Pool-of-Knowledge/Guide-Pausing-Music-Player-on-unplugging-headset/m-p/637614/highlight/false#M1... So for all you guys, here's the solution...^_^ If you find my post helpful please click the green star on the left under the avatar. Thanks.