click here for complete presentation on LOOPS 3 loops in Java
While Loops
Anatomy of a while Loopwhile( condition ) statement1;
}
statement2; .. statementN; int i = 1; System.out.println(i); i++; Output Using an if with a while loopint i = 1; if (i ==2 || i ==4) myCtr2++; i++; }System.out.,println(“The counter is ” + myCtr2); //output The counter is 2 for Loops
A for loop usually has 3 parts in its header:
Anatomy of a for Loopfor(Initialization; Condition; Increment) statement1;
}
statement2; .. statementN; for(int i=1; i < 5; i++) System.out.println(i); Output Using an if with a for loopint ctr=0; if (i==1 || i == 3 || i == 4) ctr = ctr + 1; } System.out.println(ctr); //output 3 ctr=3 |
Scanner ClassCredit: A Slides are modified with permission from Barry Wittman at Elizabethtown College |
|