0% found this document useful (0 votes)
53 views

Java Lecture 4

The document discusses different types of loops in Java programming including for, while, and do-while loops. It provides the syntax for each loop and examples of how to use them, as well as homework problems for students to practice loops.

Uploaded by

digitalm4044
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Java Lecture 4

The document discusses different types of loops in Java programming including for, while, and do-while loops. It provides the syntax for each loop and examples of how to use them, as well as homework problems for students to practice loops.

Uploaded by

digitalm4044
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

‭Java - Introduction to Programming‬

‭Lecture 4‬

‭Loops‬
‭ loop is used for executing a block of statements repeatedly until a particular‬
A
‭condition is satisfied. A loop consists of an initialization statement, a test‬
‭condition and an increment statement.‬

‭ or Loop‬
F
‭The syntax of the for loop is :‬

f‭ or (initialization; condition; update) {‬


‭// body of-loop‬
‭}‬

for‬‭
‭ (‬
int‬‭
‭ i=‬
1‭
‭;
‬‬‭
i<=‬
20‬
‭ ;‬‭
‭ i++) {‬
out‬

System.‬
‭ .println(i)‬
‭ ;‬

}‬

‭ hile Loop‬
W
‭The syntax for while loop is :‬
‭while(condition) {‬
‭// body of the loop‬
‭}‬

‭nt‬‭
i i =‬‭
0‭
;
‬‬
while‬
‭ (i<=‬
‭ 20‬
‭ ) {‬

out‬

System.‬
‭ .println(i)‬
‭ ;‬

i++‬
‭ ;‬

}‬

‭ o-While Loop‬
D
‭The syntax for the do-while loop is :‬
‭do {‬
‭// body of loop;‬
‭}‬
‭while (condition);‬

‭nt‬‭
i i =‬‭
0‭
;
‬‬
do‬‭
‭ {‬
out‬

System.‬
‭ .println(i)‬
‭ ;‬

‭Apna College‬
i++‬
‭ ;‬

}‬‭
‭ while‬
(i<=‬
‭ 20‬
‭ )‭
‭;
‬‬

‭Homework Problems‬
1‭ .‬ P‭ rint all even numbers till n.‬
‭2.‬ ‭Run‬
for‬
‭ (; ;) {‬

System‬
‭ .‬
‭ out‬
‭ .‬
‭ println‬
‭ (‬
‭ "Apna College"‬
‭ );‬

}‬

l‭oop on your system and analyze what happens. Try to think of the reason for‬
‭the output produced.‬

‭3.‬ ‭Make a menu driven program. The user can enter 2 numbers, either 1 or 0.‬

I‭f the user enters 1 then keep taking input from the user for a student’s‬
‭marks(out of 100).‬

‭If they enter 0 then stop.‬

‭If he/ she scores :‬

‭Marks >=90‬‭-> print “This is Good”‬

‭89 >= Marks >= 60‬‭-> print “This is also Good”‬

‭59 >= Marks >= 0‬‭-> print “This is Good as well”‬

‭Because marks don’t matter but our effort does.‬

‭(Hint : use do-while loop but think & understand why)‬

‭BONUS‬

‭Qs. Print if a number is prime or not (Input n from the user).‬

‭[In this problem you will learn how to check if a number is prime or not]‬

‭Apna College‬
‭Homework Solution (Lecture 3)‬

import‬‭
‭ java‬
.‭
‭u‬til‬
.*;‬

public‬‭
‭ class‬‭
Conditions‬‭
{‬
public‬‭
‭ static‬‭
void‬‭
main‬
(‬
‭ String‬‭
‭ args‬
[]) {‬

Scanner‬‭
‭ sc‬‭
=‬‭
new‬‭
Scanner‬
(‭
‭S‬ystem‬
.‬
‭ in‬
‭ );‬

int‬‭
‭ a‬‭
=‬‭
sc‬
.‭
‭n
‬extInt‬
();‬

int‬‭
‭ b‬‭
=‬‭
sc‬
.‭
‭n
‬extInt‬
();‬

int‬‭
‭ operator‬‭
=‬‭
sc‬
.‬
‭ nextInt‬
‭ ();‬

/**‬

* 1 -> +‬

* 2 -> -‬

* 3 -> *‬

* 4 -> /‬

* 5 -> %‬

*/‬

switch‬
‭ (‭
‭o‬perator‬
) {‬

case‬‭
‭ 1‬‭
:‬‭
System‬
.‬
‭ out‬
‭ .‬
‭ println‬
‭ (‬
‭ a‭
‭+‬‬
b‭
‭)‬;‬
break‬
‭ ;‬

case‬‭
‭ 2‬‭
:‬‭
System‬
.‬
‭ out‬
‭ .‬
‭ println‬
‭ (‬
‭ a‭
‭-‬‬
b‭
‭)‬;‬
break‬
‭ ;‬

case‬‭
‭ 3‬‭
:‬‭
System‬
.‬
‭ out‬
‭ .‬
‭ println‬
‭ (‬
‭ a‭
‭*‬‬
b‭
‭)‬;‬
break‬
‭ ;‬

case‬‭
‭ 4‬‭
:‬‭
if‬
(‬
‭ b‬‭
‭ ==‬‭
0‬) {‬

System‬
‭ .‭
‭o
‬ut‬
.‭
‭p‬rintln‬
(‭
‭"‬Invalid‬‭
Division"‬
);‬

}‬‭
‭ else‬‭
{‬
System‬
‭ .‭
‭o
‬ut‬
.‭
‭p‬rintln‬
(‭
‭a‬‬
/‭
‭b‬‬
);‬

}‬

break‬
‭ ;‬

case‬‭
‭ 5‬‭
:‬‭
if‬
(‬
‭ b‬‭
‭ ==‬‭
0‬) {‬

System‬
‭ .‭
‭o
‬ut‬
.‭
‭p‬rintln‬
(‭
‭"‬Invalid‬‭
Division"‬
);‬

}‬‭
‭ else‬‭
{‬
System‬
‭ .‭
‭o
‬ut‬
.‭
‭p‬rintln‬
(‭
‭a‬‬
%‭
‭b‬‬
);‬

}‬

break‬
‭ ;‬

default‬‭
‭ :‬‭
System‬
.‭
‭o‬ut‬
.‭
‭p‬rintln‬
(‭
‭"‬Invalid Operator"‬
);‬

}‬

}‬

‭Apna College‬
}‬

‭Apna College‬

You might also like