Java Programming Assignment Help - Chicago University USA| Java Programming Homework Help - Chicago University USA | Java Programming Project Help - Chicago University USA | Online Java Programming Help - Chicago University USA

In Programming homework help student face problem in implementing Control flow statements in Java Programming like Decision Making: if-then, if-else, if-else if-else.Java provides control flow statements for decision making (if, if-else, switch) and for looping (for, while, do-while). Academic problems of Java Assignment Help Chicago University USA following is simple example to understand the control flow statements programming :

Block of statements
In java the statements between { } are called block of statements.
{
//block of code
}

Decision making statements
if statement
“ if ” tell the program execute the section of code when the condition evaluates to true.
Syntax
if(condition){
// Executes if the condition true
}

Example
class Grades{
public static void main(String args[]){
int marks = 20;
if(marks < 35 ){
System.out.println("You failed in the Exam. Better luck next time");
}
}
}
Output
You failed in the Exam. Better luck next time

points to remember
In java the condition in the if must evaluates to boolean value, other wise compiler will throw error

Example
class Grades{
public static void main(String args[]){
int var1;
if(var1 = 10){

}
}
}

When you try to compile the above program, you will get the below error
Grades.java:5: error: incompatible types
if(var1 = 10){
^
required: boolean
found: int
1 error
if-else statement
Syntax:
if(condition){

}
else{

}

If the condition true, then if block code executed. other wise else block code executed.

Example
class Grades{
public static void main(String args[]){
int marks = 36;

if(marks < 35)
System.out.println("You are failed");
else
System.out.println("You are passed");
}
}

Output
You are passed

Observation
In the above program we haven't written the if, else block codes in between { }, since if there is only one statement in between the blocks, we can omit { }.

if-else if-else
By using if-else if-else construct, you can choose number of alternatives.

An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement.

class Grades{
public static void main(String args[]){
int marks = 69;

if(marks < 35)
System.out.println("You are failed");
else if( marks < 50)
System.out.println("You are passed and got third class");
else if( marks < 60)
System.out.println("You are passed and got second class");
else if( marks < 70)
System.out.println("You are passed and got first class");
else
System.out.println("You are passed and got distinction");
}
}

Output
You are passed and got first class

Control flow statements : Decision Making: switch
The body of a switch statement is known as a switch block. A statement in the switch block can be labelled with one or more case or default labels. The switch statement evaluates its expression, then executes all statements that follow the matching case label.

switch works with the byte, short, char, and int primitive data types. It supports enums also

java 7 supporting strings also in switch case evaluation, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer.

Integer, Char, Byte, Short are called wrapper classes. Will discuss about these more on coming posts.

Example
class SwitchEx{
public static void main(String args[]){
int day = 5;

switch(day){
case 1:
System.out.println("Sunday");
break;
case 2:
System.out.println("Monday");
break;
case 3:
System.out.println("Tueday");
break;
case 4:
System.out.println("Wednesday");
break;
case 5:
System.out.println("ThursDay");
break;
case 6:
System.out.println("Friday");
break;
case 7:
System.out.println("Saturday");
break;
default:
System.out.println("You entered wrong day number");
}
}
}

Output
ThursDay

Explanation
As you see in the above program, variable “day” set to the value 5. So in the switch case, case 5 is executed. If the day is set to 1, then case 1 will execute and come out of the switch.

You can see in the above program, each case has a break statement associates with it, is it necessary ? Yes of course, if you don't specify the break for case statements, then all the case statement below the evaluated case are executed.

And one more thing is, case statements need not be in proper order. They can be written in any way.

class SwitchEx{
public static void main(String args[]){
int day = 5;

switch(day){
case 2:
System.out.println("Monday");
case 3:
System.out.println("Tueday");
case 4:
System.out.println("Wednesday");
case 5:
System.out.println("ThursDay");
case 6:
System.out.println("Friday");
case 7:
System.out.println("Saturday");
case 1:
System.out.println("Sunday");
default:
System.out.println("You entered wrong day number");
}
}
}

Output
ThursDay
Friday
Saturday
Sunday
You entered wrong day number

As you see in the above program, “day” is set to 5, and there is no breaks in the corresponding cases. So case 5, 6,7, 1 and default are executed.

One more thing is in the above program, case 1 came after 7, it is acceptable behaviour in java.

What is the necessity of default here
If no case is evaluated, then default executes, just like else block in if-else if-else ladder.

Example
class SwitchEx{
public static void main(String args[]){
int day = 9;

switch(day){
case 1:
System.out.println("Sunday");
break;
case 2:
System.out.println("Monday");
break;
case 3:
System.out.println("Tueday");
break;
case 4:
System.out.println("Wednesday");
break;
case 5:
System.out.println("ThursDay");
break;
case 6:
System.out.println("Friday");
break;
case 7:
System.out.println("Saturday");
break;
default:
System.out.println("You entered wrong day number");
}
}
}

Output
You entered wrong day number

Some points to remember
1. When to use if-else if -else ladder, than switch ?
Switch case won't supports range checks like age>30, year<2000 etc., in those cases better to go for if-else if-else ladder.

 

Our Team

We have 2500+ expert pool from all over the world. They are highly qualified and well experienced. All experts have experinced in teaching and good command over programming topics. Some of our experts are as follows:


John Doe

JAVA EXPERT(10+ Yrs Exp.)

Larry Doe

C EXPERT(12+ Yrs Exp.)

Ranith Kays

C++ EXPERT(8+ Yrs Exp.)

Joan Ray

OPEN SOURCE LANGUAGE EXPERT(8+ Yrs Exp.)