Difference between revisions of "Java/C2/Nested-if/English"
PoojaMoolya (Talk | contribs) |
|||
Line 8: | Line 8: | ||
{| style="border-spacing:0;" | {| style="border-spacing:0;" | ||
− | | style="border:0.035cm solid #000000;padding:0.097cm;"| '''Visual | + | | style="border:0.035cm solid #000000;padding:0.097cm;"| '''Visual Cue''' |
| style="border:0.035cm solid #000000;padding:0.097cm;"| '''Description''' | | style="border:0.035cm solid #000000;padding:0.097cm;"| '''Description''' | ||
Latest revision as of 11:57, 17 June 2014
Author : TalentSprint
Script Title : Nested-If and Ternary Operator
Keywords : If else conditional statements, Ternary operator, nested-if, video tutorial
Visual Cue | 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 v 11.10, JDK 1.6,and Eclipse 3.7.0 |
Slide 4
Pre-requisites |
To follow this tutorial, you should know,
- about 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 } |
Now let us see 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 it will directly jump to its else statement i.e. block 3. |
nOw 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.
we will also handle negative numbers using a nested-if.
int n = minus 5;
Now we shall write the if conditions. |
Type
if (n < 0) { System.out.println(“Negative number”); } |
Next line Type
open curly bracket. Press enter System.out.println Within brackets and double quotes (“Negative number”);
We first see if the number is a negative number. If yes then we will not check for even and odd. |
if the number is not a negative, we then check for even and odd. | |
Type after the if statement.
else{ } |
Next line Type
else { } Press enter
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 modules 2 double equal to 0) { Press enter System.out.println(“Even number”); } else { System.out.println(“Odd number”); }
|
Now let us see the code in action. | |
Save and Run | Save and run the file.As we can see, we get the output as“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 number as expected. Let us try an even number |
Change n = 5 to n = 10 | Change n = 5 to n = 10.
Now Save and run the file |
Point to the output. | As we can see, the output is “even” number 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; } |
NExt line Type
n = 5;
nHalf = n / 2; } else { nHalf = (n + 1) / 2; }
|
Type
|
Now Let us add a print statement to see the program in action.
So Type
|
Save and Run.
|
Save and Run the file
|
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. |
Ternary Operator conditional operator providing results similar to nested-if.
It Takes three operands at a time. |
Slide 8
Point to the syntax
Result = Expression ? Operand 1 : Operand 2 |
Let us learn about the syntax of Ternary Operator.
Now Let us use it in our program. |
{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.
|