Difference between revisions of "Java/C2/if-else/English"
(Created page with ''''Author : '''TalentSprint '''ScriptTitle: '''If Else Construct '''Keywords: '''if, else, conditional statements, if, if else, if else if, Eclipse, video tutorial {| style=…') |
|||
Line 35: | Line 35: | ||
| style="border:0.035cm solid #000000;padding:0.097cm;"| For this tutorial we are using: | | style="border:0.035cm solid #000000;padding:0.097cm;"| For this tutorial we are using: | ||
− | Ubuntu 11.10 | + | Ubuntu v 11.10 |
− | JDK 1.6 | + | JDK 1.6 and |
− | Eclipse 3.7 | + | Eclipse 3.7.0 |
|- | |- | ||
Line 95: | Line 95: | ||
− | + | ||
|- | |- | ||
Line 115: | Line 115: | ||
// program continues here''' ''' | // program continues here''' ''' | ||
− | | style="border:0.035cm solid #000000;padding:0.097cm;"| | + | | style="border:0.035cm solid #000000;padding:0.097cm;"| |
Line 121: | Line 121: | ||
− | If the condition is '''false, '''the block is skipped and not executed. | + | If the condition is '''false, '''the block is skipped and it is not executed. |
Line 157: | Line 157: | ||
Highlight '''{}''' | Highlight '''{}''' | ||
− | | style="border:0.035cm solid #000000;padding:0.097cm;"| Let us look at an example to understand how the '''If Statement''' can be used. | + | | style="border:0.035cm solid #000000;padding:0.097cm;"| now Let us look at an example to understand how the '''If Statement''' can be used. |
Line 171: | Line 171: | ||
Now, inside the main method let us declare a variable '''‘age’''' of type '''int.''' | Now, inside the main method let us declare a variable '''‘age’''' of type '''int.''' | ||
− | So type '''''int age '''is equal to'' 20. | + | So type '''''int age '''is equal to'' 20 semi-colom. |
Line 177: | Line 177: | ||
− | '''''if '''within | + | '''''if '''within bracket '''age<nowiki> < 21</nowiki>''' open curly brackets.'' |
Line 189: | Line 189: | ||
− | '''System '''''dot '''''out '''''dot '''''println '''within brackets and double quotes''' The person is Minor.''' | + | '''System '''''dot '''''out '''''dot '''''println '''within brackets and double quotes''' The person is Minor semi-colon.''' |
Line 195: | Line 195: | ||
− | + | So '''save''' and''' run '''the file. | |
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.176cm;"| Highlight the output. | | style="border:0.035cm solid #000000;padding:0.176cm;"| Highlight the output. | ||
− | | style="border:0.035cm solid #000000;padding:0.176cm;"| We get the output as follows. | + | | style="border:0.035cm solid #000000;padding:0.176cm;"| We get the output as follows. The person is minor |
− | |||
+ | In this case, the person's age is''' 20''', which is less than '''21. ''' | ||
So, we got the output as “'''The person is minor'''”. | So, we got the output as “'''The person is minor'''”. | ||
Line 253: | Line 253: | ||
− | If the condition is True, | + | If the condition is True, the statement or block of code is executed.. |
− | Else it executes another statement or | + | Else it executes another statement or block of code. |
|- | |- | ||
Line 305: | Line 305: | ||
Highlight as you explain. | Highlight as you explain. | ||
| style="border:0.035cm solid #000000;padding:0.097cm;"| We will now see how the '''If…else statement '''can be used in a program. | | style="border:0.035cm solid #000000;padding:0.097cm;"| We will now see how the '''If…else statement '''can be used in a program. | ||
+ | |||
+ | So let us switch to the eclipse. | ||
+ | we will write a program to identify whether the person is '''Minor or Major'''. | ||
− | |||
+ | So inside the '''Main''' method type; int age is equal to 25 | ||
− | |||
+ | then '''if '''''within brackets'' '''age''' ''greater than'' '''21''', | ||
− | ''' | + | ''within curly brackets'' type '''System '''''dot''''' out '''''dot''''' println '''''within brackets '''''The person is Major.''' |
− | |||
− | + | Then type,next line | |
− | Then type | + | |
'''else '''''within curly brackets'' type | '''else '''''within curly brackets'' type | ||
− | '''System''' ''dot '''''out''' ''dot'' '''println '''''within brackets'' '''The person is Minor.''' | + | '''System''' ''dot '''''out''' ''dot'' '''println '''''within brackets''in double quotes '''The person is Minor semi-colon.''' |
Line 338: | Line 339: | ||
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.176cm;"| Highlight the output and explain. | | style="border:0.035cm solid #000000;padding:0.176cm;"| Highlight the output and explain. | ||
− | | style="border:0.035cm solid #000000;padding:0.176cm;"| We get the output as | + | | style="border:0.035cm solid #000000;padding:0.176cm;"| We get the output as the person is major |
− | + | ||
Here, the person's '''age''' is '''25,''' which is greater than '''21'''. | Here, the person's '''age''' is '''25,''' which is greater than '''21'''. | ||
Line 372: | Line 372: | ||
− | It is also called as | + | It is also called as ''' branching '''or '''decision making statement.''' |
Line 396: | Line 396: | ||
<nowiki><</nowiki>Statement_or_block 3> | <nowiki><</nowiki>Statement_or_block 3> | ||
− | | style="border:0.035cm solid #000000;padding:0.097cm;"| Let us look at the syntax for writing the '''If…Else If '''statement. | + | | style="border:0.035cm solid #000000;padding:0.097cm;"|Now Let us look at the syntax for writing the '''If…Else If '''statement. |
Line 402: | Line 402: | ||
− | If''' condition 1 '''is true, it executes the '''statement | + | If''' condition 1 '''is true, it executes the '''statement '''or '''block code 1'''. |
Line 408: | Line 408: | ||
− | If '''condition 2''' is true, it executes '''statement | + | If '''condition 2''' is true, it executes '''statement '''or '''block code 2'''. |
Line 489: | Line 489: | ||
− | + | So inside the''' Main''' method, type '''int''' ''space''''' testScore''' ''equal to''''' 70''' ''semicolon.'' | |
Line 495: | Line 495: | ||
− | + | Next line type''' if''' ''within brackets'' '''testScore''' ''is less than'' '''35, '''''within curly brackets '''''System '''dot''' out '''''dot println within brackets and double quotes''''' C grade '''''semicolon'''''.''' | |
Line 504: | Line 504: | ||
− | Then next line type '''if'' '''within brackets''''' testScore '''''greater than or equal to'' '''35 '''and '''testScore '''''less than or equal to '''''60.''' | + | Then next line type '''if'' '''within brackets''''' testScore '''''greater than or equal to'' '''35 '''and '''testScore '''''less than or equal to '''''60.'''Put the soul condition within brackets open curly brackets press enter. |
Here, the program will check for the second condition in the '''Else If''' section. | Here, the program will check for the second condition in the '''Else If''' section. | ||
− | + | ||
+ | System dot println b grade semi-colomn | ||
If the '''testScore''' is between '''35 '''and '''60''' then the program displays "'''B Grade'''". | If the '''testScore''' is between '''35 '''and '''60''' then the program displays "'''B Grade'''". |
Revision as of 19:52, 8 May 2013
Author : TalentSprint
ScriptTitle: If Else Construct
Keywords: if, else, conditional statements, if, if else, if else if, Eclipse, video tutorial
Visual cue | Narration |
Slide 1
Welcome |
Welcome to the spoken tutorial on If else constructs in java. |
Slide 2
Learning Objectives |
In this tutorial we will learn:
|
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 have knowledge of using
If not, for relevant tutorials please visit our website which is as shown. |
Slide 5
Conditional Statements |
You may have to perform different actions for different decisions in your code.
|
Slide 6
|
In Java we have the following conditional statements:
In this tutorial, we will learn about If, If...Else and If...Else If statements in detail. |
Slide 7
|
If statement is used to execute a block of statements based on a condition.
|
Slide 8
{ code_block; } // end of if construct // program continues here |
|
Example of If statement
{ public static void main(String args[]) { int age=20; if(age<21) { System.out.println("The person is Minor"); } }
|
now Let us look at an example to understand how the If Statement can be used.
So type int age is equal to 20 semi-colom.
So save and run the file. |
Highlight the output. | We get the output as follows. The person is minor
In this case, the person's age is 20, which is less than 21. So, we got the output as “The person is minor”.
|
Slide 9
|
Now, we will learn about if...else statement.
|
Slide 10
<statement_or_block> else <statement_or_block> // end of if construct // program continues here |
Let us look at the syntax for writing the If…Else statement.
|
Example of If…Else statement
{ int age=25;
public static void main(String args[]) { if(age>21) { System.out.println("The person is Major"); } else { System.out.println("The person is Minor"); } } }
|
We will now see how the If…else statement can be used in a program.
So let us switch to the eclipse. we will write a program to identify whether the person is Minor or Major.
then if within brackets age greater than 21, within curly brackets type System dot out dot println within brackets The person is Major.
else within curly brackets type System dot out dot println within bracketsin double quotes The person is Minor semi-colon.
|
Highlight the output and explain. | We get the output as the person is major
Here, the person's age is 25, which is greater than 21.
|
Slide 11
(Restore the slide)
|
If…Else If statement is used to execute various set of statements.
|
Slide 12
<Statement_or_block 1>
<Statement of block 2> else <Statement_or_block 3> |
Now Let us look at the syntax for writing the If…Else If statement.
|
Example of If…Else If statement
{ public static void main(String args[]) { int testScore=70;
{ System.out.println("C grade "); } else if((testScore>=35)&&(testScore<=60)) { System.out.println("B grade"); } else { System.out.println("A grade"); } }
|
We will see how the If…Else If statement can be used in a program.
Next line type if within brackets testScore is less than 35, within curly brackets System dot out dot println within brackets and double quotes C grade semicolon.
System dot println b grade semi-colomn If the testScore is between 35 and 60 then the program displays "B Grade".
|
Highlight the output and explain | We get the output as follows.
|
Change testScore=55; | Let us change the testScore to 55.
|
Highlight the output as you explain. | We get the output as follows.
|
else if((testScore>=60)&&(testScore<=75))
{ System.out.println("O grade"); } Highlight as you explain. |
We can also increase the number of conditions.
|
Highlight the output. | We get the output as follows.
|
Slide 13
|
While coding conditional structures:
|
Slide 14
Summary |
We have come to the end of this tutorial.
In this tutorial, we:
|
Slide 15
Assignment
|
Now take an assignment on writing java programs using conditional statements: if, if...else and if...else if statements.
Hint : use if...else statement.
Hint : use if...else if statement.
|
Slide 16
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,
It summarizes the Spoken Tutorial project.
|
Slide 17
Spoken Tutorial Workshops The Spoken Tutorial Project Team
For more details, please write to contact@spoken-tutorial.org |
The Spoken Tutorial Project Team
|
Slide 18
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 19
About the contributor
Thanks for joining
|
This tutorial has been contributed by TalentSprint.
Thanks for joining
|