Control Flow, Loops & Branching PDF
Document Details
Uploaded by AdventurousNobility7731
Jasiya Fairiz Raisa
Tags
Summary
This document provides lecture notes on control flow, loops, and branching statements in Java. It covers topics like if statements, switch statements, for loops, while loops, do-while loops, break statements, continue statements, and return statements. The document also provides example code.
Full Transcript
Control Flow, Loops & Branching Lecturer Jasiya Fairiz Raisa Content Selection Statements If-else, if-else-if, else, Nested if-else Switch-case Loops Statements For While Do-while Branching statements Break and Continue Return Statement Introduction...
Control Flow, Loops & Branching Lecturer Jasiya Fairiz Raisa Content Selection Statements If-else, if-else-if, else, Nested if-else Switch-case Loops Statements For While Do-while Branching statements Break and Continue Return Statement Introduction to Control Flow Staements The statements in the code are executed according to the order in which they appear. Java provides statements that can be used to control the flow of Java code. Such statements are called control flow statements. Types: Decision Making statements: if, if-else, switch Loop statements: for, while, do-while Branching statements: break, continue, return Decision Making Statements decision-making statements decide which statement to execute and when. Decision-making statements evaluate the Boolean expression and control the program flow depending upon the result of the condition provided. If statement: If Statement The condition of the If statement gives a Boolean value, either true or false. Types: Simple if statement: It evaluates a Boolean expression and enables the program to enter a block of code if the expression evaluates to true. if-else statement: extension to the if-statement, which uses another block of code, i.e., else block. The else block is executed if the condition of the if-block is evaluated as false. if-else-if ladder: The if-else-if statement contains the if-statement followed by multiple else-if statements. Nested if-statement: In nested if-statements, the if statement can contain a if or if-else statement inside another if or else-if statement. Switch Statement In Java, Switch statements are similar to if-else-if statements. The switch statement contains multiple blocks of code called cases and a single case is executed based on the variable which is being switched. Remember: The case variables can be int, short, byte, char, or enumeration. String type is also supported since version 7 of Java Cases cannot be duplicate Default statement is executed when any of the case doesn't match the value of expression. It is optional. Break statement terminates the switch block when the condition is satisfied. It is optional, if not used, next case is executed. While using switch statements, we must notice that the case expression will be of the same type as the variable. However, it will also be a constant value. Switch Statement public class Student implements Cloneable { public static void main(String[] args) { int num = 2; switch (num){ case 0: System.out.println("number is 0"); break; case 1: System.out.println("number is 1"); break; default: System.out.println(num); } } } Loop Statements Loop statements are used to execute the set of instructions in a repeated order. The execution of the set of instructions depends upon a particular condition. Types: For loop: initialize the loop variable, check the condition, and increment/decrement in 1 line of code. While loop: this loop is used we don't know the number of iterations in advance. Do-while loop: When the number of iteration is not known and we have to execute the loop at least once, we can use do-while loop. This loop checks the condition at the end of the loop after executing the loop statements. Also called “exit-controlled loop”, since the condition is not checked in advance. Loop Statements for(int j = 1; j