From Script | Spoken-Tutorial
Revision as of 12:46, 13 August 2013 by Sneha (Talk | contribs)

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.
  • 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 tutorial 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 locate the syntax for writing the Nested-If statement.
00:53 In this case, if condition 1 is true, then the program checks for condition 2.
00:59 Condition 2 is given using another If statement.
01:03 If condition 2 is true, then the program executes Statement or block 1.
01:09 Else, it executes Statement or block 2.

01:13 If condition 1 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 and 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 a 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
02:02 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 {

} 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
02:53 if (n modules 2 double equal to 0) { Press enter
03:03 System.out.println(“Even number”);


else { press enter


System.out.println(“Odd number”);


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 NowSave 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.
05:01 See inside the main method Type int n, nHalf ;
05:08 We will store the number in n and the half number in nHalf
05:13 NExt line Type n = 5;
05:18 NExt line Type if (n % 2 == 0) { Press enter
05:28 Type nHalf = n / 2;


else {

nHalf = (n + 1) / 2;


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(nHalf);
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 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 = n % 2 == 0 ? n / 2 : (n + 1) / 2 semi-colon

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
08:15 * Usage of Nested-If Statements and Ternary Operator in a Java program
08:22 Now take an assignment on
08 :23 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 for 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