Skip to main content

Expressions

Operators & Expression


C & C++ Language, C#.NET

The Relational Operators.

Name Operator Sample Evaluates
Equals == 100 == 50; false
50 == 50; true
Not Equals != 100 != 50; true
50 != 50; false
Greater Than > 100 > 50; true
50 > 50; false
Greater Than >= 100 >= 50; true
or Equals 50 >= 50; true
Less Than < 100 < 50; false
50 < 50; false
Less Than <= 100 <= 50; false
or Equals 50 <= 50; true

Logical operators.

Operator Symbol Example
AND && exp1 && exp2
OR  || exp1 || exp2
NOT  ! !exp1

Logical operators in use.

Expression What It Evaluates To
(exp1 && exp2) True (1) only if both exp1 and exp2 are true;
False (0) otherwise
(exp1 || exp2) True (1) if either exp1 or exp2 is true;
False (0) only if both are false
(!exp1) False (0) if exp1 is true;
True (1) if exp1 is false

Compound assignment operators.

When You Write This... It Is Equivalent To This
x *= y x = x * y
y -= z + 1 y = y - z + 1
a /= b a = a / b
x += y / 8 x = x + y / 8
y %= 3 y = y % 3

If - Else Condition

Decision making structures require that the programmer specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.

 The following conditional statements:

  • Use if to specify a block of code to be executed, if a specified condition is true
  • Use else to specify a block of code to be executed, if the same condition is false
  • Use else if to specify a new condition to test, if the first condition is false

Form 1: If statement

It is the simplest form of control statement, frequently used in decision making and changing the control flow of the program execution. Where, condition is a Boolean or relational condition and Statement(s) is a simple or compound statement.

if (expression)
 statement;

If expression evaluates to true, statement is executed

int x, y;
if (x == y)
  printf("x is equal to y\n");

if (x > y)
  printf("x is greater than y\n");

if (x < y)
  printf("x is smaller than y\n");

Suppose :
Input an integer value for x: 100
Input an integer value for y: 10
x is greater than y

Input an integer value for x: 10
Input an integer value for y: 100
x is smaller than y

Input an integer value for x: 10
Input an integer value for y: 10
x is equal to y


An if statement can control the execution of multiple statements through the use of a compound statement, or block.
if (expression)
{
   statement 1;
   statement 2;
.
.
.
/* additional code goes here */
  statement n;
}

Form 2: The if - else Clause

An if statement can optionally include an else clause. An If statement can be followed by an optional Else statement, which executes when the Boolean expression is false.
If the Boolean expression evaluates to true, then the if block of code will be executed, otherwise else block of code will be executed. The else clause is included as follows:

if (expression)      
  statement1;  
else      
  statement2;  

If expression evaluates to true, statement1 is executed. If expression evaluates to false, statement2 is executed. Both statement1 and statement2 can be compound statements or blocks.

Form 3: The if -else if Clause

if( expression1 )
  statement1;
else if ( expression2 )
  statement2;
else
  statement3;

  next_statement;

This is a nested if. If the first expression, expression1, is true, statement1 is executed before the program continues with the next_statement.
If the first expression is not true, the second expression, expression2, is checked.
If the first expression is not true, and the second is true, statement2 is executed.
If both expressions are false, statement3 is executed.
Only one of the three statements is executed.

If - Else Condition / Expression - VB.NET

 The following conditional statements:

  • Use if to specify a block of code to be executed, if a specified condition is true
  • Use else to specify a block of code to be executed, if the same condition is false
  • Use else if to specify a new condition to test, if the first condition is false

Form 1: If statement - VB.NET

It is the simplest form of control statement, frequently used in decision making and changing the control flow of the program execution. Where, condition is a Boolean or relational condition and Statement(s) is a simple or compound statement Syntax :

 If boolean_expression Then   
    [Statement(s)]  
 End If
Example: 
  If BackColor = Color.Red Then
     BackColor = Color.Blue
     Exit Sub
  End If

Note the use of the End If  keywords to terminate this construct.

Form 2: The if - else Clause - VB.NET

An If statement can be followed by an optional Else statement, which executes when the Boolean expression is false.
If the Boolean expression evaluates to true, then the if block of code will be executed, otherwise else block of code will be executed.

If(boolean_expression)Then     
   'statement(s) will execute if the Boolean expression is true   
Else    
   'statement(s) will execute if the Boolean expression is false   
End If
Example: 
    If num2 = 0 Then       
      MsgBox("Cannot divide by 0")     
    Else      
      MsgBox (num1 / num2)    
    End If

Form 3: The if -else if Clause - VB.NET

The If...Else If...Else Statement

Use the else if statement to specify a new condition if the all previous conditions are False.
If(boolean_expression 1)Then     
   ' Executes when the boolean expression 1 is true   
ElseIf( boolean_expression 2)Then     
   ' Executes when the boolean expression 2 is true    
Else      
 ' executes when the none of the above condition is true   
End If  

Module Module1
  Function Test(ByVal ops As String) As Integer
 ' Check input with If, ElseIf and Else.
  If ops = "cat" Then
    Return 1
  ElseIf ops = "dog" Then
    Return 2
   Else
    Return 0
   End If
  End Function

Sub Main()
  ' Call Test function.
  Console.WriteLine(Test("dog"))
  Console.WriteLine(Test("cat"))
  Console.WriteLine(Test("elephant"))
End Sub
End Module

Comments

Popular posts from this blog

Robot

Robot is drawn from an old Church Slavonic word, robota, for “servitude,” “forced labor” or “drudgery.” The word, which also has cognates in German, Russian, Polish and Czech, was a product of the central European system of serfdom by which a tenant’s rent was paid for in forced labor or service. The word robot was coined by artist Josef Čapek, the brother of famed Czechoslovakian author Karel Čapek. As a word, robot is a relative newcomer to the English language. It was the brainchild of a brilliant Czech playwright, novelist and journalist named Karel Čapek (1880-1938) who introduced it in his 1920 hit play, R.U.R., or Rossum’s Universal Robots. The robots in this play were not what we would call robots today, and they weren’t made of steel, plastic, and lines of code. Those robots were manufactured as pseudo-organic components out of a substance that acted like protoplasm in a factory, then “assembled” into humanoids. Watch video --> Saudi Arabia grants citizenship to huma...

The 120 year old light bulb that never been turned off.

Light bulb that never been turned off since 1901. ivermore's Centennial Light Bulb The Centennial Light is the world's longest-lasting light bulb, burning since 1901 , and almost never switched off. Due to its longevity, the bulb has been noted by The Guinness Book of World Records , Ripley's Believe It or Not!, and General Electric.  The Centennial Light was originally a 30-watt(or 60-watt) bulb, but is now very dim, emitting about the same light as a 4-watt nightlight. The hand-blown, carbon-filament common light bulb was manufactured in Shelby, Ohio, by the Shelby Electric Company in the late 1890s and was invented by Adolphe A. Chaillet. The hand-blown, carbon-filament common light bulb was invented by Adolphe Chaillet, a French engineer who filed a patent for this technology. It was manufactured in Shelby, Ohio, by the Shelby Electric Company in the late 1890s; many just like it still exist and can be found functioning. According to Zylpha Bernal Beck, the bulb was...

Wireless power transfer

Wireless power transfer Inventor and engineer Nikola Tesla, 'the man who invented the 20th century' theorized about wireless electricity back in the 1890s. He even demonstrated the principle by lighting up glass tubes with wireless power transmission. Nikola Tesla wanted to create the way to supply power without stringing wires. He almost accomplished his goal when his experiment led him to creation of the Tesla coil. It was the first system that could wirelessly transmit electricity. Wireless power transfer (WPT), wireless power transmission, wireless energy transmission (WET), or electromagnetic power transfer is the transmission of electrical energy without wires as a physical link. Wireless power transfer (WPT) is one of the hottest topics being actively studied, and it is being widely commercialized. In particular, there has been a rapid expansion of WPT in mobile phone chargers, stationary charging electric vehicles (EVs), and dynamic charging EVs, also called road-po...