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

C# Laborator 2

The document contains examples of different C# programming concepts including: 1) Getting user input from the console using ReadLine and displaying it. 2) Conditional logic using if/else statements to compare numbers. 3) The conditional operator. 4) A switch statement to evaluate different cases. 5) A do-while loop to continuously display a menu until the user selects "Quit". 6) A for loop example using break and continue.

Uploaded by

Alex
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)
62 views

C# Laborator 2

The document contains examples of different C# programming concepts including: 1) Getting user input from the console using ReadLine and displaying it. 2) Conditional logic using if/else statements to compare numbers. 3) The conditional operator. 4) A switch statement to evaluate different cases. 5) A do-while loop to continuously display a menu until the user selects "Quit". 6) A for loop example using break and continue.

Uploaded by

Alex
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/ 5

C# Laborator 2

1. Preluare date de la tastatura:


using System;
namespace GettingInputDemo
{
public class Program
{
public static void Main()
{
string name;
int age;
double height;
Console.Write("Enter your name: ");
name = Console.ReadLine();
Console.Write("Enter your age: ");
age = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter your height: ");
height = Convert.ToDouble(Console.ReadLine());
//Print a blank line
Console.WriteLine();
//Show the details you typed
Console.WriteLine("Name is {0}.", name);
Console.WriteLine("Age is {0}.", age);
Console.WriteLine("Height is {0}.", height);
}
}
}
2. Instructiuni de decizie:
using System;
namespace IfStatementDemo2
{
public class Program
{
public static void Main()
{
int firstNumber;
int secondNumber;
Console.Write("Enter a number: ");
firstNumber = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter another number: ");
secondNumber = Convert.ToInt32(Console.ReadLine());
if (firstNumber == secondNumber)
{
Console.WriteLine("{0} == {1}", firstNumber, secondNumber);

}
if (firstNumber != secondNumber)
{
Console.WriteLine("{0} != {1}", firstNumber, secondNumber);
}
if (firstNumber < secondNumber)
{
Console.WriteLine("{0} < {1}", firstNumber, secondNumber);
}
if (firstNumber > secondNumber)
{
Console.WriteLine("{0} > {1}", firstNumber, secondNumber);
}
if (firstNumber <= secondNumber)
{
Console.WriteLine("{0} <= {1}", firstNumber, secondNumber);
}
if (firstNumber >= secondNumber)
{
Console.WriteLine("{0} >= {1}", firstNumber, secondNumber);
}
}
}
}
3. Operatorul conditional
namespace ConditionalOperatorDemo
{
public class Program
{
public static void Main()
{
string pet1 = "puppy";
string pet2 = "kitten";
string type1;
string type2;
type1 = (pet1 == "puppy") ? "dog" : "cat";
type2 = (pet2 == "kitten") ? "cat" : "dog";
}
}
}
4. Exemplu switch:
using System;
namespace SwitchStatementDemo
{
public class Program
{
public static void Main()
{
int choice;
Console.WriteLine("What's your favorite pet?");

Console.WriteLine("[1]
Console.WriteLine("[2]
Console.WriteLine("[3]
Console.WriteLine("[4]
Console.WriteLine("[5]
Console.WriteLine("[6]
Console.Write("\nEnter

Dog");
Cat");
Rabbit");
Turtle");
Fish");
Not in the choices");
your choice: ");

choice = Convert.ToInt32(Console.ReadLine());
switch (choice)
{
case 1:
Console.WriteLine("Your
break;
case 2:
Console.WriteLine("Your
break;
case 3:
Console.WriteLine("Your
break;
case 4:
Console.WriteLine("Your
break;
case 5:
Console.WriteLine("Your
break;
case 6:
Console.WriteLine("Your

favorite pet is Dog.");

favorite pet is Cat.");

favorite pet is Rabbit.");

favorite pet is Turtle.");

favorite pet is Fish.");

favorite pet is not in the

choices.");
break;
default:
Console.WriteLine("You don't have a favorite pet.");
break;
}
}
}
}

5. Do while
using System;
class DoLoop
{
public static void Main()
{
string myChoice;
do
{
// Print A Menu
Console.WriteLine("My Address Book\n");
Console.WriteLine("A - Add New Address");

Console.WriteLine("D - Delete Address");


Console.WriteLine("M - Modify Address");
Console.WriteLine("V - View Addresses");
Console.WriteLine("Q - Quit\n");
Console.WriteLine("Choice (A,D,M,V,or Q): ");
// Retrieve the user's choice
myChoice = Console.ReadLine();
// Make a decision based on the user's choice
switch(myChoice)
{
case "A":
case "a":
Console.WriteLine("You wish to add an address.");
break;
case "D":
case "d":
Console.WriteLine("You wish to delete an address.");
break;
case "M":
case "m":
Console.WriteLine("You wish to modify an address.");
break;
case "V":
case "v":
Console.WriteLine("You wish to view the address list.");
break;
case "Q":
case "q":
Console.WriteLine("Bye.");
break;
default:
Console.WriteLine("{0} is not a valid choice", myChoice);
break;
}
// Pause to allow the user to see the results
Console.Write("press Enter key to continue...");
Console.ReadLine();
Console.WriteLine();
} while (myChoice != "Q" && myChoice != "q"); // Keep going until the user wants to quit
}
}
6. Exemplu for:
Using System;
class ForLoop
{
public static void Main()
{
for (int i=0; i < 20; i++)

{
if (i == 10)
break;
if (i % 2 == 0)
continue;
Console.Write("{0} ", i);
}
Console.WriteLine();
}
}

You might also like