Constructs in Java

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Conditional construct

Control flow:
Instruct the program how to make a decision and how further processing should be done
on the basis of that decision.
Statement:
1. A program is a set of instructions which are used to perform a particular task.
2. These instructions are called statements in java.
3. Every statement in java must end with semicolon (;).
Compound statement:
1. A block is group of statements that form a single compound statement.
2. Compound statement are statements that contain operations within a block that can be
surrounded by braces. ({…..})
Java comparison operators: (returns true)
== equal to
!= not equal to
< less than
> greater than
<= less than equal to
>= greater than equal to

Control statements:
The statements that control the order of the execution of the statements in a program are
termed as control statements.
There are 3 categories of control statements:
A. Decision making/branching/conditional constructs
B. Looping constructs
C. jumping statements
Control statements
Decision making Constructs LoopingConstructs Jumping statements
if for return
variants of if while break
switch do..while continue
A. conditional constructs:
1. depending on some condition or situation a statement or a group of statements are
executed.
2. These are used to make decision in a program.
3. They are used to evaluate whether a condition is true or false.
4. Conditional statements are
a. if
b. variants of if
c. switch
5. ways of using if statement.
i. if
ii. nested if
iii. if else
iv. nested if else
v. if else if ladder.
a.
1. if
a. the simplest but most important conditional statements is ‘if’.
b. Syntax:
if(condition) if(condition)
statements; OR {
statements;
}
Example: to check whether number is even.
if(number%2==0)
System.out.println(“Number is even..”);
Explanation:
If the expression in the parenthesis evaluates to true, the statement is executed. And if
the expression evaluates to false, statement is skipped and execution continues at the
statement following ‘if’ statement.
2. Nested if: One if statement within another if.
Syntax:
if(condition)
{
if(condition 2)
{
Statements;
}
}
Example: to check whether entered number is two digit or not..
import java.util.*;
public class TwoDigit
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a;
System.out.println(“Enter number=”);
a=sc.nextInt();
if(a<100)
{
if(a>=10)
{
System.out.println(“Number is two digit number”+a);
}//end of inner if
}//end of outer if
System.out.println(“Number is not two digit number”+a);
}//end of main
}//end of class
3. if else
it is similar to ternary operator (? : )
boolean expression ? value 1 : value 2;
if the boolean expression evaluates to true, value 1 is printed and if boolean expression
evaluates to false value 2 is printed.
Syntax:
if(condition)
{
Statements of if;
}
else
{
Statements of else;
}
Explanation: if the condition is true statements of if block will be executed and if the condition is
false statements of else block will be executed.
Example: check whether the person is eligible for vote or not.
import java.util.*;
public class Eligibility
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int age;
System.out.println(“Enter person age=”);
age=sc.nextInt();
if(age>18)
{
System.out.println(“Person is eligible to vote.”);
}
else
{
System.out.println(“Person is not eligible to vote.”);
}
}//end of main
}//end of class
4. nested if else:
one if else into another if else. Syntax:
If else within if If else within else If else within if and else
if(condition 1) if(condition 1) if(condition 1)
{ { {
if(condition 2) statements if(condition 2)
{ }//end of if {
Statements; else Statements;
} { }
else if(condition 2) else
{ { {
Statements; Statements; Statements;
} } }
}//end of if else }//end of if
else { else
{ Statements; {
Statements; } if(condition 3)
} }// end of else {
Statements;
}
else
{
Statements;
}
}// end of else

Example :
To find greater among 3 numbers:
import java.util.*;
public class Eligibility
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a,b,c;
System.out.println(“Enter three numbers=”);
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
if(a>b)
{
if(a>c)
System.out.println(“greatest is ”+a);
else
System.out.println(“greatest is ”+c);
}// end of if
else
{
if(b>c)
System.out.println(“greatest is ”+b);
else
System.out.println(“greatest is ”+c);
}//end of else
}//end of main
}//end of class
5. if else if ladder:
it is used where multiple decisions are required.
Syntax:
if(condition 1)
{
Statements;
}
else if(condition 2)
{
Statements;
}
else if(condition 3)
{
Statements;
}
else
{
Statements;
}
Example 1: compare two numbers.:
if(a>b)
System.out.println(“Big=”+a);
else if(b>a)
System.out.println(“Big=”+b);
else
System.out.println(“Both are equal.”);
Example 2: Enter marks from user and print the grade according to following conditions,
If marks >= 90 garde A+
marks >=80 and marks <90 grade A
marks >=70 and marks <80 grade B
marks >=60 and marks <70 grade C
marks >=40 and marks <60 grade D
marks<40 grade Fail
import java.util.*;
public class Grade{
public static void main (String args []){
Scanner sc=new Scanner(System.in);
int marks;
System.out.println(“Enter marks=”);
marks=sc.nextInt();
if(marks<40)
System.out.println(“Grade Fail”);
else if(marks >= 40 && marks < 60)
System.out.println(“Grade D”);
else if(marks >= 60 && marks < 70)
System.out.println(“Grade C”);
else if(marks >= 70 && marks < 80)
System.out.println(“Grade B”);
else if(marks >= 80 && marks<90)
System.out.println(“Grade A”);
else if(marks >=90)
System.out.println(“Grade A+”);
}//end of main
}//end of class
Example 3: write a java program to enter a number from user and check if the number is
zero, positive or negative using if-else-if ladder.
import java.util.Scanner;
public class Number
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int num;
System.out.println(“Enter a number=”);
num=sc.nextInt();
if(num == 0)
System.out.println(“Entered number is 0. ”);
else if(num > 0)
System.out.println(“Entered number is positive. ”);
else if(num < 0)
System.out.println(“Entered number is negative. ”);
else
System.out.println(“YoueEntered invalid value. ”);
}// end of main
}//end of class

If there is multiway branching, the use of ‘if’ statement becomes complex. In such conditions
where multiway branching is required we can use ‘switch’ statement.
c. switch:
1. this statement is a multi-branching statement that transfer the program control
matching case depending on the value of the control variable.
2. It is mainly used in menu driven programs.
3. The Java switch statement executes one statement from multiple conditions.
4. The switch statement works with byte, short, int, long, enum types, String
5. It is best alternative for if-else-if ladder’.
6. Syntax:
switch(value or variable or condition)
{
case choice1:
statements;
break;
case choice2:
statements;
break;
case choice3:
statements;
break;
.
.
default:
statements;
break;
}
7. switch statement has 4 keywords:
a. switch : uses a control value and works depending on that.
switch(ch)
{


}
b. case : it uses a choice value that can be byte, short, int, long, enum types,
String.
case 1:
statements;
break;
c. break : used to terminate execution of particular case hence it is also known
as case terminator. When break encountered execution will resume.
d. default : if no match are found and default is present in switch construct, the
group of statements associated with default will be executed.

Points to Remember while using switch statement.


• There can be one or N number of case values for a switch expression.
• The case value must be of switch expression type only. The case value must be literal or
constant. It doesn't allow variables.
• The case values must be unique. In case of duplicate value, it renders compile-time error.
• The Java switch expression must be of byte, short, int, long (with its Wrapper type),
enums and string.
• Each case statement can have a break statement which is optional. When control reaches
to the break statement, it jumps the control after the switch expression. If a break
statement is not found, it executes the next case.
• The case value can have a default label which is optional.

Example 1: The example below uses the weekday number to calculate the weekday name:
import java.util.Scanner;
public class Week
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int day;
System.out.println("Enter day number = ");
day=sc.nextInt();
switch (day)
{
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid week number.");
}//end of switch
}//end of main
}//end of class

Fall through
Fall through is a situation in java program takes place when the loop misses out a
break statement between the different cases.
Explanation:
The following general rule in fall through the behavior of switch statement.

• The switch statement terminates, whenever it reaches a break statement, and the
action jumps to the line after the switch statement.
• In a switch statement if no break appears, then the control will fall through to
subsequent cases till it hit a break statement in the loop.

// Java Program to check whether the character is a vowel or not


import java.util.*;
class Vowel{
public static void main(String[] args) {

char ch='f';
switch ( ch ){
a case 'a':
case 'e':
case 'i':
case 'o':
case 'u':System.out.println("Vowel");
break;
default :{
System.out.println("Consonant");
}
}//end of swich
}//end of main
}//end of class

You might also like