Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

Class notes 18cs32

Rating
-
Sold
-
Pages
18
Uploaded on
06-05-2024
Written in
2023/2024

Providing knowledge in basics of java with a neat notes

Institution
Course

Content preview

Module 2: Operators and Control Statements Programming in Java (18CS653)


Module 2
Control Statements
 Java’s program control statements can be put into the following categories: selection,
iteration, and jump.
 Selection statements allow your program to choose different paths of execution based
upon the outcome of an expression or the state of a variable.
 Iteration statements enable program execution to repeat one or more statements (that
is, iteration statements form loops).
 Jump statements allow your program to execute in a nonlinear fashion.


Java’s Selection Statements
 Java supports two selection statements: if and switch.


The if statement
 The if statement executes a block of code only if the specified expression is true.
 If the value is false, then the if block is skipped and execution continues with the rest
of the program.
 You can either have a single statement or a block of code within an if statement.
 Note that the conditional expression must be a Boolean expression.

Syntax:
if (<conditional expression>) {
<statements>
}



Example:
public class Example {
public static void main(String[] args) {
int a = 10, b = 20;
if (a > b)
System.out.println("a > b");
if (a < b)



Mrs. Mamatha A, Asst. Prof, Dept., of CSE, SVIT 1

,Module 2: Operators and Control Statements Programming in Java (18CS653)


System.out.println("b > a");
}
}


The if else statement
 The if statement is Java’s conditional branch statement. It can be used to route
program execution through two different paths.
 Here is the general form of the if statement:

Syntax:
if (condition)
statement1;
else statement2;



 Here, each statement may be a single statement or a compound statement enclosed in
curly braces (that is, a block).
 The condition is any expression that returns a boolean value. The else clause is
optional.
 The if works like this: If the condition is true, then statement1 is executed. Otherwise,
statement2 (if it exists) is executed.
Example:
public class Example {
public static void main(String[] args) {
int a = 10, b = 20;
if (a > b)
System.out.println("a > b");
else
System.out.println("b > a");
}
}




Mrs. Mamatha A, Asst. Prof, Dept., of CSE, SVIT 2

, Module 2: Operators and Control Statements Programming in Java (18CS653)


Nested ifs
 A nested if is an if statement that is the target of another if or else.
 When you nest ifs, the main thing to remember is that an else statement always refers
to the nearest if statement that is within the same block as the else and that is not
already associated with an else.
Here is an example:
if(i == 10) {
if(j < 20) a = b;
if(k > 100) c = d; // this if is
else a = c; // associated with this else
}
else a = d; // this else refers to if(i == 10)


The if-else-if Ladder
 A common programming construct that is based upon a sequence of nested ifs is the
if-else-if ladder.
 It looks like this:

if(condition)
statement;
else if(condition)
statement;
else if(condition)
statement;
...
else
statement;



 The if statements are executed from the top down.
 As soon as one of the conditions controlling the if is true, the statement associated
with that if is executed, and the rest of the ladder is bypassed.
 If none of the conditions is true, then the final else statement will be executed.




Mrs. Mamatha A, Asst. Prof, Dept., of CSE, SVIT 3

Written for

Institution
Course

Document information

Uploaded on
May 6, 2024
Number of pages
18
Written in
2023/2024
Type
Class notes
Professor(s)
Suresh
Contains
All classes

Subjects

$10.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
supriyahiremath

Get to know the seller

Seller avatar
supriyahiremath RYMEC BELLARY
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
2 year
Number of followers
0
Documents
5
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions