Chapter 3 Decision making Branching ie cmd X
Chapter 3 Decision making & Branching ie cmd X Introduction Decision making with If statement Simple if statement The If. . Else statement Nesting of if…else statement The switch statement
Introduction. When a program breaks a sequential flow and jumps to another part of the code, it is called branching. ie cmd X When the branching is based on the particular condition, it is known as conditional branching. Java language processes such decision making capabilities and supports the following statements known as control or decision making statements. 1. if statement. 2. switch statements. 3. Conditional operator statement.
Decision making with if statement is used to control the flow of execution of statements. It takes the following form ie cmd if (test expression) It allows the computer to evaluate the expression first and then, depending on whether the value of the expression (relation or condition) is ‘true’ or ‘false’. Entry X Test Expression ? True False
Simple if statement The general form of the if statement is: ie cmd if (test expression) { Statement Block; Entry Test expression True ? Statement - block False } X Statement x; Statement - X Flow chart of simple if control
if. . else statement The general form of the if statement is: ie cmd X if (test expression) { True block statement; } else { False block statement; } Statement X; Entry False Test expression True ? False block statement True block statement Statement - X Flow chart of if - else control
Nesting of if. . else statement The general form of the if statement is: ie cmd X Entry True Test False if (test expression) Expression 1 { ? if (test expression) Test False True Expression 2 True block statement; ? False block statement else False block statement; Statement - X True block statement } else { if (test expression) Flow chart of nested if - else control True block statement; else False block statement; } Statement X; Nestedifelse. java
The switch statement Java has multiway decision making statement known as a switch. The switch tests the value of the given variable against a list of case value and when it found the associated statement will be executes. The general form of switch statement as follows: ie cmd X switch (expression) { case Value-1: block 1; break; case Value-2: block 2; break; ………………. default: block: break; } Switch. Demo. java
- Slides: 7