Decision-making:
By: aathishankaran Printer Friendly Format
Decision-making: The if construct
If a certain condition is true, you direct the program to take one course of action. If the condition is false, you direct the program to do something else. This is one of the most fundamental concepts in computer programming. This decision-making can be done by means of the if construct. The if construct makes use of relational and logical operators for decision-making. The simplest form of if construct is as follows:
if (condition)
{
statement 1;
statement 2;
}
Example:
To accept two numbers and verify and whether they are equal or not, the following pseudocode can be witten:
PROGRAM_EQUAL
{
integer Number1;
integer Number2;
accept Number1;
accept Number2;
if(Number1 == Number2)
{
display “Input numbers are equalâ€;
}
}
If the numbers are equal, it displays the message “Input numbers are equal.â€. If the are not equal, the execution of program sequence terminates without any message. To handle the situation when two numbers are not equal, the following pseudocode can be written as:
PROGRAM_EQUAL
{
integer Number1, Number2;
accept Number1;
accept Number2;
if(Number1 == Number2)
{
display “Input numbers are equal.â€;
}
else
{
display â€Input numbers are not equal.â€;
}
}
The general form of if construct is as follows:
If( condition )
{
statement1;
statement2;
}
else
{
statement3;
statement4;
}
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Subscribe to Tutorials
Related Tutorials
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java