Summary

This document provides an introduction to control structures in Java programming. It covers if-else statements, switch statements, and repetition structures (loops). The material includes examples and syntax for each structure.

Full Transcript

IT2402 CONTROL STRUCTURES A program performs decision-making to determine the sequence in which it will execute the instructions. Control statements are used to control the execution flow of the program. The execution order of the program is based on the supplie...

IT2402 CONTROL STRUCTURES A program performs decision-making to determine the sequence in which it will execute the instructions. Control statements are used to control the execution flow of the program. The execution order of the program is based on the supplied data values and the conditional logic. Java supports the following types of control statements: Selection, Repetition, and Branching. Selection (Agarwal & Bansal, 2023) Selection statements are decision-making statements that include if, if-else, and switch statements. if statement The if decision construct is followed by a logical expression wherein data is compared, and a decision is made depending on the comparison result. This statement has the following syntax: if (boolean-expression){ statements; } For example: int a=16; if (a%2==0){ System.out.println("This is an even number"); } The example lets the program decide whether the output matches the condition of the Boolean expression. if-else statement It is an extension of the if statement that provides an alternative action when the if statement results in a false outcome. The else block is executed whenever the if statement is false. A block is the set of statements between two curly braces ({, }). This statement has the following syntax: if (boolean-exprsn){ statements; } else{ statements; } For example, a code determines whether aNum1 or aNum2 holds a higher value. if (aNum1 > aNum2){ aMax = aNum1; } else { aMax = aNum2; } 06 Handout 1 *Property of STI  [email protected] Page 1 of 8 IT2402 If the value of aNum1 is greater than that of aNum2, the statement in the if block is executed; otherwise, the statement in the else block is executed. Extending the if statement example with an else block in case the condition of the if statement is not met or if it is false. int a=16; if (a%2==0){ System.out.println("This is an even number"); } else { System.out.println("It is an odd number"); } switch statement It is considered an easier implementation of the if-else statement. It is used whenever there are multiple values possible for a variable. This statement successfully lists the value of a variable or an expression against a list of byte, short, char, or int primitive data types only. The statements linked with that case constant are executed when a match is found. The syntax includes: 1 switch (variable-name) 2 { 3 case exprsn1: 4 statements; 5 break; 6 case exprsn2: 7 statement; 8 break; 9 case exprsn3: 10 statement; 11 break; 12 default: 13 statement; 14 } The switch keyword is followed by the variable in parentheses, such as in line 1. Each case keyword found in lines 3, 6, and 9 is followed by a case constant (exprsn1/2/3). The data type of the case constant must match that of the switch variable. Before entering the switch construct or statement, a value should be assigned to the switch variable. The break statements in lines 5, 8, and 11 are used to cause the program flow to exit from the body of the switch control. Control goes to the first statement following the end of the switch construct. If unused, the control passes to the next case statement and the remaining statements are also executed. 06 Handout 1 *Property of STI  [email protected] Page 2 of 8 IT2402 The statement associated with the default keyword such as in line 13 and line 12, respectively, is executed if the value of the switch variable does not match any of the case constants. Take note of the following: The default label must always be the last option in the switch construct if used The case expressions can be of either numeric or character data type The case constants in the switch statement cannot have the same value For example, the expression “day” in switch statements evaluates to “5,” matching with the case labeled “5.” The code in case 5 is executed and results in the output “Friday” on the screen. int day=5; switch (day) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; case 4: System.out.println("Thursday"); break; case 5: System.out.println("Friday"); break; case 6: System.out.println("Saturday"); break; case 7: System.out.println("Sunday"); break; default: System.out.println("Invalid entry"); break; } Repetition (Agarwal & Bansal, 2023) Repetition statements are looping statements that include while, do-while, and for statements. A loop causes a section of a program to be repeated based on the specified number of times. This repetition continues while the condition set for it remains true. Otherwise, the loop ends and the control is passed to the statement following the loop construct. while loop statement It is a looping construct that continues until the evaluating condition becomes false. The evaluating condition must be a logical expression and must return a true or a false value. The variable checked in the Boolean expression is called the loop control variable. 06 Handout 1 *Property of STI  [email protected] Page 3 of 8 IT2402 The syntax goes as follows: while (boolean-exprsn) { statement; } The if statement and the break and continue keywords can be used within the while block to exit the loop. For example, a code that generates the Fibonacci series, wherein each number is the sum of its two preceding numbers. The series starts with 1. Output: public class FibonacciDemo { public static void main (String a[]) { int i1 = 1, i2 = 1; System.out.println(i1); while (i1

Use Quizgecko on...
Browser
Browser