You are on page 1of 4

SH1803

Conditionals and Loops


I. The if-else Statement

A. The if Statement
 A conditional statement that executes a block of code when a condition returns a
Boolean value of true.
 Can have conditions of any expression, so long as it returns Boolean values (true or
false).

B. Relational Operators

SoloLearn.com

 Used mostly to evaluate conditions for conditional statements.


 Uses a good variety of operators for different expressions.

C. The else Clause


 A clause that can be used for the for the if statement.
 Can be specified to execute a block of code when the condition in the if block returns a
false value.

D. Nested if-else Statements


 These are if-else statements within another if-else statement.
 Programmers can nest an infinite amount of if-else statements.

E. The if-else-if Statement


 A compound conditional statement that can be used to help a program to decide among
three (3) or more actions.
 An if statement can have zero (0) or more else-if's clauses and they must come before
the last else clause, which is optional.
 Once an else-if clause succeeds, none of the remaining else-if's or else clauses will be
tested.

II. The switch Statement


 Provides a more elegant way to test 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 switch
case.
 A switch statement can include any number of cases. However, no two case labels may
contain the same constant value.
 Each case should have a break statement to end the code block.

02 Handout 1 *Property of STI


 student.feedback@sti.edu Page 1 of 4
SH1803

A. The default Case


 An optional case that is executed when none of the previous cases match.
 Essentially similar in principle to the else statement.

III. The while Loop


 Repeatedly executes a block of code as long as a given condition is satisfied.
 Once the condition is not satisfied during a given repetition (also known as an iteration),
the loop will stop running.
 Compound arithmetic and increment/decrement operators (both prefix and postfix) can be
used to further control the number of times a loop runs.

IV. The for Loop


 A type of loop that executes a statement for a specific number of times.
 Declares a variable for a counter (also called the loop control variable), commonly named
init.
 The condition evaluates the value of the counter and the body of loop is executed if the
condition is satisfied.
 After loop execution, the increment statement will update the counter.
 The loop will then continue to repeat until the condition fails to be satisfied with the current
values.

V. The do-while Loop


 A loop structure that is fundamentally similar to the while loop.
 The only difference is that this loop is guaranteed to execute an iteration at least one (1)
time.

VI. break and continue

A. The break Statement


 Used for terminating certain control structures, such as switch statements and loops.
 For switch statements, absence of break statements will cause ALL case statements
after the matching case statement to be executed, even when the case labels don’t
match the switch variable.
 For loop bodies, the statement can be used to immediately terminate a loop if given a
certain condition; this means that the break statement will effectively stop the loop
from repeatedly executing statements.

B. The continue Statement


 Is fundamentally similar to the break statement.
 Differs to the break statement in that, instead of terminating the loop entirely, it skips
the current iteration of the loop and continues on to the next iteration.

02 Handout 1 *Property of STI


 student.feedback@sti.edu Page 2 of 4
SH1803

VII.Logical Operators

SoloLearn.com

 Operators that are used to join multiple expressions and/or conditions.


 Can only return Boolean values (true or false).
 Can be used to create compound conditions and expressions that allow for executions of
specific code blocks in a program.

A. The AND (&&) Operator

SoloLearn.com

 An operator represented by two (2) ampersand signs.


 Will only return a Boolean value of true if and only if all expressions involved return
a value of true.
 Any operand that returns a value of false will make the entire expression return a value
of false.

B. The OR (||) Operator

SoloLearn.com

 An operator represented by two (2) vertical bars.


 Will return a Boolean value of true if any one of its operands return a value of true.

02 Handout 1 *Property of STI


 student.feedback@sti.edu Page 3 of 4
SH1803

C. The Logical NOT (!) Operator

SoloLearn.com

 An operator represented by an exclamation mark.


 Works using just a single operand.
 Reverses the logical state of an existing operand (an operand that normally evaluates
as true will return a value of false, and vice-versa).

VIII. The Conditional Operator

A. The ?: Operator
 Is represented by the question mark (?) and the colon (:) symbols.
 An operator used to shorten expressions and structures that require at least three (3)
expressions.
 The first expression is evaluated; the resulting Boolean value will then dictate which of
the remaining two (2) remaining expressions are evaluated.
o If the first expression returns a value of true, then the second expression is evaluated
and its resulting value becomes the value of the whole expression.
o If the first expression returns a value of false, then the third expression is evaluated
and its resulting value becomes the value of the whole expression.

References:
 SoloLearn. (n.d.). C# tutorial. Retrieved from https://www.sololearn.com/Course/CSharp/
 Microsoft (n.d.). Introducing Visual Studio.NET. Retrieved May 2016 from
http://msdn.microsoft.com/en-uslibrary/fc6bk1f4(v=vs.71).aspx.

02 Handout 1 *Property of STI


 student.feedback@sti.edu Page 4 of 4

You might also like