You are on page 1of 14

1

Computer Fundamentals
Programming
Module 4 – Conditional Statements

IRMA T. PLATA, DIT


ISABELA STATE UNIVERSITY- Main Campus
College of Computing Studies, Information and Communications Technology
Echague, Isabela
When do we use conditional statements?
2

Decisions Evaluate a value


C# Conditional Statements
3

• if statement
• if…else statement
• else if statement
• switch statement
if statement
4

Syntax:
if(boolean_expression)
{
/* statement(s) will execute
if the boolean expression is true
*/
}

Sample Problem #1

Create a C# program that would display


“UNDER-AGE” if the age entered is below 18.
Solution to Problem #1
5

using System;
class my_app
{
static void Main(string[] args)
{
string Age = "";

Console.WriteLine("Enter your age: ");


Age = Console.ReadLine();

if( int.Parse(Age) < 18 )


{
Console.WriteLine(“UNDER-AGE");
}
Console.ReadKey();
}
}
if…else statement
6

Syntax:
if(boolean_expression)
{
/* statement(s) will execute if
the boolean expression is true */
}
else
{
/* statement(s) will execute if
the boolean expression is false */
}

Sample Problem #2

Create a C# program that would display “UNDER-AGE” if the age


entered is below 18, otherwise “OVER-AGE” if age above 18.
Solution to Problem #2
7
using System;
class my_app
{
static void Main(string[] args)
{
string Age = "";

Console.WriteLine("Enter your age: ");


Age = Console.ReadLine();

if( int.Parse(Age) < 18 )


{
Console.WriteLine(“UNDER-AGE");
}
else
{
Console.WriteLine(“OVER-AGE");
}

Console.ReadKey();
}
}
nested if……
8

It is used to evaluate multiple conditions

Sample Problem #3

Create a C# program that would display “UNDER-AGE” if the age


entered is below 18, if age is equal to 18 display “EXACT-AGE”,
otherwise “OVER-AGE” if age above 18.
Solution to Problem #3
9
using System;
class my_app
{
static void Main(string[] args)
{
string Age = "";

Console.WriteLine("Enter your age: ");


Age = Console.ReadLine();

if( int.Parse(Age) < 18 )


{
Console.WriteLine(“UNDER-AGE");
}
else if( int.Parse(Age) == 18 )
{
Console.WriteLine(“EXACT-AGE");
}
else
{
Console.WriteLine(“OVER-AGE");
}

Console.ReadKey();

}
}
10

Computer Fundamentals
Programming
Module 5 – C# Loops

IRMA T. PLATA, DIT


ISABELA STATE UNIVERSITY- Main Campus
College of Computing Studies, Information and Communications Technology
Echague, Isabela
C# Loops
11

• There may be a situation, when you need to


execute a block of code several number of
times. In general, the statements are
executed sequentially: The first statement in
a function is executed first, followed by the
second, and so on.
• Programming languages provide various
control structures that allow for more
complicated execution paths.
C# Conditional Statements
12

• A loop statement
allows us to execute a
statement or a group
of statements
multiple times and
following is the
general form of a loop
statement in most of
the programming
languages
Loop Types and Description
13

Loop Types and Description

while Loop It repeats a statement or a group of statements


while a given condition is true. It tests the
condition before executing the loop body.
For loop It executes a sequence of statements multiple
times and abbreviates the code that manages
the loop variable.
Do…while It is similar to a while statement, except that it
loop tests the condition at the end of the loop body
Nested loops You can use one or more loop inside any
another while, for or do..while loop.
# - While Loop
14

• A while loop statement in C#


repeatedly executes a target
statement as long as a given
condition is true.
• Here, statement(s) may be a
single statement or a block of
statements. The condition may
be any expression, and true is any
non-zero value. The loop iterates
while the condition is true.
• When the condition becomes
false, program control passes to
the line immediately following
the loop.

You might also like