Professional Documents
Culture Documents
Physics 12th
Physics 12th
Control Structures
There are mainly three steps involved in a computer program.
1. Input
2. Processing
3. Output
Various Java statements, which we’ll soon discuss, enable you to specify that the next statement to execute is
not necessarily the next one in sequence. This is called transfer of control.
Decisions
We, as a human, make decisions every day. These decisions may include small scale or large scale decisions.
For example, we take an umbrella with us if it is raining outside.
How do we make decision in this case?
1. We ask ourselves a question i.e. Is it raining outside?
2. To find the answer, we use some means to check the weather condition outside, maybe by actually
looking outside, checking the weather on smartphone etc.
3. We get an answer in the form of YES or NO.
4. Based on this answer, we decide our action. In our example, there are two main actions:
a. In case of raining, take an umbrella.
b. Otherwise, don’t take an umbrella.
So, we normally take these steps to make a decision. Can we enable the computer to make such decisions based
on condition(s)?
Flowchart
Flowcharts graphically represents an algorithm. Flowcharts are very useful in explaining programs.
Flowcharts are independent of the programming language. You use simple, easy to understand terms in
flowchart symbols so that any person can understand the flow and logic of the program.
Following are some flowchart symbols and their purpose:
Here, we can easily see that the statements are executed one after another, there are no
branches/decisions/selections. Also, we can easily get an idea about the program that it is calculating profit on
sales.
In selection or decision making flowchart, there will be at least one decision (diamond) symbol.
All of these statements contain some condition (written in red) and based on the result of that condition
some action is performed.
In first three statements, we have only one choice of action when the condition is met (true)
but in the 4th statement we have two choices of action. One choice is performed if the condition
is met (true), other choice is performed if the condition is not met (false).
Do you see that the answer to each of these conditions is either true or false?
Yes, those expressions which either evaluate to true or false are called boolean expressions.
Java allow us to program such statements. The first decision making structure Java gives us is if statement.
Syntax:
if ( boolean expression ) {
statement(s) to be executed when logical expression is true
}
Note that if is a keyword followed by a boolean expression also called a condition in parenthesis,
followed by a statement or a set of statements inside curly brackets. The statement(s) is/are executed when the
boolean expression is true. These statements are also called action statements.
true
logical
statement(s)
expression
false
Relational Operators
To make decisions, you must be able to express conditions and make comparisons. For example, a bank wants
to send a notice to a customer if her or his checking account balance falls below the required minimum balance.
That is, if the balance is below the required minimum, the bank should send a notice to the customer; otherwise,
it should do nothing. Similarly, an online shopping website adds shipping charges if the amount is less than
1000 PKR. Both of these examples involves some sort of comparison. You can compare items for equality, and
inequality. You can also determine whether one item is greater than another, less than another and so on.
An expression that has a value of either true or false is called a logical (boolean) expression.
The values true and false are called logical (boolean) values. In Java, a condition is represented by
a logical (boolean) expression; conditions are either true or false.
Java provides us relational operators to make comparisons and check for equality/inequality.
Code Examples
Program: SimpleIf.java
import java.util.Scanner;
Program: RelationalOperators.java
import java.util.Scanner;
Program: PositiveNegative.java
import java.util.Scanner;
}//main
}//class
Common Errors
1. Using semi-colon after if condition
if(condition);{
if(condition){
};
if(a = b){
Correct Way
if(a == b){
System.out.println(“a and b are equal”);
}
if( condition )
{