Jump to content

Java for loops

stefanmz
Go to solution Solved by RockSolid1106,

First, you initialize your variable, then the condition, followed by what happens to the variable after the loop is executed.

for (int i=0; i<=10; i++){
  //code
}

 

You can also skip certain parts of it by just removing the statement, but leaving the semicolon there.

int i = 5 // already initializing my var

for( ; i<=10; i++){
  // code
 }

 

Hey if I have a for loop in java and I want for example

for(int a>=b and int a<=c;a<1000;a++){
...code
}

how do I do that? The a>=b and a<=c part?

Link to comment
Share on other sites

Link to post
Share on other sites

All conditional checks go in the second part, where you currently have `a<1000`. First part is for variable initialization.

HAL9000: AMD Ryzen 9 3900x | Noctua NH-D15 chromax.black | 32 GB Corsair Vengeance LPX DDR4 3200 MHz | Asus X570 Prime Pro | ASUS TUF 3080 Ti | 1 TB Samsung 970 Evo Plus + 1 TB Crucial MX500 + 6 TB WD RED | Corsair HX1000 | be quiet Pure Base 500DX | LG 34UM95 34" 3440x1440

Hydrogen server: Intel i3-10100 | Cryorig M9i | 64 GB Crucial Ballistix 3200MHz DDR4 | Gigabyte B560M-DS3H | 33 TB of storage | Fractal Design Define R5 | unRAID 6.9.2

Carbon server: Fujitsu PRIMERGY RX100 S7p | Xeon E3-1230 v2 | 16 GB DDR3 ECC | 60 GB Corsair SSD & 250 GB Samsung 850 Pro | Intel i340-T4 | ESXi 6.5.1

Big Mac cluster: 2x Raspberry Pi 2 Model B | 1x Raspberry Pi 3 Model B | 2x Raspberry Pi 3 Model B+

Link to comment
Share on other sites

Link to post
Share on other sites

First, you initialize your variable, then the condition, followed by what happens to the variable after the loop is executed.

for (int i=0; i<=10; i++){
  //code
}

 

You can also skip certain parts of it by just removing the statement, but leaving the semicolon there.

int i = 5 // already initializing my var

for( ; i<=10; i++){
  // code
 }

 

On 4/5/2024 at 10:13 PM, LAwLz said:

I am getting pretty fucking sick and tired of the "watch something else" responses. It's such a cop out answer because you could say that about basically anything, and it doesn't address the actual complaints. People use it as some kind of card they pull when they can't actually respond to the criticism raised but they still feel like they need to defend some company/person. If you don't like this thread then stop reading it. See how stupid it is? It's basically like telling someone "shut the fuck up". It's not a clever responsive, it doesn't address anything said, and it is rude. 

 ^

 

bruh switch to dark mode its at the bottom of this page

VPN Server Guide

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, RockSolid1106 said:

First, you initialize your variable, then the condition, followed by what happens to the variable after the loop is executed.

for (int i=0; i<=10; i++){
  //code
}

 

You can also skip certain parts of it by just removing the statement, but leaving the semicolon there.

int i = 5 // already initializing my var

for( ; i<=10; i++){
  // code
 }

 

oh ok thanks! I got it now!

Link to comment
Share on other sites

Link to post
Share on other sites

If you need more complex conditions, you can also use other loop-constructs like while.

int a = 
int b = 
int c = 

while (a >= b && a <= c) {
     do something
     do not forget to modify a/b/c
}

 

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Eigenvektor said:

If you need more complex conditions, you can also use other loop-constructs like while.

int a = 
int b = 
int c = 

while (a >= b && a <= c) {
     do something
     do not forget to modify a/b/c
}

 

oh yeah that actually could be helpful in my case, forgot about that

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×