Difference between revisions of "Java/C2/if-else/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) |
|||
(12 intermediate revisions by one other user not shown) | |||
Line 5: | Line 5: | ||
|- | |- | ||
| 00:02 | | 00:02 | ||
− | | Welcome to the spoken tutorial on '''If else constructs '''in java. | + | | 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 | | 00:18 | ||
− | | For this tutorial we are using: | + | | For this tutorial, we are using: |
+ | '''Ubuntu v 11.10''' | ||
− | + | '''JDK 1.6''' and | |
− | + | '''Eclipse 3.7.0.''' | |
− | + | ||
− | Eclipse 3.7.0 | + | |
|- | |- | ||
| 00:27 | | 00:27 | ||
− | | To follow this tutorial you should have knowledge of using | + | | To follow this tutorial, you should have knowledge of using: |
|- | |- | ||
− | | | + | | 00:31 |
− | | | + | | '''Arithmetic''', '''Relational''' and '''Logical''' '''operators''' in Java. |
|- | |- | ||
| 00:35 | | 00:35 | ||
− | | If not, for relevant tutorials please visit our website which is as shown. | + | | If not, for relevant tutorials, please visit our website which is as shown. |
|- | |- | ||
| 00:42 | | 00:42 | ||
− | |'' Conditional statements'' You may have to perform different actions for different decisions in your code. | + | |'' 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 '''If...Else''' statement |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | | | + | | 01:03 |
− | | | + | | '''If...Else if''' statement |
|- | |- | ||
− | | | + | | 01:05 |
− | + | |'''Nested If''' statement '''Switch''' statement | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | | | + | |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
− | | | + | | 01:08 |
− | | In this tutorial, we will learn about '''If, If...Else and If...Else If | + | | In this tutorial, we will learn about '''If, If...Else and If...Else If''' statements in detail. |
|- | |- | ||
| 01:15 | | 01:15 | ||
− | + | | '''if statement''': '''if''' statement is used to '''execute''' a block of statements based on a condition. | |
− | + | ||
|- | |- | ||
− | | | + | | 01:22 |
| It is called a '''single conditional statement.''' | | It is called a '''single conditional statement.''' | ||
− | |||
|- | |- | ||
− | | | + | | 01:26 |
− | | | + | | Syntax for '''If''' statement ; |
− | + | ||
− | + | ||
|- | |- | ||
− | | | + | | 01:28 |
− | | In the '''if | + | | In the '''if''' statement, if the condition is true, the block is executed. |
− | + | ||
|- | |- | ||
− | | | + | | 01:34 |
− | | If the condition is | + | | 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 | + | | So, let us switch to '''eclipse.''' |
− | + | ||
|- | |- | ||
− | | | + | | 01:48 |
| We will write a program to identify whether a person is '''Minor'''. | | We will write a program to identify whether a person is '''Minor'''. | ||
− | |||
|- | |- | ||
− | | | + | | 01:53 |
− | | I have already created a class '''Person.''' | + | | 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''' space '''age ''' equal to 20 semicolon. |
|- | |- | ||
− | | | + | | 02:14 |
− | + | |Now, we will write an '''If''' statement as follows: | |
− | + | ||
− | + | ||
− | |Now, we will write an If statement as follows: | + | |
− | + | ||
|- | |- | ||
− | | | + | | 02:18 |
− | |Next line | + | |Next line, '''if ''' within brackets '''age < 21''' open curly brackets. Press Enter. |
− | + | ||
|- | |- | ||
− | | | + | | 02:30 |
| Here, we are checking if '''age''' is less than '''21'''. | | Here, we are checking if '''age''' is less than '''21'''. | ||
− | |||
|- | |- | ||
| 02:34 | | 02:34 | ||
− | | Whatever is inside the brackets belongs to the if block. | + | | Whatever is inside the brackets belongs to the '''if''' block. |
− | + | ||
|- | |- | ||
| 02:38 | | 02:38 | ||
− | |So | + | |So, inside the brackets type: |
− | + | ||
|- | |- | ||
| 02:41 | | 02:41 | ||
− | | '''System | + | | '''System''' dot '''out''' dot '''println''' within brackets and double quotes '''The person is Minor ''' semicolon. |
− | + | ||
|- | |- | ||
| 02:56 | | 02:56 | ||
− | | Here, if '''age '''is less than '''21,''' | + | | Here, if '''age '''is less than '''21,''' then “The person is minor” will be displayed. |
− | + | ||
|- | |- | ||
| 03:03 | | 03:03 | ||
− | | So | + | | So '''save''' and '''run''' the file. |
|- | |- | ||
| 03:08 | | 03:08 | ||
− | | We get the output as follows | + | | We get the output as follows: "The person is minor". |
− | + | ||
− | + | ||
|- | |- | ||
| 03:14 | | 03:14 | ||
− | | In this case, the person's age is''' 20''' | + | | In this case, the person's age is '''20''' which is less than '''21. ''' |
|- | |- | ||
| 03:20 | | 03:20 | ||
− | | So, we got the output as | + | | So, we got the output as “The person is minor”. |
− | + | ||
|- | |- | ||
| 03:24 | | 03:24 | ||
− | |Now, we will learn about '''if...else '''statement. | + | |Now, we will learn about '''if...else''' statement. |
− | + | ||
|- | |- | ||
| 03:27 | | 03:27 | ||
− | | If...Else statement is used to execute alternative statements. | + | | '''If...Else''' statement is used to '''execute''' alternative statements. |
− | + | ||
|- | |- | ||
− | | | + | | 03:31 |
| These are based on a single condition. | | 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 | + | | If the condition is '''true''', the statement or block of code is executed. |
− | + | ||
|- | |- | ||
− | | | + | | 03:44 |
− | | Else it executes another statement or block of code. | + | | Else, it executes another statement or block of code. |
|- | |- | ||
| 03:49 | | 03:49 | ||
− | | We will now see how the '''If…else | + | | We will now see how the '''If…else''' statement can be used in a program. |
|- | |- | ||
| 03:54 | | 03:54 | ||
− | | So let us switch to the eclipse. | + | | So, let us switch to the '''eclipse'''. |
|- | |- | ||
| 03:57 | | 03:57 | ||
− | | | + | | We will now write a program to identify whether the person is minor or major. |
− | + | ||
|- | |- | ||
| 04:03 | | 04:03 | ||
− | | So inside the ''' | + | | So, inside the '''main''' '''method''', type: '''int age''' equal to 25 |
− | + | ||
|- | |- | ||
| 04:12 | | 04:12 | ||
− | | then '''if | + | | then, '''if''' within brackets '''age''' greater than '''21''' |
|- | |- | ||
| 04:19 | | 04:19 | ||
− | | | + | | within curly brackets, type: '''System''' dot '''out''' dot ''' println''' within brackets '''The person is Major.''' |
− | + | ||
|- | |- | ||
| 04:28 | | 04:28 | ||
− | | Then type,next line | + | | Then type, next line, |
|- | |- | ||
| 04:32 | | 04:32 | ||
− | | '''else | + | | '''else ''' within curly brackets type: |
|- | |- | ||
| 04:38 | | 04:38 | ||
− | | '''System | + | | '''System''' dot '''out''' dot '''println''' within brackets in double quotes '''The person is Minor''' semicolon. |
− | + | ||
|- | |- | ||
| 04:51 | | 04:51 | ||
− | | Here, if '''age | + | | Here, if '''age''' is less than '''21''', “The person is Minor” will be displayed. |
− | + | ||
|- | |- | ||
| 04:58 | | 04:58 | ||
− | | | + | | Else, the “The person is Major” will be displayed. |
− | + | ||
|- | |- | ||
| 05:02 | | 05:02 | ||
− | | | + | | So, now let us '''save''' and ''' run''' the program. |
|- | |- | ||
| 05:07 | | 05:07 | ||
− | | We get the output as | + | | We get the output as: "The person is Major". |
|- | |- | ||
| 05:11 | | 05:11 | ||
− | | Here, the person's | + | | Here, the person's age is '''25,''' which is greater than '''21'''. |
− | + | ||
|- | |- | ||
| 05:17 | | 05:17 | ||
− | | Therefore the program | + | | Therefore, the program displayed the output as “The person is Major”. |
− | + | ||
− | + | ||
|- | |- | ||
− | | | + | | 05:22 |
− | | '''If…Else If''' statement is used to execute various set of statements. | + | | '''If…Else If''' statement: '''If…Else If''' statement is used to execute various set of statements. |
− | + | ||
|- | |- | ||
− | | | + | | 05:29 |
| These are based on the two given conditions. | | These are based on the two given conditions. | ||
− | |||
|- | |- | ||
|05:33 | |05:33 | ||
| You can also add more conditions based on your requirement. | | You can also add more conditions based on your requirement. | ||
− | |||
|- | |- | ||
− | | | + | | 05:38 |
− | | It is also called as ''' branching '''or | + | | 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. | + | | Now, Let us look at the syntax for writing the '''If…Else If '''statement. |
− | + | ||
|- | |- | ||
− | | | + | | 05:48 |
− | | If statement initially checks for '''condition 1.''' | + | | '''If''' statement initially checks for '''condition 1.''' |
− | + | ||
|- | |- | ||
− | | | + | | 05:53 |
− | | If''' condition 1 '''is true, it executes the '''statement | + | | If '''condition 1''' is true, it executes the '''statement-or-block 1 code'''. |
− | + | ||
|- | |- | ||
| 05:59 | | 05:59 | ||
− | | Else it again checks for '''condition 2.''' | + | | Else, it again checks for '''condition 2.''' |
− | + | ||
|- | |- | ||
| 06:02 | | 06:02 | ||
− | | If '''condition 2''' is true, it executes '''statement | + | | If '''condition 2''' is true, it executes '''statement-or-block 2'''. |
− | + | ||
|- | |- | ||
| 06:09 | | 06:09 | ||
− | | Else it executes '''statement 3''' or '''block code 3'''. | + | | Else, it executes '''statement 3''' or '''block code 3'''. |
− | + | ||
|- | |- | ||
| 06:13 | | 06:13 | ||
| In this way, we can extend the code by '''If…Else''' blocks. | | In this way, we can extend the code by '''If…Else''' blocks. | ||
− | |||
|- | |- | ||
| 06:17 | | 06:17 | ||
− | | These blocks can have multiple conditions. | + | | These '''blocks''' can have multiple conditions. |
− | + | ||
|- | |- | ||
| 06:20 | | 06:20 | ||
| The corresponding code will get executed, until it finds the true condition. | | The corresponding code will get executed, until it finds the true condition. | ||
− | |||
|- | |- | ||
Line 361: | Line 305: | ||
|- | |- | ||
− | | | + | | 06:30 |
− | | | + | | We will see how the '''If…Else If statement''' can be used in a program. |
− | + | ||
|- | |- | ||
− | | | + | | 06:35 |
− | | So switch to Eclipse. | + | | So, switch to '''Eclipse'''. |
− | + | ||
|- | |- | ||
− | | | + | | 06:37 |
− | | I have already created a class named Student. | + | | I have already created a '''class''' named '''Student'''. |
− | + | ||
|- | |- | ||
| 06:40 | | 06:40 | ||
− | | Let us write a program to identify grade of a student. | + | | Let us write a program to identify '''grade''' of a student. |
− | + | ||
|- | |- | ||
| 06:44 | | 06:44 | ||
| This is done based on the score percentage. | | This is done based on the score percentage. | ||
− | |||
|- | |- | ||
| 06:47 | | 06:47 | ||
− | | So | + | | So, inside the '''main''' '''method''', type: '''int''' space '''testScore''' equal to '''70''' semicolon. |
− | + | ||
|- | |- | ||
| 06:58 | | 06:58 | ||
− | | The input variable named | + | | The input variable named '''testScore''' is used to get the score percentage. |
− | + | ||
|- | |- | ||
| 07:05 | | 07:05 | ||
− | | Next line | + | | Next line, type: '''if''' within brackets '''testScore''' less than '''35''' then within curly brackets type: '''System''' dot '''out''' dot '''println''' within brackets and double quotes '''C grade''' semicolon. |
− | + | ||
|- | |- | ||
| 07:28 | | 07:28 | ||
− | | If the | + | | If the 'testScore' is less than 35, then the program displays "C Grade". |
− | + | ||
|- | |- | ||
| 07:34 | | 07:34 | ||
− | | Next line type '''else ''' | + | | Next line, type '''else'''. |
− | + | ||
|- | |- | ||
| 07:37 | | 07:37 | ||
− | | | + | | Next line, '''if''' within brackets '''testScore''' greater than or equal to '''35''' '''AND''' (&&) '''testScore ''' less than or equal to '''60.''' Put this whole condition within brackets open curly brackets, press Enter. |
|- | |- | ||
| 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 424: | Line 358: | ||
|- | |- | ||
| 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 ''' | + | | So Finally, if both the conditions are '''false''', the program displays “A Grade". |
− | + | ||
|- | |- | ||
| 08:48 | | 08:48 | ||
| Now, let us '''save''' and '''run''' this code. | | Now, let us '''save''' and '''run''' this code. | ||
− | |||
− | |||
− | |||
|- | |- | ||
| 08:51 | | 08:51 | ||
− | | | + | | We get the output as '''A Grade'''. |
|- | |- | ||
− | | | + | | 08:55 |
− | | In this program, the student’s | + | | In this program, the student’s 'testScore' is 70. |
− | + | ||
|- | |- | ||
− | | | + | | 09:00 |
− | | So the output will be displayed as | + | | So the output will be displayed as “A Grade”. |
|- | |- | ||
− | | | + | | 09:02 |
− | | Now Let us change the''' testScore''' to''' 55.''' | + | | Now Let us change the '''testScore''' to ''' 55.''' |
− | + | ||
|- | |- | ||
− | | | + | | 09:07 |
| Now, '''save''' and '''run''' this program. | | Now, '''save''' and '''run''' this program. | ||
|- | |- | ||
− | | | + | | 09:10 |
− | | In this case, the output will be displayed as | + | | 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 | + | | Let us add one more condition after the “B grade” output section. |
− | + | ||
|- | |- | ||
− | | | + | | 09:23 |
| So type here, | | 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 | | 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 |
− | | Here if the | + | | 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'''. | + | | Now, change the '''testScore''' of the student to '''70'''. |
− | + | ||
|- | |- | ||
− | | | + | | 10:12 |
| Now, '''save '''and''' run''' the file. | | Now, '''save '''and''' run''' the file. | ||
− | |||
− | |||
− | |||
|- | |- | ||
| 10:15 | | 10:15 | ||
− | + | | We get the output as follows. | |
− | + | ||
|- | |- | ||
− | | | + | | 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''': |
|- | |- | ||
− | | | + | | 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. |
|- | |- | ||
Line 552: | Line 466: | ||
|- | |- | ||
− | | | + | | 10:51 |
| In this tutorial, | | In this tutorial, | ||
|- | |- | ||
− | | | + | | 10:53 |
− | | We have Explained conditional statements | + | | 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 | | 11:04 | ||
− | |Now take an assignment on writing java programs using conditional statements | + | |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. | |
− | + | ||
− | + | ||
− | | | + | |
− | + | ||
− | 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. |
− | + | ||
− | 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. | | watch the video available at the following link. | ||
|- | |- | ||
| 11:35 | | 11:35 | ||
− | | It | + | | It summarizes the Spoken Tutorial project. |
− | + | ||
|- | |- | ||
| 11:38 | | 11:38 | ||
− | | If you do not have good bandwidth, you can download and watch it | + | | 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 | | 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 | Thanks for joining | ||
− | |||
− | |||
− | |||
− | |||
|} | |} |
Latest revision as of 15:16, 9 March 2017
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 If...Else statement |
01:03 | If...Else if statement |
01:05 | Nested If statement 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: 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 space age equal to 20 semicolon. |
02:14 | Now, we will write an If statement as follows: |
02:18 | Next line, if within brackets 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 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 | So, 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 displayed the output as “The person is Major”. |
05:22 | If…Else If statement: 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 the 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 1 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 then within curly brackets type: 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, if within brackets testScore greater than or equal to 35 AND (&&) testScore less than or equal to 60. Put this 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 |