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
Post a Comment