0% found this document useful (0 votes)
9 views

Conditional Statements Group Report.pptx NEW 5

Uploaded by

yeagerbadboi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Conditional Statements Group Report.pptx NEW 5

Uploaded by

yeagerbadboi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 50

Department of Industrial and

Information Technology

DCIT 22: Computer Programming I

LESSON 1
PREPARED BY:
BALLER, JOHN HENRY V.
CABIGAN, MJ R.
DIGMAN, MA. LOVINIA B.
GALON, LERITA JOY O.
LAGUARTILLA JR., VIRGILIO E.
MAGNAY, RHEA LYN B.
PAMPILLONA, TRISTAN T.
PEREZ, NHAZ D.
TOQUERO, JACOB ZACH R .
Department of Industrial and
Information Technology

GROUP4
PRESENTATION
Department of Industrial and
Information Technology

TOPIC
Vl. Conditional • What is a Statement?
Statements • Types of Statement
a. Declaration Statement
b. Expression Statement
c. Control Flow Statement
i. Conditional
ii. Repetition
Department of Industrial and
Information Technology

TOPIC
• A Block Statement
The Conditional Statement
a. If statement
b. If Else statement
c. If Else statement
d. Switch Statement
e. Nested Conditional Statement
Department of Industrial and
Information Technology

TOPIC
Vll. Repetition • The for Statement
Statements a. Initialization
b. Condition Expression
c. Expression List
Department of Industrial and
Information Technology

TOPIC
• The while statement
The do-while statement
The for-each statement
The break statement
The continue statement
The empty statement
Department of Industrial and
Information Technology

 Conditional Statements
• A statement that can be executed based on a condition is
known as a “Conditional Statement”. The statement is
often a block of code.
Department of Industrial and
Information Technology

 What is a Statement?
• A statement is a single line of code that performs an
action or expresses a command.
• The actions that a program takes are expressed in
statements. Common actions include declaring variables,
assigning values, calling methods, looping through
collections, and branching to one or another block of
code, depending on a given condition.
Department of Industrial and
Information Technology

Exp:
Department of Industrial and
Information Technology

 Types of Statements
 Declaration Statement
 Expression Statement
 Control Flow Statement
Department of Industrial and
Information Technology

 Declaration Statement
• Declaration statements are used to declare and initialize variables.
• The following code shows examples of variable declarations with and
without an initial assignment, and a constant declaration with the
necessary initialization.
Exp:
Department of Industrial and
Information Technology

 Expression Statement
• An expression followed by a semicolon is called an expression statement.
• The following code shows examples of expression statements, including
assignment, object creation with assignment, and method invocation.
Exp:
Department of Industrial and
Information Technology

 Control Flow Statement

• Control flow statements in C# are essential for directing the flow


of program execution. They enable you to make decisions, repeat
actions, and create structured and dynamic behavior. Selection
statements such as 'if', 'else', and'switch' allow you to make
conditional decisions. Iteration statements like 'for', 'while', 'do-
while', and 'foreach' make repetition possible.Jump statements
such as 'break,' 'continue,' and'return' allow you to deviate from
the typical flow.
Department of Industrial and
Information Technology

Conditional
• Used to make decisions in the code based on certain
conditions
Types of Conditional Statements:
•If statement
•Else statement
•Else if statement
•Switch statement
Department of Industrial and
Information Technology

Repetition
Repetition statements are called loops, and are used to repeat
the same code mulitple times in succession.
The number of repetitions is based on criteria defined in the loop
structure, usually a true/false expression.
Department of Industrial and
Information Technology

Exp:
Department of Industrial and
Information Technology

 BLOCK STATEMENT
A block is a combination of zero
or more statements that is
enclosed inside curly brackets
{ }.
Department of Industrial and
Information Technology

CONDITIONAL STATEMENT
Conditional statements in are used to make decisions in your code based
on certain conditions or expressions. These statements allow you to
control the flow of your program by executing different blocks of code
depending on whether a condition is true or false.
Department of Industrial and
Information Technology

• IF STATEMENT
a. if statement is a control
structure used to execute a
block of code conditionally. It
allows you to specify a
condition, and if that condition
evaluates to true, the code
block within the "if" statement
is executed.
Department of Industrial and
Information Technology

• IF - ELSE STATEMENT
b. The if-else statement allows us to
provide an alternative block of code
to execute if the condition is false. An
if-else statement in C# is a control
structure that allows you to
conditionally execute different blocks
of code based on a specified
condition. It is used to make decisions
in your program based on whether a
particular condition is true or false.
Department of Industrial and
Information Technology

• IF - ELSE - IF STATEMENT
c. In C#, an "if-else-if" statement is a
control structure that allows you to
execute different blocks of code
based on multiple conditions. It
provides a way to test multiple
conditions sequentially and perform
different actions depending on the
outcome of each condition.
Department of Industrial and
Information Technology

• SWITCH STATEMENT
d. A switch statement executes the
statement list in the first switch section
whose case pattern matches a match
expression and whose case guard, if
present, evaluates to true. A switch
statement evaluates case patterns in text
order from top to bottom. The compiler
generates an error when a switch statement
contains an unreachable case. That is a case
that is already handled by an upper case or
whose pattern is impossible to match.
Department of Industrial and
Information Technology
Department of Industrial and
Information Technology

EXP: Output:
Department of Industrial and
Information Technology

• NESTED CONDITIONAL
STATEMENT
e. The nested if statement in C#
refers to the concept of having one
if statement inside another if
statement. It allows for more
complex decision-making logic by
evaluating multiple conditions in a
hierarchical manner.
Department of Industrial and
Information Technology
Exp:
Department of Industrial and
Information Technology

 REPETITION STATEMENT
- allow a block of code to be executed multiple times based on a
condition.
Department of Industrial and
Information Technology

• THE FOR STATEMENT


A. Initialization
B. Condition Expression
C. Expression List
Department of Industrial and
Information Technology

A. Initialization
-Initialization sets the variable to a new instance. It must be to a type that is
compatible with the declaration type.

Example:
Department of Industrial and
Information Technology

b. CONDITION EXPRESSION
This is the condition that must be true for the loop to continue executing.
Example:

The loop stops when this Condition


evaluates to false.
Department of Industrial and
Information Technology

c. EXPRESSION LIST
-This is executed after each iteration of the loop, often used to update the loop
counter

Example:
Department of Industrial and
Information Technology

• THE FOR LOOP STATEMENT


When you know exactly how many times
you want to loop through a block of code,
use the for loop instead of a while loop
Department of Industrial and
Information Technology

• THE WHILE STATEMENT


It is used when you want a
code block to run repeatedly
WHILE a Condition is met.
Department of Industrial and
Information Technology

• THE DO - WHILE STATEMENT


Same as the While Loop but it
EXECUTES the Code Block first
before checking the CONDITION
Department of Industrial and
Information Technology

• THE FOR - EACH STATEMENT

There is also a foreach loop, which


is used exclusively to loop through
elements in an array (or other data
sets):
Department of Industrial and
Information Technology

• THE BREAK STATEMENT


In C#, we use the break
statement to terminate the
loop. As we know, loops iterate
over a block of code until the
test expression is false.
However, sometimes we may
need to terminate the loop
immediately without checking
the test expression.
Department of Industrial and
Information Technology

• THE CONTINUE STATEMENT


The continue statement starts a
new iteration of the closest
enclosing iteration statement
(that is, for, foreach, while, or
do loop), as the following
example shows:
Department of Industrial and
Information Technology

• AN EMPTY STATEMENT
The empty statement consists
of a single semicolon. It does
nothing and can be used in
places where a statement is
required but no action needs to
be performed.
Department of Industrial and
Information Technology

 QUIZ
1. What is a statement in programming?
A. A command that performs an action
B.A type of variable
B. A function that returns a value
D. A comment in the code

2. Which of the following is used to make decisions in the code based on certain conditions?
A. Declaration Statement
B. Expression Statement
C. Control Flow Statement
D Conditional Statement
Department of Industrial and
Information Technology

3. (True or False): All programming languages have the same syntax for conditional
statements.

4. What is the purpose of a conditional statement?


A. To declare a variables
B. To perform actions based on conditions
C. To define functions
D. To comment code

5. Which of the following is NOT a type of conditional statement?


A. If statement
B. Switch statement
C. Loop statement
D. Else statement
Department of Industrial and
Information Technology

6. In an if - else statement, what happens if the condition is false?


A. The if block executes
B. The else block executes
C. The program terminates
D. An error is thrown

7. What keyword is used to define a block of code that executes when a condition is true?
A. else
B. if
C. switch
D. case
Department of Industrial and
Information Technology

8. Which of the following is an example of logical operator?


A. +
B. &&
C. =
D. %

9. What does the switch statement do?


A. It executes a block of code based on multiple conditions.
B. It loops through a block of code.
C. It defines a function.
D. It declares a variable.
Department of Industrial and
Information Technology

10. What is the output of the following code?

A. Greater
B. Lesser
C. Error
D. None
Department of Industrial and
Information Technology

11. Which of the following statements is used to exit a loop or switch statement?
A. break
B. continue
C. return
D. exit

12. What is the result of the following expression: true && false?
A. true
B. false
C. null
D. undefined
Department of Industrial and
Information Technology

13. , Which keyword is used to define an alternative block of code in an if statement?


A. else
B. else if
C. switch
D. case

14. What is the purpose of the ‘else if’ statement?


A. To define a loop
B. To check multiple conditions
C. To declare a variable
D. To comment a code
Department of Industrial and
Information Technology

15. Which of the following best describes a nested conditional statement?


A. A conditional statement inside another conditional statement.
B. A statement that does not require a condition.
C. A loop that contains a conditional statement.
D. A function that returns a conditional value.
Department of Industrial and
Information Technology

REFERENCES:
https://www.w3schools.com/
https://www.programiz.com/csharp-programming/expressions-statements-blocks#:~:text=A%20statement%20is%20a%20basic,both%20lines%20above%20are%20statements.
https://www.scaler.com/topics/csharp/control-statements-in-csharp/
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/statements
https://www.geeksforgeeks.org/loops-in-c-sharp/
https://www.programiz.com/csharp-programming/expressions-statements-blocks#:~:text=C%23%20Blocks,enclosed%20inside%20curly%20brackets%20%7B%20%7D.
https://www.scholarhat.com/tutorial/csharp/conditional-statements-in-csharp
https://www.informit.com/articles/article.aspx?p=1701416&seqNum=8
https://www.pluralsight.com/resources/blog/guides/declaring-and-initializing-variables-in-c#:~:text=Initialization%20sets%20the%20variable%20to,compatible%20with%20the
%20declaration%20type.&text=In%20the%20above%20code%2C%20the,achieves%20precisely%20the%20same%20result.
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator
https://www.tutorialsteacher.com/csharp/csharp-while-loop
https://www.tutorialsteacher.com/csharp/csharp-do-while-loop
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/jump-statements
Lambda Expressions (C# Programming Guide)
Anonymous Methods (C# Programming Guide)
Action and Func Delegates (C# Programming Guide)
Expression Trees (C# Programming Guide)
The System.Linq.Expressions Namespace
Department of Industrial and
Information Technology
Lambda Expressions in C#
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements/expressions-
operators/lambda-expressions

Anonymous Methods in C#
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements/expressions-
operators/anonymous-methods

Using Delegates in C#
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/inside-a-program/delegates

Expression Trees in C#
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/expression-trees/

https://www.c-sharpcorner.com/UploadFile/8af593/conditional-statement-in-C-Sharp/
https://www.youtube.com/watch?v=wxznTygnRfQ
Department of Industrial and
Information Technology

ANSWERS:
1. A
2. D
3. False 10. A
4. B
5. C 11. A
6. B 12. B
7. B 13. A
8. B 14. B
9. A 15. A
Department of Industrial and
Information Technology

THANK YOU!

You might also like