Conditional statement

From Wikipedia, the free encyclopedia

In computer science, conditional statements are a vital part of a programming language. These statements are requests to the computer to make an execution choice based on a given condition.

One form is the If-Then clause, sometimes seen as :

If (condition) Then
(statements)
Else
(statements)
End If

It works this way - when the computer finds an If (a reserved word), it expects a data comparison, for example, x = 0 and evaluates the condition. If the condition is true, the statement block following the Then (another reserved word) shall be executed, otherwise the execution shall continue in the following statement block.

Depending on the language, the If may use Then, brackets, and another clause called Else or ElseIf.

There are many other conditional statements in programming languages, however, all of them follow these principles. Other conditional statements include switch statements, case statements and statements with guards.

Main article: Ternary operation

Many languages support alternative syntax for if statements: (condition)?(evaluate if condition was true):(evaluate if condition was false).
This is mainly used for in-line if statements that wouldn't be suitable in the place used, for example:

//Invalid
$var = if(true) { 'foo' } else { 'bar' };
//Valid
$var = (true)?'foo':'bar'

To accomplish the same as the second (correct) line above, using standard if/else construct:

if (true) {
  $var = 'foo';
}
else {
  $var = 'bar';
}

The alternative syntax can also be used inside if statements themselves: that is to say more generally, "The alternative syntax, accomplishing the equivalent of an analogous if/else construct, may be used anywhere an if/else construct may be appropriately used".

Contrary to other languages, in Smalltalk the conditional statement is not a language construct but defined as abstract method in the class Boolean as a method which takes two parameters, both clauses as closure. And then Boolean has two subclasses, True and False, which both define the method, True executing the first closure only, False executing the second closure only.

var := condition 
    ifTrue: [ 'foo' ]
    ifFalse: [ 'bar' ]

Advanced Search
Included Web Search Engines


Safe Search

close

Top Matching Results

Occasionally Search.com will highlight specialized results that are based on the context of your query. Examples of specialized results include specific links to news, images, or video.

Top Matching Results may highlight information from other Search.com pages, content from the CNET Network of sites, or third party content. The listings are based purely on relevance. Search.com does not receive payment for listings in this section but our partners that provide this data may get paid for listing these products.

Sponsored Links

This section contains paid listings which have been purchased by companies that want to have their sites appear for specific search terms and related content. These listings are administered, sorted and maintained by a third party and are not endorsed by Search.com.

Search Results

Search.com sends your search query to several search engines at one time and integrates the results into one list which has been sorted by relevance using Search.com's proprietary algorithm. You can customize the list of search engines included in your metasearch from the preferences.

The search engines that are used in your metasearch may allow companies to pay to have their Web sites included within the results. To view the Paid Inclusion policy for a specific search engine, please visit their Web site. Search.com does not accept payment or share revenue with any search engine partner for listings in this section.