Basic Building Blocks PDF

Summary

This document provides an overview of basic building blocks in programming, specifically focusing on control statements in Java. The document explains selection statements (if-else), iteration (loops), and jump statements. It also covers nested if-statements and switch statements, and includes examples.

Full Transcript

Basic Building Blocks UNIT - II Control Statements Control statements are used to alter the flow of execution according to the changes in program state. Control Statements Selection...

Basic Building Blocks UNIT - II Control Statements Control statements are used to alter the flow of execution according to the changes in program state. Control Statements Selection Iteration Jump Control Statements Selection statements allow your program to choose different paths of execution based upon the outcome of an expression or the state of a variable. Iteration statements enable program execution to repeat one or more statements (that is, iteration statements form loops). Jump statements allow your program to execute in a nonlinear fashion. All of Java’s control statements are examined here. Selection Statement (If Statement) The if statement is Java’s conditional branch statement (selection statement). It can be used to route program execution through two different paths. Here is the general form of the if statement: if (condition) statement1; else statement2; Here, each statement may be a single statement or a compound statement enclosed in curly braces (that is, a block). The condition is any expression that returns a boolean value. The else clause is optional. If the condition is true, then statement1 is executed. Otherwise, statement2 (if it exists) is executed. Nested If Statement A nested if is an if statement that is the target of another if or else. In nested ifs, an else statement always refers to the nearest if statement that is within the same block as the else and that is not already associated with an else statement. if (a < 10) { if (a < 10) { if (a < 10) { if (j < 20) a = b; if (j < 20) if (j < 20) a = b; if (k > 100) c = d; if (k > 100) c = d; if (k > 100) c = d; Dangling Else / else a = c; else a = c; Else without If else a = c; error } else a=b else d = c; else a = d; } } else a = d; else a = d; In yellow box, else which is inside the block is associated with nearest if (k>100) and the else which is outside the block is associated with outside nearest if (a < 10). In green box, first else inside the block is associated with if (k>100) and second else is associated with if (j100) and second else is without an if statement. The If-else-If Ladder A common programming construct that is based upon a sequence of nested ifs. Its general form is: if(condition1) statement; else if(condition2) statement; else if(condition3) statement;... else statement; Whenever a condition associated with an if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement (acts as default condition) will be executed. Switch Statement The general form of switch statement is: switch (expression) { case value1: // statement sequence break; case value2: // statement sequence break;... case valueN: // statement sequence break; default: // default statement sequence} The expression must be of type byte, short, int, or char; each of the values specified in the case statements must be of a type compatible with the expression. Switch statement The value of the expression is compared with each of the literal values in the case statements. If a match is found, the code sequence following that case statement is executed. If none of the constants matches the value of the expression, then the default statement is executed. However, the default statement is optional. If no case matches and no default is present, then no further action is taken. The break statement is used inside the switch to terminate a statement sequence. When a break statement is encountered, execution branches to the first line of code that follows the entire switch statement. A simple example of the switch statement class SampleSwitch { public static void main(String args[]) { for(int i=0; i

Use Quizgecko on...
Browser
Browser