if ... else statements two-way selection control structures
Coding River
if ... else statements two-way selection control structures
Let’s continue our discussion on selection control structures In our last video, we talked about how to create a one-way selection control structure in a program using if statement So, in this video, I am now going to show you how to create a two-way selection control structure using if else statement
The if else statement is useful when you want your program to choose between two action statements
On the screen, I have written the syntax of the if else selection statement If you look closer, it has the same syntax as compared to the if statement except that here we have added the keyword else and a second action statement
int age = 20;
If ( age is greater then 18 ) System.out.println(“You are eligible to vote”); else System.out.println(“You are not eligible to vote”);
Now, let me explain this syntax As you can see, it begins with the keyword if Next, we have the logical expression or the condition inside the brackets Followed by the first action statement
After, we get the keyword else Which is followed by a second action statement
So now during the execution of the program, if the condition evaluates to true Then the first action statement will execute But if the condition evaluates to false then the second action statement will execute; skipping the first action statement
According to the code I have written, that means that if the value stored in age is greater than 18 then the message You are eligible to vote will show in the console But if the value in age is less than 18 then the message You are not eligible to vote will be returned to the console
#codingriver #java #programming ... https://www.youtube.com/watch?v=glc7EYPLpFo
5815046 Bytes