Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
18 views29 pages

Lecture 3 - Conditional Statement

Conditional statements are essential for decision-making in programming, allowing different actions based on varying conditions, primarily using the 'if' statement. The document explains various conditional constructs, including 'if-else', 'else if', and 'switch' statements, along with logical operators like AND, OR, and NOT. Examples illustrate the syntax and functionality of these statements, emphasizing their role in controlling the flow of execution based on conditions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views29 pages

Lecture 3 - Conditional Statement

Conditional statements are essential for decision-making in programming, allowing different actions based on varying conditions, primarily using the 'if' statement. The document explains various conditional constructs, including 'if-else', 'else if', and 'switch' statements, along with logical operators like AND, OR, and NOT. Examples illustrate the syntax and functionality of these statements, emphasizing their role in controlling the flow of execution based on conditions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Conditional

Statements
Conditional Statements
Decision Making
 Conditional statements are used to perform
different actions based on different conditions.
 The if statement is one of the most frequently
used conditional statements.
 If the if statement’s condition expression
evaluates to true, the block of code inside
the if statement is executed. If the
expression is found to be false, the code
within the if statement is ignored and the
first set of code after the end of the if
statement (after the closing curly brace) is
executed.
Example
Syntax:
An example of compound
assignment operator
Any of the following comparison operators
may be used to form the condition:
• Left Bracket (<) - less than
• Right Bracket (>) - greater than
• Exclamation Mark and Equal Sign (!=) - not equal to
• Double Equal Sign (==) - equal to
• Left Bracket and Equal Sign (<=) - less than or equal to
• Right Bracket and Equal Sign (>=) - greater than or
equal to
Example
An example of conditional statement using
comparison operators:
if... else Statements

• You can use one if-else statement inside


another if or else statement.
Example
Nested if Statements

 You can use one if-else statement inside


another if or else statement.
 For example: Examples of variable declarations:
Example
Output of the Example
else if Statements

The Math operators


• Instead of using nested if-else statements, you can use the else if
statement to check multiple conditions.
• For example:
Example
Logical Statements

 Logical Operators
 using the AND logical operator, denoted by two (2)
ampersands (&&), is the easier way:
Example
Example
The OR Operator

• The OR operator, denoted by two (2) vertical bars ( ||),


checks if any one of the conditions is true.
• The condition becomes true, if any one of the operands
evaluates to true.
Example
The NOT Operator

• The NOT logical operator, denoted by a single


exclamation point (!), is used to reverse the logical
state of its operand. If a condition is true, the NOT
logical operator will make it false.
Example
The switch Statement

 A switch statement tests a variable for


equality against a list of values. Each value
is called a case, and the variable being
switched on is checked for each case.
Example
Syntax:
 When the variable being switched on is
equal to a case, the statements following
that case will execute unit a break
statement is reached.
 When a break statement is reached, the
switch terminates, and the flow of control
jumps to the next line after the switch
statement.
 Not every case needs to contain a break.
If no break appears, the flow of control will
fall through to subsequent cases until a
break is reached.
 The example tests day against a set of
values and prints a corresponding
message:
The default Statement

 A switch statement can have an option


default case.
 The default case can be used for
performing a task when none of the cases
is matched.
Example
END

You might also like