0% found this document useful (0 votes)
0 views20 pages

Conditional Structures in C# Programming Language

The document provides an overview of conditionals and loops in C#, including examples of if, else, else if, switch statements, while loops, do-while loops, and for loops. Each section contains code snippets demonstrating the syntax and functionality of these control structures. It serves as a guide for understanding how to implement conditional logic and looping mechanisms in C# programming.

Uploaded by

Ikeason Lagamayo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views20 pages

Conditional Structures in C# Programming Language

The document provides an overview of conditionals and loops in C#, including examples of if, else, else if, switch statements, while loops, do-while loops, and for loops. Each section contains code snippets demonstrating the syntax and functionality of these control structures. It serves as a guide for understanding how to implement conditional logic and looping mechanisms in C# programming.

Uploaded by

Ikeason Lagamayo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

CONDITIONALS

AND LOOPS

Prepared by: Ms. Maricel M. Prongo


CONDITIONALS
are used to perform various
computations or actions based
on whether a condition is true or
false.
THE IF .....
ELSE
STATEMENT
IF STATEMENTS
using System;

namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
if (20 > 18)
{
Console.WriteLine("20 is greater than 18");
}
}
}
}
IF STATEMENTS
using System;

namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
int x = 20;
int y = 18;
if (x > y)
{
Console.WriteLine("x is greater than y");
}
}
}
}
ELSE
using System;
STATEMENTS
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
int time = 20;
if (time < 18)
{
Console.WriteLine("Good day.");
}
else
{
Console.WriteLine("Good evening.");
}
}
}
}
ELSE
using System;
STATEMENTS
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
int time = 20;
if (time < 18)
{
Console.WriteLine("Good day.");
}
else
{
Console.WriteLine("Good evening.");
}
}
}
}
THE ELSE IF
STATEMENTS
ELSE IF STATEMENT
using System;

namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
int time = 22;
if (time < 10)
{
Console.WriteLine("Good morning.");
}
else if (time < 20)
{
Console.WriteLine("Good day.");
}
else
{
Console.WriteLine("Good evening.");
}
}
}
}
C# SWITCH
STATEMENTS
C# SWITCH STATEMENTS
using System;

namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
int day = 4;
switch (day)
{
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
case 4:
Console.WriteLine("Thursday");
break;
case 5:
Console.WriteLine("Friday");
break;
C# WHILE
LOOP
WHILE LOOP
using System;

namespace SampleForAndrew
{
class Program
{
static void Main(string[] args)
{
int x = 0;
while (x < 5)
{
Console.WriteLine(x);
x++;
}
}
}
}
WHILE LOOP
using System;

namespace SampleForAndrew
{
class Program
{
static void Main(string[] args)
{
int x = 1;
while (x < 5)
{
Console.WriteLine("Value of x:" + x);
x++;
}
}
}
}
THE DO /
WHILE LOOP
DO WHILE
using System;
STATEMENTS
namespace SampleForAndrew
{
class Program
{
static void Main(string[] args)
{
int x = 0;
do
{
Console.WriteLine(x);
x++;
}
while (x < 5);
}
}
}
FOR LOOP
FOR LOOP SYNTAX
for (statement 1; statement 2; statement 3)
{
// code block to be executed

}
FOR LOOP EXAMPLE
using System;

namespace forDemonstrationAndrew
{
class Program
{
static void Main(string[] args)
{
for (int x = 0; x < 5; x++)
{
Console.WriteLine(x);
}
}
}
}
FOR LOOP EXAMPLE
using System;

namespace forDemonstrationAndrew
{
class Program
{
static void Main(string[] args)
{
for (int x = 0; x <= 10; x = x + 2)
{
Console.WriteLine(x);
}
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy