The do while statement The do while statement evaluates the test after executing the body of a loop. The structure of the Java do-while statement is as below: do { statements } while (expression); Let us write the same program to print the squares of numbers from 1 to 5. This time we will use a do-while loop. The following steps need to be performed. 1. Initialize number = 1 2....
Continue reading »
February 27, 2016 evirtualguru_ajaygourOnline Computer EducationNo Comment
The while statement The while statement evaluates the test before executing the body of a loop. The structure of the Java while statement is as below: while (expression) { statements } Let us write a program to print the squares of numbers from 1 to 5. The following steps need to be performed. 1. Initialize number = 1 2. Test if the number <= 5 3. If yes, print the square...
Continue reading »
February 27, 2016 evirtualguru_ajaygourOnline Computer EducationNo Comment
Repetition Structures In real life you often do something repeatedly, for example, consider a task such as reading a book, first you open the book, and then repeatedly – read a page; flip the page – until you get to the end of the book, then close the book. Similarly, when writing programs, you might need to perform the same sequence of statements repeatedly until some condition is met. The ability...
Continue reading »
February 27, 2016 evirtualguru_ajaygourOnline Computer EducationNo Comment
The switch Statement The switch statement is used to execute a block of code matching one value out of many possible values. The structure of the Java switch statement is as below: switch (expression) { case constant_1 : statements; break; case constant_2 : statements; break; … … default : statements; break; } Within the switch statement, as you can see, there can be many case statements. The expression is compared with...
Continue reading »
February 27, 2016 evirtualguru_ajaygourOnline Computer EducationNo Comment
The if else Statement The if statement in Java lets us execute a block of code depending upon whether an expression evaluates to true or false. The structure of the Java if statement is as below: if (expression) { statements } The expression given in the round brackets after the if keyword, is a condition – an expression constructed using relational and logical operators. If the condition evaluates to true, the...
Continue reading »
February 27, 2016 evirtualguru_ajaygourOnline Computer EducationNo Comment
Selection Structures In real life, you often select your actions based on whether a condition is true or not. For example, if it is raining outside, you carry an umbrella, otherwise not. Similarly in a program, you may want to execute a part of the program based on the value of an expression. Java provides two statements – the if else statement and the switch statement for executing a block of...
Continue reading »
February 27, 2016 evirtualguru_ajaygourOnline Computer EducationNo Comment
Control Flow Recall, that a program is nothing but a sequence of instructions. Java executes the instructions in sequential order, that is, one after the other. However, sometimes you might want to execute an instruction only if a condition holds true. Or you may want to execute a set of instructions repeatedly until a condition is met. Java provides selection structures for the former and repetition structures for the latter.
Continue reading »
February 27, 2016 evirtualguru_ajaygourOnline Computer EducationNo Comment
Operators Operators are special symbols in a programming language and perform certain specific operations. You have already seen two operator * and /. Table 2 lists the Java Arithmetic operators, Table 3 lists the Relational operators, Table 4 lists the Assignment operators, and Table 5 lists the Logical operators available in Java. Java also provides Bitwise operators which are beyond the scope of this text.
Continue reading »
February 27, 2016 evirtualguru_ajaygourOnline Computer EducationNo Comment