if statement if-else statement

J.E.D.I 6 Control Structures

6.1 Objectives

In the previous sections, we have given examples of sequential programs, wherein statements are executed one after another in a fixed order. In this section, we will be discussing control structures, which allows us to change the ordering of how the statements in our programs are executed. At the end of the lesson, the student should be able to: • Use decision control structures if, else, switch which allows selection of specific sections of code to be executed • Use repetition control structures while, do-while, for which allow executing specific sections of code a number of times • Use branching statements break, continue, return which allows redirection of program flow

6.2 Decision Control Structures

Decision control structures are Java statements that allows us to select and execute specific blocks of code while skipping other sections.

6.2.1 if statement

The if-statement specifies that a statement or block of code will be executed if and only if a certain boolean statement is true. The if-statement has the form, if boolean_expression statement; or if boolean_expression { statement1; statement2; . . . } where, boolean_expression is either a boolean expression or boolean variable. Introduction to Programming I 95 J.E.D.I For example, given the code snippet, int grade = 68; if grade 60 System.out.printlnCongratulations; or int grade = 68; if grade 60 { System.out.printlnCongratulations; System.out.printlnYou passed; } Coding Guidelines: 1. The boolean_expression part of a statement should evaluate to a boolean value. That means that the execution of the condition should either result to a value of true or a false. 2. Indent the statements inside the if-block.For example, if boolean_expression { statement1; statement2; } Introduction to Programming I 96 Figure 6.1: Flowchart of If-Statement J.E.D.I

6.2.2 if-else statement

The if-else statement is used when we want to execute a certain statement if a condition is true, and a different statement if the condition is false. The if-else statement has the form, if boolean_expression statement; else statement; or can also be written as, if boolean_expression { statement1; statement2; . . . } else{ statement1; statement2; . . . } For example, given the code snippet, int grade = 68; if grade 60 System.out.printlnCongratulations; else System.out.printlnSorry you failed; or int grade = 68; if grade 60 { System.out.printlnCongratulations; System.out.printlnYou passed; } else{ System.out.printlnSorry you failed; } Introduction to Programming I 97 J.E.D.I Coding Guidelines: 1. To avoid confusion, always place the statement or statements of an if or if-else block inside brackets {}. 2. You can have nested if-else blocks. This means that you can have other if-else blocks inside another if-else block.For example, if boolean_expression { if boolean_expression { . . . } } else{ . . . } Introduction to Programming I 98 Figure 6.2: Flowchart of If-Else Statement J.E.D.I

6.2.3 if-else-if statement