Java/C2/Nested-if/English
Author : TalentSprint
Script Title : Nested-If and Ternary Operator
Keywords : If else conditional statements, Ternary operator, nested-if, video tutorial
Visual Clue | Description |
Slide 1
Welcome |
Welcome to the spoken tutorial on Nested-If and Ternary Operator in java. |
Slide 2
Learning Objectives |
By the end of this tutorial you should be able to:
|
Slide 3
Tools Used
|
For this tutorial we are using:
Ubuntu 11.10, JDK 1.6,and Eclipse 3.7.0 |
Slide 4
Pre-requisites |
To follow this tutorial, you should know,
- the usage of relational and logical operators. - if...else control flow statements.
|
Slide 5
Nested-If statements |
An if statement within another if statement is called a nested-if statement. |
Slide 6
Syntax for Nested-If Statement
if (condition2) { Statement or block 1 } else { Statement or block 2 } } else { Statement or block 3 } |
Let us look at the syntax for writing the Nested-If statement.
In this case, if condition 1 is true, then the program checks for condition 2.
Else, it executes Statement or block 2.
Instead will directly jump to its else statement i.e. block 3. |
Let us try and example to understand that better | |
Point to the code
{ Minimize slides and open Eclise. It should contain the following }
public static void main(String args[]) { } } |
We have the eclipse IDE and the skeleton required for the rest of the code.
We have created a class NesedIfDemo and added the main method to it. |
Type
int n = -5;
|
We shall check if the given number is a even number or an odd number.
int n = minus 5;
Now we shall write the if conditions. |
Type
if (n < 0) { System.out.println(“Negative number”); } |
Type
open curly bracket System.out.println(“Negative number”); close curly bracket
If yes then we will not check for even and odd. |
Then if the number is not a negative, we then check for even and odd. | |
Type after the if statement.
else{ } |
Type
else { }
It means that the number is non negative.
|
inside else, type
if (n % 2 == 0) { System.out.println(“Even number”); } else { System.out.println(“Odd number”); } |
Type
if (n percent 2 equalto equalto 0) { System.out.println(“Even number”); } else { System.out.println(“Odd number”); }
|
Now let us see the code in action. | |
Save and Run | As we can see, we get the output “negative”.
|
Change n = -5 to n = 5 | Change n = -5 to n = 5
|
Point to the output | As we can see, the output is odd as expected. Let us try an even number |
Change n = 5 to n = 10 | Change n = 5 to n = 10.
Save and run. |
Point to the output. | As we can see, the output is “even” as expected. |
Highlight the code | This process of including an if statement inside another, is called nested-if.
|
Remove everything inside the Main method | Now we shall look at the ternary operator.
|
We shall write a program that divides a number by 2.
| |
Let us see how to write such a program. | |
Type
int n, nHalf; |
Type
int n, nHalf;
|
Type
nHalf = n / 2; } else { nHalf = (n + 1) / 2; } |
Type
n = 5;
nHalf = n / 2; } else { nHalf = (n + 1) / 2; }
|
Type
|
Let us add a print statement to see the program in action.
|
Save and Run.
|
Save and Run.
|
Point to the code. | But if we notice, all we are doing is, setting the value of a variable depending on a condition.
|
{Switch to Slides}
Slide 7 Ternary Operator. |
A conditional operator providing results similar to nested-if.
|
Slide 8
Point to the syntax
Result = Expression ? Operand 1 : Operand 2 |
Let us learn about the syntax of Ternary Operator.
|
{Switch to eclipse}
Remove the if-else statement |
First let us remove the if-else statement. |
Type
|
Type
Otherwise, it is n plus 1 by 2. |
Save and Run. | Let us now see it in action.
|
Point to the output. | As we can see, the output is as expected.
|
Slide 9
Summary |
We have come to the end of this tutorial.
In this tutorial we have learnt:
|
Slide 10
Assignment |
Now take an assignment on
Nested-If and Ternary operator. Write java program for the following.
|
Slide 11
About the Spoken Tutorial Project
If you do not have good bandwidth, you can download and watch it |
To know more about the Spoken Tutorial project, watch the video available at the following link. It summarizes the project.If you do not have good bandwidth, you can download and watch it. |
Slide 12
Spoken Tutorial Workshops The Spoken Tutorial Project Team
|
The Spoken Tutorial Project Team. Conducts workshops using spoken tutorials and gives certificates for those who pass an online test. For more details, please write to contact AT spoken HYPHEN tutorial DOT org. |
Slide 13
Acknowledgement
|
Spoken Tutorial Project is a part of the Talk to a Teacher project and is supported by the National Mission on Education through ICT, MHRD, Government of India. More information on this Mission is available at spoken HYPHEN tutorial DOT org SLASH NMEICT HYPHEN Intro |
Slide 14
About the contributor
Thanks for joining |
This tutorial has been contributed by TalentSprint.
Thanks for joining.
|