Friday 14 September 2018

Control Statements (Part 2)




In this post, will talk about all Control Statements in details and Will see the syntax of it.
In next couple of part will see some of the examples that will definitely help you to understand better,
as well as I will going to give you some basics programming question to solve it.



Syntax - Syntax is the set of rules that defines the combinations of symbols that are considered to be a correctly structured document. (or in short, it is a grammar of programming)


Expression  - Expression is a valid and logical combination of the operator ( athematic and relational ) and number.


Boolean Expression - Boolean Expression is an expression which always gives its result either "true" or "false".




# if Statement


+ If the Boolean Expression / Condition gives true, then whatever written inside if block will execute.
+ If the Boolean Expression / Condition gives false, then whatever written inside if block will never execute.
+ Note - In the place of Boolean Expression / Condition, you can not use 0 or  1 (java is unlike C or C++).



# if else Statement 

+ If the Boolean Expression / Condition gives true, then whatever written in the place of "do this work 1" will execute.
+ If the Boolean Expression / Condition gives false, then whatever written in the place of "do this work 2" will never execute.
Note - In the place of Boolean Expression / Condition, you can not use 0 or  1 (java is unlike C or C++).



# switch Statement


+ The switch statement is also called Integer Based Decision Maker ( till Java 1.4).
+  X can be constant (final) and in the range.
+ in the place of X you can use Number (byte, short, integer), character, wrapper classes (will See later) and string (Sequence of characters).  
+ default is also a case if no case matches then default case will be executed. 
+*  in switch "break" and "continue", these two keywords will be used frequently. (will discuss in next post).



# while loop
+ it is also called Entry Controlled loop because it will check the condition before execution.
+ If the Boolean Expression / Condition gives true, then whatever written inside the block will execute repeatedly.
+ If the Boolean Expression / Condition return false, then whatever written inside the block will never execute and control goes out of the loop.
Note - In the place of Boolean Expression / Condition, you can not use 0 or  1 (java is unlike C or C++).



# do while loop
+ it is also called Exit Controlled loop because condition checks at the time of exit.
+ it will definitely going to be executed at once then checks for the condition.
+ If the Boolean Expression / Condition gives true, then whatever written inside the block will execute again.
+ If the Boolean Expression / Condition return false, then control goes out of the loop.
Note - In the place of Boolean Expression / Condition, you can not use 0 or  1 (java is unlike C or C++).



# for loop


+ it is used for counting purpose
+  it has three steps

  • initialization - counting start from
  • condition - do it, till the condition satisfied
  • step - ascending to descending or descending to ascending
+ above steps are optional 
+ ; (semi-colon) is mandatory.







No comments:

Post a Comment