For loop array and if, else condition

Joined
Apr 25, 2017
Messages
248
Reaction score
32
something like this?

Java:
for (int k = 0; k < y.length; k++) {
    if (y[k] >= 10) {
        System.out.print(y[k] * 2 + "\t");
    }
}
 
Joined
Mar 28, 2022
Messages
82
Reaction score
11
Also, a good thing to study is Boolean Algebra. It helps sort out unnecessary if/then statements, verify all possible states are covered, and can be used to simplify complicated if/then trees into their smallest form.
 
Joined
Jul 13, 2022
Messages
1
Reaction score
0
You can use an if-else statement in a loop like this-

for(i=0;i<a.length;i++){
if(i%2==0){
System.out.println(a);
}else{
System.out.println("odd index");
}

you can take this as an example of how to use if-else in a loop. This program will print all the elements present on an even index and print "odd index" in place of all index elements of array.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top