if Statement in Java - One-way Selection Control Structures
Coding River
if Statement in Java - One-way Selection Control Structures
In this video, we are going to talk about if Statement
Actually, if statements are part of what we call in programming: Selection Control Structures
In programming, Control Structures provide alternatives to sequential program execution and are used to alter the flow of execution of a program
The two most common control structures are the selection and the repetition
Basically, a computer has three ways of processing a program The sequential execution process, that means the execution starts at the beginning and follows the statements in order one after the other. No decisions are made and there is no repetition
The second way a computer processes a program is by making a selection or choice to execute particular statements based on one or more conditions
The third way to process a program is by repetition which consists of executing a statement over and over using a structure called a loop
So, we will start talking about the Selection Control Structures
In Java, we have three types of selection control structures, which are the if statements, the if …. else statements and the switch structure
In this video particularly, I will show you how to use if statement in order to make a one-way selection
Here on the screen is the syntax of a one-way selection using if statement
int age = 20;
if ( age greater than 18) System.out.println(“You are eligible to vote”);
Let me explain the elements of the syntax The syntax starts with the keyword if Followed by a logical expression in between the brackets it is also called a condition Next, we have the statement The statement is also called the Action Statement, simply because it refers to the action that the program will take whenever a condition is met
In the previous videos, we talked about logical expressions and relational operators So this logical expression is extremely useful because it will allow the program to make a decision about what to do or which statement to execute The logical expression is also called a Condition because as I said, it determines whether to execute the statement that follows it or not
During the execution of this small program, if the logical expression evaluates to true then the following statement will execute But if it evaluates to false then the following statement does not execute and it is skipped
This portion of code is called a one-way selection control structure because it allows the program to select a single action
So, in this code, if the logical expression evaluates to true then we will get the message You are eligible to vote in the console But if age evaluates to false then nothing will appear in the console because the action statement has been skipped
So guys that it concerning the ... https://www.youtube.com/watch?v=qWxipSsl3ZE
8526827 Bytes