Difference between revisions of "Java/C2/if-else/English"
Line 24: | Line 24: | ||
* About conditional statements | * About conditional statements | ||
− | * | + | * types of conditional statements and |
* How to use conditional statements in Java programs | * How to use conditional statements in Java programs | ||
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;"| ''Syntax for If statement ''' |
+ | |||
Line 160: | Line 161: | ||
− | Let us switch to '''eclipse.''' | + | So Let us switch to '''eclipse.''' |
Line 177: | Line 178: | ||
− | '''''if '''within bracket '''age<nowiki> < 21</nowiki>''' open curly brackets.'' | + | '''''if '''within bracket '''age<nowiki> < 21</nowiki>''' open curly brackets.'' Press enter |
Line 192: | Line 193: | ||
− | Here, if '''age '''is less than '''21,''' | + | Here, if '''age '''is less than '''21,''' than “'''The person is minor'''” will be displayed. |
Line 326: | Line 327: | ||
− | Here, if the '''age''' ''is less than'' '''21''', | + | Here, if the '''age''' ''is less than'' '''21''', “' ''The person is Minor'''” will be displayed. |
Line 332: | Line 333: | ||
− | + | Let us'''save it '''and''' run''' the program. | |
Line 344: | Line 345: | ||
− | Therefore the program | + | Therefore the program display the output as “'''The person is Major'''”. |
Line 399: | Line 400: | ||
− | + | If statement initially checks for '''condition 1.''' | |
Line 506: | Line 507: | ||
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. | 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. | ||
− | + | Type '''System dot println''' within brackets ''' B grade''' semi-colomn | |
+ | |||
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. | ||
− | |||
− | |||
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'''". | ||
Line 527: | Line 527: | ||
|- | |- | ||
| 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 '''A Grade''' |
− | + | ||
In this program, the student’s '''testScore''' is '''70''' . | In this program, the student’s '''testScore''' is '''70''' . | ||
Line 537: | Line 536: | ||
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.176cm;"| Change testScore<nowiki>=55;</nowiki> | | style="border:0.035cm solid #000000;padding:0.176cm;"| Change testScore<nowiki>=55;</nowiki> | ||
− | | style="border:0.035cm solid #000000;padding:0.176cm;"| Let us change the''' testScore''' to''' 55.''' | + | | style="border:0.035cm solid #000000;padding:0.176cm;"|Now Let us change the''' testScore''' to''' 55.''' |
Line 547: | Line 546: | ||
|- | |- | ||
| style="border:0.035cm solid #000000;padding:0.176cm;"| Highlight the output as you explain. | | style="border:0.035cm solid #000000;padding:0.176cm;"| Highlight the output as you explain. | ||
− | | style="border:0.035cm solid #000000;padding:0.176cm;"| | + | | style="border:0.035cm solid #000000;padding:0.176cm;"| |
Line 571: | Line 570: | ||
− | So type here, '''if''' ''within brackets''''' testScore''' ''greater than or equal to'' '''60 '''and''' testScore''' ''less than or equal to'' ''' | + | So type here, |
+ | Else, Next line | ||
+ | '''if''' ''within brackets''''' testScore''' ''greater than or equal to'' '''60 '''and''' testScore''' ''less than or equal to'' '''70.''' | ||
− | Then within curly brackets | + | Then within curly brackets press enter '''System '''''dot '''''out''' ''dot '''''println''' ''within brackets and double quotes''''' O grade''' ''semicolon''. |
− | Here if the '''testScore''' is between '''60''' and '''75''' | + | Here if the '''testScore''' is between '''60''' and '''75''' the program will display "'''O Grade'''". |
Line 599: | Line 600: | ||
− | The program will display “'''A grade'''” for the '''testScore''' greater than ''' | + | The program will display “'''A grade'''” for the '''testScore''' greater than '''70.''' |
|- | |- | ||
Line 612: | Line 613: | ||
* But don’t add semi-colons after the condition. | * But don’t add semi-colons after the condition. | ||
− | * Add the block of code within curly | + | * Add the block of code within curly brackets |
* Curly braces are optional if the block contains a single statement. | * Curly braces are optional if the block contains a single statement. | ||
Latest revision as of 10:58, 9 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 |
Syntax for If statement '
In the if statement, if the condition is true, the block is executed.
|
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.
Type System dot println within brackets B grade semi-colomn Here, the program will check for the second condition in the Else If section. If the testScore is between 35 and 60 then the program displays "B Grade".
|
Highlight the output and explain | We get the output as A Grade
In this program, the student’s testScore is 70 .
|
Change testScore=55; | Now Let us change the testScore to 55.
|
Highlight the output as you explain. |
|
else if((testScore>=60)&&(testScore<=75))
{ System.out.println("O grade"); } Highlight as you explain. |
We can also increase the number of conditions.
if within brackets testScore greater than or equal to 60 and testScore less than or equal to 70.
|
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
|