Difference between revisions of "Java/C2/if-else/English-timed"
From Script | Spoken-Tutorial
Sandhya.np14 (Talk | contribs) |
Sandhya.np14 (Talk | contribs) |
||
Line 337: | Line 337: | ||
|- | |- | ||
| 07:28 | | 07:28 | ||
− | | If the 'testScore' is less than 35, then the program displays " | + | | If the 'testScore' is less than 35, then the program displays "C Grade". |
|- | |- | ||
Line 349: | Line 349: | ||
|- | |- | ||
| 08:03 | | 08:03 | ||
− | | Type '''System''' dot out dot println''' within brackets ''' B grade''' | + | | Type: '''System''' dot '''out''' dot '''println''' within brackets '''B grade''' semicolon |
|- | |- | ||
Line 357: | Line 357: | ||
|- | |- | ||
| 08:18 | | 08:18 | ||
− | | If the | + | | If the 'testScore' is between 35 and 60 then the program displays "B Grade". |
|- | |- | ||
| 08:24 | | 08:24 | ||
− | | Next line type '''else | + | | Next line, type: '''else''' within brackets type '''System''' dot '''out''' dot '''println''' within brackets and double quotes '''A grade''' semicolon. |
|- | |- | ||
| 08:42 | | 08:42 | ||
− | | So Finally, if both the conditions are '''False''', the program displays | + | | So Finally, if both the conditions are '''False''', the program displays “A Grade". |
|- | |- | ||
Line 373: | Line 373: | ||
|- | |- | ||
| 08:51 | | 08:51 | ||
− | | We get the output as '''A Grade''' | + | | We get the output as '''A Grade'''. |
|- | |- | ||
| 08:55 | | 08:55 | ||
− | | In this program, the student’s | + | | In this program, the student’s 'testScore' is 70. |
|- | |- | ||
| 09:00 | | 09:00 | ||
− | | So the output will be displayed as | + | | So the output will be displayed as “A Grade”. |
|- | |- | ||
| 09:02 | | 09:02 | ||
− | | Now Let us change the''' testScore''' to''' 55.''' | + | | Now Let us change the '''testScore''' to ''' 55.''' |
|- | |- | ||
Line 393: | Line 393: | ||
|- | |- | ||
| 09:10 | | 09:10 | ||
− | | In this case, the output will be displayed as | + | | In this case, the output will be displayed as “B Grade”. |
|- | |- | ||
Line 401: | Line 401: | ||
|- | |- | ||
| 09:19 | | 09:19 | ||
− | | Let us add one more condition after the | + | | Let us add one more condition after the “B grade” output section. |
|- | |- | ||
Line 407: | Line 407: | ||
| So type here, | | So type here, | ||
Else, Next line | Else, Next line | ||
− | '''if | + | '''if''' within brackets '''testScore''' greater than or equal to '''60''' and '''testScore''' less than or equal to '''70.''' |
|- | |- | ||
| 09:47 | | 09:47 | ||
− | | Open curly brackets press | + | | Open curly brackets press '''Enter''' '''System ''' dot '''out''' dot '''println''' within brackets and double quotes '''O grade''' semicolon. |
|- | |- | ||
| 10:01 | | 10:01 | ||
− | | Here if the | + | | Here, if the 'testScore' is between 60 and 70 the program will display "O Grade". |
|- | |- | ||
| 10:07 | | 10:07 | ||
− | | Now, change the '''testScore''' of the student to''' 70'''. | + | | Now, change the '''testScore''' of the student to '''70'''. |
|- | |- | ||
Line 431: | Line 431: | ||
|- | |- | ||
| 10:17 | | 10:17 | ||
− | | The program will display the output as | + | | The program will display the output as “O grade”. |
|- | |- | ||
| 10:20 | | 10:20 | ||
− | | It is not | + | | It is not “A grade” as it displayed before. |
|- | |- | ||
| 10:23 | | 10:23 | ||
− | | The program will display | + | | The program will display “A grade” for the '''testScore''' greater than 70. |
|- | |- | ||
| 10:28 | | 10:28 | ||
− | | While coding conditional structures: | + | | While coding '''conditional structures''': |
|- | |- | ||
Line 451: | Line 451: | ||
|- | |- | ||
| 10:35 | | 10:35 | ||
− | | * But don’t add | + | | * But don’t add semicolons after the condition. |
|- | |- | ||
| 10:40 | | 10:40 | ||
− | | * Add the block of code within curly brackets | + | | * Add the block of code within curly brackets. |
|- | |- | ||
| 10:43 | | 10:43 | ||
Line 470: | Line 470: | ||
|- | |- | ||
| 10:53 | | 10:53 | ||
− | | We have Explained conditional statements | + | | We have Explained conditional statements: |
|- | |- | ||
| 10:56 | | 10:56 | ||
Line 476: | Line 476: | ||
|- | |- | ||
| 10:59 | | 10:59 | ||
− | | * Used conditional statements: '''if, if...else and if...else if''' in Java programs. | + | | * Used conditional statements: '''if, if...else''' and '''if...else if''' in Java programs. |
|- | |- | ||
| 11:04 | | 11:04 | ||
− | |Now take an assignment on writing java programs using conditional statements: '''if, if...else and if...else if '''statements. | + | |Now take an assignment on writing java programs using conditional statements: '''if, if...else''' and '''if...else if '''statements. |
|- | |- | ||
Line 503: | Line 503: | ||
|- | |- | ||
| 11:32 | | 11:32 | ||
− | | | + | | watch the video available at the following link. |
|- | |- | ||
Line 536: | Line 536: | ||
|- | |- | ||
| 12:15 | | 12:15 | ||
− | | This script has been contributed by TalentSprint. This is Arya | + | | This script has been contributed by TalentSprint. This is Arya Ratish from IIT Bombay, signing off |
Thanks for joining | Thanks for joining | ||
|} | |} |
Revision as of 08:12, 25 March 2015
Time | Narration |
00:02 | Welcome to the spoken tutorial on If else constructs in java. |
00:07 | In this tutorial, we will learn: |
00:09 | * About conditional statements |
00:11 | * Types of conditional statements and |
00:13 | * How to use conditional statements in Java programs. |
00:18 | For this tutorial, we are using:
Ubuntu v 11.10 JDK 1.6 and Eclipse 3.7.0 |
00:27 | To follow this tutorial, you should have knowledge of using: |
00:31 | * Arithmetic, Relational and Logical operators in Java. |
00:35 | If not, for relevant tutorials, please visit our website which is as shown. |
00:42 | Conditional statements- You may have to perform different actions for different decisions in your code. |
00:48 | In such cases you can use conditional statements. |
00:52 | A conditional statement helps to control the flow of execution of a program. |
00:57 | In Java, we have the following conditional statements: |
01:01 | * If statement; |
01:02 | * If...Else statement; |
01:03 | * If...Else if statement; |
01:05 | * Nested If statement; |
01:06 | * Switch statement; |
01:08 | In this tutorial, we will learn about If, If...Else and If...Else If statements in detail. |
01:15 | If statement is used to execute a block of statements based on a condition. |
01:22 | It is called a single conditional statement. |
01:26 | Syntax for If statement ; |
01:28 | In the if statement, if the condition is true, the block is executed. |
01:34 | If the condition is false, the block is skipped and it is not executed. |
01:40 | Now, let us look at an example to understand how the If statement can be used. |
01:45 | So, let us switch to eclipse. |
01:48 | We will write a program to identify whether a person is Minor. |
01:53 | I have already created a class Person. |
01:56 | Now, inside the main method, let us declare a variable ‘age’ of type int. |
02:02 | So, type inside the main method: int age is equal to 20 semicolom. |
02:14 | Now, we will write an If statement as follows: |
02:18 | Next line if within bracket age < 21 open curly brackets Press Enter. |
02:30 | Here, we are checking if age is less than 21. |
02:34 | Whatever is inside the brackets belongs to the if block. |
02:38 | So, inside the brackets type: |
02:41 | System dot out dot println within brackets and double quotes The person is Minor semicolon. |
02:56 | Here, if age is less than 21, then “The person is minor” will be displayed. |
03:03 | So save and run the file. |
03:08 | We get the output as follows: "The person is minor". |
03:14 | In this case, the person's age is 20, which is less than 21. |
03:20 | So, we got the output as “The person is minor”. |
03:24 | Now, we will learn about if...else statement. |
03:27 | If...Else statement is used to execute alternative statements. |
03:31 | These are based on a single condition. |
03:34 | Let us look at the syntax for writing If…Else statement. |
03:38 | If the condition is True, the statement or block of code is executed. |
03:44 | Else, it executes another statement or block of code. |
03:49 | We will now see how the If…else statement can be used in a program. |
03:54 | So let us switch to the eclipse. |
03:57 | We will now write a program to identify whether the person is Minor or Major. |
04:03 | So, inside the Main method, type: int age is equal to 25 |
04:12 | Then, if within brackets age greater than 21 |
04:19 | within curly brackets type: System dot out dot println within brackets The person is Major. |
04:28 | Then type next line |
04:32 | else within curly brackets type: |
04:38 | System dot out dot println within brackets in double quotes The person is Minor semicolon. |
04:51 | Here, if age is less than 21, “The person is Minor” will be displayed. |
04:58 | Else the “The person is Major” will be displayed. |
05:02 | See, now Let us save and run the program. |
05:07 | We get the output as: "The person is major". |
05:11 | Here, the person's age is 25, which is greater than 21. |
05:17 | Therefore, the program display the output as “The person is Major”. |
05:22 | If…Else If statement is used to execute various set of statements. |
05:29 | These are based on the two given conditions. |
05:33 | You can also add more conditions based on your requirement. |
05:38 | It is also called as branching or decision making statement. |
05:43 | Now Let us look at the syntax for writing If…Else If statement. |
05:48 | If statement initially checks for condition 1. |
05:53 | If condition 1 is true, it executes the statement or block code. |
05:59 | Else it again checks for condition 2. |
06:02 | If condition 2 is true, it executes statement or block 2. |
06:09 | Else it executes statement 3 or block code 3. |
06:13 | In this way, we can extend the code by If…Else blocks. |
06:17 | These blocks can have multiple conditions. |
06:20 | The corresponding code will get executed, until it finds the true condition. |
06:25 | If all the conditions are false, it will execute the final Else section. |
06:30 | We will see how the If…Else If statement; can be used in a program. |
06:35 | So switch to Eclipse. |
06:37 | I have already created a class named Student. |
06:40 | Let us write a program to identify grade of a student. |
06:44 | This is done based on the score percentage. |
06:47 | So, inside the main method, type: int space testScore equal to 70 semicolon. |
06:58 | The input variable named testScore is used to get the score percentage. |
07:05 | Next line type: if within brackets testScore less than 35 within curly brackets System dot out dot println within brackets and double quotes C grade semicolon. |
07:28 | If the 'testScore' is less than 35, then the program displays "C Grade". |
07:34 | Next line, type else. |
07:37 | Next line, type: if within brackets testScore greater than or equal to 35 and testScore less than or equal to 60. Put the whole condition within brackets open curly brackets, press Enter. |
08:03 | Type: System dot out dot println within brackets B grade semicolon |
08:13 | Here, the program will check for the second condition in the Else If section. |
08:18 | If the 'testScore' is between 35 and 60 then the program displays "B Grade". |
08:24 | Next line, type: else within brackets type System dot out dot println within brackets and double quotes A grade semicolon. |
08:42 | So Finally, if both the conditions are False, the program displays “A Grade". |
08:48 | Now, let us save and run this code. |
08:51 | We get the output as A Grade. |
08:55 | In this program, the student’s 'testScore' is 70. |
09:00 | So the output will be displayed as “A Grade”. |
09:02 | Now Let us change the testScore to 55. |
09:07 | Now, save and run this program. |
09:10 | In this case, the output will be displayed as “B Grade”. |
09:16 | We can also increase the number of conditions. |
09:19 | Let us add one more condition after the “B grade” output section. |
09:23 | So type here,
Else, Next line if within brackets testScore greater than or equal to 60 and testScore less than or equal to 70. |
09:47 | Open curly brackets press Enter System dot out dot println within brackets and double quotes O grade semicolon. |
10:01 | Here, if the 'testScore' is between 60 and 70 the program will display "O Grade". |
10:07 | Now, change the testScore of the student to 70. |
10:12 | Now, save and run the file. |
10:15 | We get the output as follows. |
10:17 | The program will display the output as “O grade”. |
10:20 | It is not “A grade” as it displayed before. |
10:23 | The program will display “A grade” for the testScore greater than 70. |
10:28 | While coding conditional structures: |
10:30 | * Always remember to add a semicolon while terminating a statement. |
10:35 | * But don’t add semicolons after the condition. |
10:40 | * Add the block of code within curly brackets. |
10:43 | * Curly braces are optional if the block contains a single statement. |
10:49 | We have come to the end of this tutorial. |
10:51 | In this tutorial, |
10:53 | We have Explained conditional statements: |
10:56 | * Listed the types of conditional statements |
10:59 | * Used conditional statements: if, if...else and if...else if in Java programs. |
11:04 | Now take an assignment on writing java programs using conditional statements: if, if...else and if...else if statements. |
11:12 | * Write a java program to compare two values using if statement. |
11:17 | * Write a java program to check whether the given number is even or odd.
Hint : use if...else statement. |
11:23 | * Write a java program to find the biggest number among the three numbers.
Hint : use if...else if statement. |
11:29 | To know more about the Spoken Tutorial project, |
11:32 | watch the video available at the following link. |
11:35 | It summarizes the Spoken Tutorial project. |
11:38 | If you do not have good bandwidth, you can download and watch it. |
11:42 | The Spoken Tutorial Project Team: |
11:44 | * Conducts workshops using Spoken tutorials. |
11:47 | * gives certificates for those who pass an online test. For more details, please write to contact AT spoken HYPHEN tutorial DOT org. |
11:56 | Spoken Tutorial Project is a part of the Talk to a Teacher project. |
12:00 | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
12:06 | More information on this mission is available at spoken HYPHEN tutorial DOT org SLASH NMEICT HYPHEN Intro. |
12:15 | This script has been contributed by TalentSprint. This is Arya Ratish from IIT Bombay, signing off
Thanks for joining |