If Statement

Ace your homework & exams now with Quizwiz!

Description - If statement within block statement in 'then' part:

- Allowing the sequence of statements to be executed rather than just one.

Description: Nested if ... else statement

- The inner if statement will be executed firstly. - The else part always related to the closest if keyword

Description - if (Single-Selection) Statement:

- if keyword: started with the statement and followed by boolean expression. - Boolean expression, a == b,which must be in parantheses ( ) - If the value of boolean expression 'true', the then part is executed - If the value of boolean expression 'false', the then part is ignred and the statement following the if statement executed.

Description: if ... else (Double-selection) Statement

- if the boolean expression evaluated to true the statemet forming 'then' part is excuted. - Otherwise 'else' part will be excuted. - One and only one statement of 'then' and 'else' parts will be excuted.

Introduction

This Topic will Discuss: The if, if...else, and nested if statements Compound assignment, increment and decrements operators Portability of Java's primitive types

Example: if ... else (Double-selection) Statement

if (a == b) c = 10; // 'then' part else c = 20; // 'else' part h = 1.2 // Nest statement after the if

Example- if (Single-Selection) Statement:

if (a == b) c = 10; // the action statement after 'then' or body part h = 1.2; // next statement after the if. Not related to if.

Example: Nested if ... else statement

if (a == b) { If (c == d) { System.out.println ("c = d"); System.out.println ("a = b"); } else { System.out.println ("c != d"); System.out.println ("a != b"); } }

Example - If statement within block statement in 'then' part:

if (a == b) { c = 10; d = 20; f(); } h = 1.2; // next statement after the if. Not related to if.

Syntax - if ... else (Double-Selection) Statement:

if (boolean_expression) do_Something; else do_Otherthing;

Syntax - Nested if ... else Statement:

if (boolean_expression) do_Something1; else if (boolean_expression2) do_Something2; ..... else do_somthing_N;

Syntax - if (Single-Selection) Statement:

if (boolean_expression) do_something;

Objective:

• To provide binary selection, allowing conditional execution of a statement depending on the value of a boolean expression. • To choose among alternative actions.


Related study sets

Entrepreneurship Final Exam #1 (Ch. 1,3,4,7, 8, 17)

View Set

MNGT 301 || Chapter 3: The Manager's Changing Work Environment and Ethical Responsibilities: Doing the Right Thing

View Set

Patterns for a Purpose Chapters 1-3

View Set