To actually dictate how your program will run and which part of code should be executed when it is important to know control flow statements.Some of these are same as what we use in C or C++. So lets look into these statements.
There are 3 main categories of control flow statements -
There are 3 main categories of control flow statements -
- Selection statements - if, if-else and switch.
- Loop statements - while, do-while and for.
- Transfer statements - break, continue, return, try-catch-finally and assert.
Selection statements
- if statement - Syntax for if statement is as follows -
if(condition)
{
//code to be executed if condition is true
}
Lets analyze above code. We have if keyword followed by condition in brackets.This condition must evaluate to be true or false.If the condition evaluates to be true the code in the curly braces following the if statement is executed.If condition evaluates to be false this code is skipped. - if-else statement - Syntax for if-else statement is as follows -
if(condition)
{
//code to be executed if condition is evaluated to be true
}
else
{
//code to be executed if condition is evaluated to be false
}
Very much same as if statement if we neglect the else part. If condition is evaluated to be true code in if part is executed and if condition is evaluated to be false code in else part is executed.
You can have as many if else statements as you need. Moreover you can even cascade them as below -
if(condition1)
{
if(condition2)
{
//condition to be executed when both condition1 and condition2 are true
}
}
You may say above code is same as writing
if(condition1 && condition2)
{
//execute some code
}
yes it will implement same logic as above but let sat you want to execute some code when condition1 is true and condition2 is false in order.In this case you have to use cascading method.
Note - If order is not important you can do something like
if(condition1 && !condition2){//Do something}
! is negation operator
Also please keep in mind that whenever you write any code please stick to proper "indentation". It not only helps readability but will also help you read your own code better. - switch statement - switch statement is used when we know that a variable can take some n values and we need to execute some code corresponding to each value that the variable takes.
Yes it same as using multiple if-else statements but switch statement gives more clarity and readability to the code.
Syntax for switch statement is as follows
switch(vehicle)
{
case 'bike' : System.out.println("I am on a bike");
break;
case 'car' : System.out.println("I am in a car");
break;
default : System.out.println("I prefer walking.");
}
Lets understand this code line by line.
First we have switch keyword follower by a variable called vehicle which is of type String.If the vehicle has value bike then program will print I am on a bike on the standard output else if vehicle has value car then program will print I am in a car on the standard output. If value of vehicle is neither bike nor car then it will go ahead and pick up default case and print I prefer walking.
Note : I have used String in above case to help you understand how switch works but support for string is added only in Java SE 7 . So if you are using any JDK version before this String will not work. You can only have primitive data types like int, char etc.What is the break statement we keep writing in each case?
break statement simply says if a case is selected and code corresponding to that is executed there is no need to check and cases after that. So just break out of switch statement and carry on with the execution.I have seen many beginners do this mistake. So just note this point.
What if i miss this break statement?
To explain this question lets go again to our example code.Lets say vehicle has value bike. So it prints
I am on a bike .Now if you have not provided with break statement it will go ahead till end of switch statement or till it finds a break statement and execute code corresponding to all values except default of course.Important point to be noted in case of switch statement
- A switch works with the
byte
,short
,char
, andint
primitive data types. It also works with enumerated data types(will be discussed later). - default case is executed only when no other cases match. Also since it is the last one to be executed there is no need of break statement. Also position of default case in switch statement is irrelevant.
- Also note that if variable used in switch statement is of integer type you don't have to put single quotes like we did in vehicle case as it was of type String.Ex - case 1 : //execute something.
- It is important to put a break statement in each case.
We will look into Loop statements and Transfer statements in next post.
- A switch works with the
No comments:
Post a Comment