Posts

Showing posts with the label Control statements

Switch, break, continue, return statements

Image
In previous post we discussed about control statements up to for loop. In this post we will come to know the remaining control statements like switch, break, continue, return statements with respective examples. If you don't have any idea about control statements in java, better follow this link to know about the  Control Statement . Switch statement         When there are several options and we have to choose only one option from the available ones, we can use switch statement. Depending on the selected option, a particular task can be performed. A task represents one or more statements. Syntax:                   switch(variable){                           case value-1 : statement-1;                           case value-2 : statement-2;                           case value-3 : statement-3;                           .                           .                           case value-n : statement-n;                           default

Control statements

Statement similar to smallest unit which completes instruction in itself. So, every statement will get end with semicolon(;) in java.In java there are majorly sequential and control statements are available. if, while, do-while, for, switch, break, continue, return statement will come under the Control statements.         1. sequential statements: These are the statements that executes one by one. 1 2 3 4 5 System . out . println ( "Hello" ); x = y + z ; System . out . println ( x );         These statements can be executed by JVM one by one in a sequential manner, that are called sequential statements . But this type of sequential execution is useful only to write simple programs . If we want to write better and complex programs, we need better control on the flow of execution. This is possible by using conditional statements.          2. control statements: Control statements are the statements which alter the flow of execution and provide better