For loop array and if, else condition


Ad

Advertisements

Joined
Apr 25, 2017
Messages
212
Reaction score
26
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
48
Reaction score
6
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.
 
Ad

Advertisements

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

Top