From Script | Spoken-Tutorial
Jump to: navigation, search
Time Narration
00:02 Welcome to the spoken tutorial on Nested-If and Ternary Operator in java.
00:07 By the end of this tutorial you should be able to:

Explain Nested-If Statements and Ternary operators and Use them in a Java program.

00:17 For this tutorial we are using:

Ubuntu v 11.10, JDK 1.6, and EclipseIDE 3.7.0

00:27 To follow this tutorial, you should know
00:29 about the usage of relational and logical operators
00:33 and if...else control flow statements.
00:36 If not, for relevant tutorials, please visit our website which is as shown.
00:41 Nested if statements: An If statement within another if statement is called a nested-if statement.
00:49 Now let us look at the syntax for writing the Nested-If statement.
00:53 In this case, if condition1 is true, then the program checks for condition2.
00:59 Condition 2 is given using another if statement.
01:03 If condition2 is true then the program executes Statement or block 1.
01:09 Else, it executes Statement or block 2.
01:13 If condition1 is false then the program will not check condition2.
01:18 Instead it will directly jump to its else statement i.e. block 3.
01:24 Now, let us try an example to understand that better.
01:28 We have the eclipse IDE and the skeleton required for the rest of the code.
01:32 We have created a class NesedIfDemo and added the main method to it.
01:37 We shall check if the given number is an even number or an odd number.
01:42 we will also handle negative numbers using a nested-if.
01:46 So, inside the main method type:
01:49 int n = minus 5;
01:54 We have created a variable 'n' to store the negative number.
01:58 Now, we shall write the if conditions.
02:01 Next line, type: if (n < 0)
02:07 open curly bracket. Press Enter.
02:10 System.out.println within brackets and double quotes Negative number;
02:22 We first see if the number is a negative number.
02:25 If yes, then we will not check for even and odd.
02:29 If the number is not a negative, we check for 'even' and 'odd'.
02:34 Next line, type:else open curly bracket. Press Enter.
02:42 Now, if the execution has come to the else part,
02:45 it means that the number is non-negative.
02:48 So, we check for odd or even inside this else part.
02:52 Type:if within brackets n modulus 2 double equal to 0 open curly bracket press Enter.
03:03 System.out.println within brackets and double quotes Even number;
03:13 Then type the else part. else open curly braket, press Enter.
03:18 Type: System.out.println within brackets and double quotes Odd number semicolon.
03:29 So, we make sure that negative numbers are not considered for 'odd' or 'even' check.
03:34 Now, let us see the code in action.
03:37 Save and run the file. As we can see, we get the output as negative number.
03:43 Now let us try a positive number.
03:46 Change n = -5 to n = 5.
03:53 Now Save and Run the file.
03:57 As we can see, the output is Odd number as expected. Let us try an even number.
04:04 Change n = 5 to n = 10.
04:09 Now Save and run the file.
04:12 As we can see, the output is Even number as expected.
04:17 This process of including an if statement inside another, is called nested-if.
04:22 There is no limit to the amount of nesting.
04:25 But it is a good practice to not go beyond 3 levels of nesting.
04:31 Now we shall look at the ternary operator.
04:33 First, let me clean up the main method.
04:37 We shall write a program that divides a number by 2.
04:40 It is a very trivial program but the issue comes in dividing odd numbers.
04:45 When 7 is divided by 2, we get 3.
04:48 But what if we want the result to be rounded off.
04:50 Which means, when 7 is divided by 2, we get 4 and not 3.
04:56 In simple terms, we need the next number.
04:59 Let us see how to write such a program.See, inside the main method type: int n, nHalf semicolon.
05:08 We will store the number in n and the half number in nHalf
05:13 Next line, type: n = 5 semicolon.
05:18 Next line, type: if within bracket n modulus 2 == 0 open curly brackets. Press Enter.
05:28 Type: nHalf = n / 2 semicolon.
05:34 In the else part, type: within curly brackets nHalf equal to within brackets n+1 the whole divided by 2 semicolon.
05:50 We check if the number is even or odd and do the division accordingly.
05:55 Now Let us add a print statement to see the program in action.
05:59 So, type System.out.println within brackets nHalf semicolon.
06:11 Now Save and Run the file.
06:14 As we can see, our objective is met. We get the output as 3 and not 2.
06:21 But if we notice, all we are doing is setting the value of a variable depending on a condition.
06:27 There is more syntax than logic in our program.
06:31 This is when ternary operator makes code simpler.
06:35 Ternary Operator is a conditional operator providing results similar to nested-if.
06:40 It Provides a short syntax and is denoted by a question mark.
06:45 It takes three operands at a time.
06:48 Let us learn about the syntax of Ternary Operator.
06:53 The Expression is the condition that has to be checked.
06:56 Operand 1 is the value of the variable. Result if the condition is true.
07:03 Operand 2 is the value if the condition is false.
07:09 Now Let us use it in our program.
07:12 First, let us remove the if-else statement.
07:17 Type: nHalf equal to n modulus 2 double equal to 0 question mark n / 2 colon within brackets n + 1 the whole divided by 2 semicolon.
07:41 This statement reads:
07:43 If 'n' is even, 'nHalf' is 'n by 2'. Otherwise it is 'n plus 1 by 2'.
07:50 Let us now see it in action.
07:52 Save and Runthe file. Press Ctrl S and Ctrl F11 keys.
07:59 As we can see, the output is as expected.
08:02 This way, ternary operator reduces clutter in the code and improves readability.
08:09 We have come to the end of this tutorial.
08:11 In this tutorial, we have learnt:
08:13 About Nested-If Statements and Ternary Operator and
08:15 Usage of Nested-If Statements and Ternary Operator in a Java program.
08:22 Now take an assignment on Nested-If and Ternary operator. Write java program for the following.
08:28 Check whether a number is even and also a multiple of 11 using nested-if.
08:34 Identify the largest number among the two given numbers using Ternary operator.
08:40 To know more about the Spoken Tutorial project, watch the video available at the following link.
08:45 It summarizes the spoken-tutorial project. If you do not have good bandwidth, you can download and watch it.
08:52 The Spoken Tutorial Project team:
08:54 Conducts workshops using spoken tutorials and
08:57 gives certificates to those who pass an online test. For more details, please write to contact AT spoken HYPHEN tutorial DOT org.
09:07 Spoken Tutorial Project is a part of the Talk to a Teacher project and
09:11 it is supported by the National mission on Education through ICT, MHRD, Government of India.
09:17 More information on this mission is available at spoken HYPHEN tutorial DOT org SLASH NMEICT HYPHEN Intro.
09:26 This script has been contributed by TalentSprint. This is Arya Ratish from IIT Bombay, signing off. Thanks for joining.

Contributors and Content Editors

Arya Ratish, Devisenan, Krupali, PoojaMoolya, Priyacst, Sandhya.np14, Sneha