0% found this document useful (0 votes)
4 views28 pages

Module - 1 - Part-2 (1) - Read-Only

The document outlines decision control statements in Java, including if, if-else, nested if, switch, and the ternary operator, emphasizing their importance in decision-making within programs. It also covers loop structures such as while, do-while, and for loops, along with labeled break and continue statements. Additionally, it provides examples and exercises to illustrate the concepts discussed.

Uploaded by

ayushghonge1309
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views28 pages

Module - 1 - Part-2 (1) - Read-Only

The document outlines decision control statements in Java, including if, if-else, nested if, switch, and the ternary operator, emphasizing their importance in decision-making within programs. It also covers loop structures such as while, do-while, and for loops, along with labeled break and continue statements. Additionally, it provides examples and exercises to illustrate the concepts discussed.

Uploaded by

ayushghonge1309
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Today’s Agenda

⚫Decision Control Statements

⚫If , if-else , nested if

⚫switch

⚫Ternary Operator
Decision Control Statement

⚫Decision making is the most crucial part of any program.

⚫For example :- Deciding whether a given number is even or


odd.

⚫In such cases Java supports various decision control statements


like other programming languages, they are

⚫if, if else, nested if


⚫switch
⚫Ternary Operator
if Statement

⚫Syntax :-
false
if(test_Condition)
{
true
-----
-----
}

* In case there is only a single statement in the body of if-


statement then curly braces can be dropped.
if else

false
if(test_Condition)
{
true
-----
-----
}
else
{
----
----
}
⚫ Every else statement should have one if statement.
if else if

⚫In case of checking multiple conditions there are two options,


1. Use only if statement to check every condition.
2. Use else if statement after the first if statement to check all
the other remaining conditions.
⚫The first method holds a drawback. Can you tell what???
⮚ The drawback in using only if statement to check all the conditions if that,
even after getting the right statement and executing it the compiler still
continues checking all the remaining statements, which increases run time of
the program.
⚫So it is convenient and suggested to use if else if statement to
check multiple conditions.
if else if

false
if(test_Condition)
{
----- true
-----
}
else if( test_Condition)
{
----
---- true
}
else
{
----
}
Nested if

⚫Any conditional statement within the other conditional


statement makes it nested in nature.

if(test condition)
{
if(test condition)
{
----------
----------
}
else
{
---------
---------
}
}
Try this…

⚫Accept an integer from user via command line argument and


check whether it is odd or even in nature.
⚫Solution
class EvenOdd
{
public static void main(String [ ] args)
{
int a=[Link](args[0]);
if(a%2==0)
[Link](“Number is even”);
else
[Link](“Number is odd”);
}
}
The switch Statement

⚫The switch statement is similar to if statement, as it is also a


decision control statement.
⚫It allows a variable to be tested against a list of values where
each value is called a case.
⚫Syntax :-
switch(variable_name or expression)
{ case value : //Statements
break;
case value : //Statements
break;
.
.
default : //Statements
}
The switch Statement

⚫The switch statement can use different variables to check the


conditions, which are byte, short, char, int.
⚫Java 7 onwards use of Strings and enumerated types are also
supported.
⚫Example :-
int month = 8;
switch (month)
{ case 1: [Link]("January“);
break;
case 2: [Link]("February“);
break;
// and so on…
default: [Link](“Invalid Month”);
}
The switch Statement

⚫Case II – Clubbing cases :-


switch(variable name)
{
case value 1: case value 2: case value3:
--------
break;
case value 4: case value 5: case value 6:
-------
break;
default:
-------
}
* Any number of cases can be clubbed together as per condition.
Ternary Operator

• The ternary operator can be used as an alternative to the Java’s


if-else and switch statements.
• But it goes beyond that, and can even be used on the right hand
side of Java statements.
• Syntax :-
• <variable>=(test condition)?<true case>:<false case>;
• Example :-
int a=4;
String str;
str=(a%2==0)? “Even” : “Odd”;
[Link](str);
Today’s Agenda

⚫Loop structures and it’s types.

⚫while and do-while loop.

⚫for loop.

⚫Labeled break and continue.


Loop Structure
Types

⚫Loops in java also are control statements used for repeating


a set of statements multiple times.

⚫They are broadly categorized to be of 2 types :-

⮚ Entry controlled – Condition is checked when the control enters loop


body. Example, while and for loop.

⮚ Exit controlled – Condition is checked after the flow enters loop


body. Example, do-while loop.
while loop

⚫Syntax :-
false
while(test condition)
{ true
----
----
----
}

* The loop continues until the condition is true, as


soon as the condition goes false flow exits the loop
body.
Exercise 1

⚫WAP to accept an integer from the user and print its


factorial. Make sure that your program should print 1 if 0 is
entered ?
⚫Sample output :-
Solution Ex 1
import [Link].*;
class Factorial
{
public static void main(String[ ] args)
{
Scanner kb=new Scanner([Link]);
int n, f=1;
[Link]("Enter a no");
n=[Link]();
while(n>=1)
{
f=f*n;
n--;
}
[Link]("Factorial is "+f);
do-while loop

⚫Syntax :-

do
{
----
----
}while(test condition);

True
False
* Since, the condition is tested at exit, so the loop body
will execute at least once irrespective of the
condition.
Exercise 2

⚫WAP to accept two integer from the user and


display their sum. Now ask the user whether he/she
wants to continue or not. If the answer is Yes then
again repeat the process otherwise terminate the
program displaying the message “Thank you”
⚫Sample Output :-
Solution Ex 2

class AddNos {
public static void main(String [] args)
{
[Link] kb=new [Link]([Link]);
int a,b;
String choice;
do{
[Link]("Enter two integers");
a=[Link]();
b=[Link]();
[Link]("Sum is” "+(a+b));
[Link]("Try again?(Y/N)");
Can we replace next() with
choice=[Link]( );
nextLine() ???
}while([Link]("Y"));
[Link]("Thank you");
}}
Try This

⚫What happens when we do so ???


⚫Can you notice something unusual in this output…

⚫Instead of asking for further input for Y or N, the further


statements execute. Why does this happen?
⚫This is because nextLine() method reads, “enter” as an input
from the keyboard’s buffer.
Buffer

⚫Buffer is a region of a physical memory storage used to


temporarily store data while it is being moved from one
place to another.

⚫So, in above case the keyboard’s buffer is left with an


“ENTER KEY” which we pressed after inputting the
second integer, which nextLine() accepts as an input.

⚫How can we solve this???...

⚫By calling nextLine() before accepting actual input. Since,


this call would clean the buffer.
Solution Ex 2

class AddNos{
public static void main(String [] args){
[Link] kb=new [Link]([Link]);
int a,b;
String choice;
do{
[Link]("Enter two integers");
a=[Link]();
b=[Link]();
[Link]("Sum is "+(a+b));
[Link]("Try again?(Y/N)");
nextLine( );
choice=[Link]( );
}while([Link]("Y"));
[Link]("Thank you");
}
for and labeled for
loop

⚫Syntax :-
false
for(initialization; test condition; statement)
{ true

----
----
----
}

* The initialization and statement part can be left


blank.
break and continue

⚫To terminate the loop and exit its body providing a condition
before it, in such cases we use the statement break.

⚫In situations where we want to skip further steps in a loop


and move directly back to the test condition, there we use the
statement continue.

⚫Let us understand these through an example.

⚫WAP to accept an integer from user & check


whether it is prime or not ?
Solution
Nested Loop

⚫Syntax :-
for(init;condition;stmt)
{
for(init;condition;stmt)
{
-----
-----
}
}
* After completing the inner loop, the control moves back to the
statement part of outer loop.
Labeled break and
continue
⚫Since, the break condition brings us out of the loop body but
in case of nested loop if we want to completely come out of
the loop, then we will use labeled break statement.
⚫Syntax :-

<label name>:
for(init;condition;stmt)
{
for(init;condition;stmt)
{
-----
break <label name>;
}
}

You might also like