From the course: Oracle Java Certification: 2. Operators and Decision Statements

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

If-then-else statements

If-then-else statements - Java Tutorial

From the course: Oracle Java Certification: 2. Operators and Decision Statements

Start my 1-month free trial

If-then-else statements

- [Instructor] One important aspect of coding is defining logic. Normally a block of code gets executed sequentially, one statement after the other. This is the sequential flow of control in a program. To change this flow of control we can use decision constructs. In fact, any Turing complete programming language should support a conditional execution of code blocks. A Turing complete language is a language that can be used to describe any task that is computable in any other language. On the left, we see the general structure of and if-then-else statement. Note that the then block cannot be omitted. As shown to the right, the then block can be empty but the parentheses must be there for it to compile. Let's start with a simple example that uses if-then-else structure. This code snippet uses an if-then-else statement to check whether a score is a passing score or not. The if condition is evaluated first, and the result…

Contents