Professional Documents
Culture Documents
x = 123; // initialization
int z = x + y;
Console.ReadKey();
using System;
namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
// constants = immutable values which are known at compile time
// and do not change for the life of the program
Console.WriteLine(pi);
Console.ReadKey();
}
}
}
static void Main(string[] args)
{
// type casting = Converting a value to a different data type
// Useful when we accept user input (string)
// Different data types can do different things
double a = 3.14;
int b = Convert.ToInt32(a);
int c = 123;
double d = Convert.ToDouble(c);
int e = 321;
String f = Convert.ToString(e);
String g = "$";
char h = Convert.ToChar(g);
String i = "true";
bool j = Convert.ToBoolean(i);
Console.WriteLine(b.GetType());
Console.WriteLine(d.GetType());
Console.WriteLine(f.GetType());
Console.WriteLine(h.GetType());
Console.WriteLine(j.GetType());
Console.ReadKey();
}
}
}
using System;
namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("What's your age?");
String name = Console.ReadLine();
Console.WriteLine("What's your age?");
int age = Convert.ToInt32(Console.ReadLine());
Console.ReadKey();
}
}
}
{
class Program
{
static void Main(string[] args)
{
int friends = 5;
friends = friends + 1;
//friends += 1;
//friends++;
//friends = friends - 1;
//friends -= 1;
//friends--;
//friends = friends * 2;
//friends *= 2;
//friends = friends / 2;
//friends /= 2;
Console.WriteLine(friends);
Console.ReadKey();
}
}
}
{
class Program
{
static void Main(string[] args)
{
//if statement = a basic form of decision making
if (name == "")
{
Console.WriteLine("You did not enter your name!");
}
else
{
Console.WriteLine("Hello " + name);
}
Console.ReadKey();
}
}
}
namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
// switch = an efficient alternative to many else if statements
switch (day)
{
case "Monday":
Console.WriteLine("It's Monday!");
break;
case "Tuesday":
Console.WriteLine("It's Tuesday!");
break;
case "Wednesday":
Console.WriteLine("It's Wednesday!");
break;
case "Thursday":
Console.WriteLine("It's Thursday!");
break;
case "Friday":
Console.WriteLine("It's Friday!");
break;
case "Saturday":
Console.WriteLine("It's Saturday!");
break;
case "Sunday":
Console.WriteLine("It's Sunday!");
break;
default:
Console.WriteLine(day + " is not a day!");
break;
}
Console.ReadKey();
}
}
}
{
class Program
{
static void Main(string[] args)
{
// && (AND)
// || (OR)
Console.ReadKey();
}
}
}
{
class Program
{
static void Main(string[] args)
{
// while loop = repeats some code while some condition remains true
Console.ReadKey();
}
}
}
{
class Program
{
static void Main(string[] args)
{
// Count up to 10
for (int i = 1; i <= 10; i++)
{
Console.WriteLine(i);
}
Console.ReadKey();
}
}
}
{
class Program
{
static void Main(string[] args)
{
// nested loops = loops inside of other loops
// Uses vary. Used a lot in sorting algorithms
Console.ReadKey();
}
}
}
{
class Program
{
static void Main(string[] args)
{
do
{
double num1 = 0;
double num2 = 0;
double result = 0;
WriteLine("------------------");
WriteLine("Calculator Program");
WriteLine("------------------");
switch (Console.ReadLine())
{
case "+":
result = num1 + num2;
Console.WriteLine($"Your result: {num1} + {num2} = " + result);
break;
case "-":
result = num1 - num2;
Console.WriteLine($"Your result: {num1} - {num2} = " + result);
break;
case "*":
result = num1 * num2;
Console.WriteLine($"Your result: {num1} * {num2} = " + result);
break;
case "/":
result = num1 / num2;
Console.WriteLine($"Your result: {num1} / {num2} = " + result);
break;
default:
Console.WriteLine("That was not a valid option");
break;
}
Console.Write("Would you like to continue? (Y = yes, N = No): ");
} while (Console.ReadLine().ToUpper() == "Y");
Console.WriteLine("Bye!");
Console.ReadKey();
}
}
}
{
class Program
{
static void Main(string[] args)
{
// array = a variable that can store multiple values. fixed size
cars[0] = "Tesla";
cars[1] = "Mustang";
cars[2] = "Corvette";
Console.ReadKey();
}
}
}
{
class Program
{
static void Main(string[] args)
{
// method = performs a section of code, whenever it's called "invoked".
// benefit = Let's us reuse code w/o writing it multiple times
// Good practice is to capitalize method names (I forgot in this video)
SingHappyBirthday(name, age);
Console.ReadKey();
}
static void SingHappyBirthday(String birthdayBoy, int yearsOld)
{
Console.WriteLine("Happy birthday to you!");
Console.WriteLine("Happy birthday to you!");
Console.WriteLine("Happy birthday dear " + birthdayBoy);
Console.WriteLine("You are " + yearsOld + " years old!");
Console.WriteLine("Happy birthday to you!");
Console.WriteLine();
}
}
}
{
class Program
{
static void Main(string[] args)
{
// return = returns data back to the place where a method is invoked
double x;
double y;
double result;
Console.WriteLine(result);
Console.ReadKey();
}
static double Multiply(double x, double y)
{
return x * y;
}
}
}
{
class Program
{
static void Main(string[] args)
{
// exception = errors that occur during execution
try
{
Console.Write("Enter number 1: ");
x = Convert.ToInt32(Console.ReadLine());
result = x / y;
Console.ReadKey();
}
}
}
{
class Program
{
static void Main(string[] args)
{
// conditional operator = used in conditional assignment if a condition is true/false
//(condition) ? x : y
double temperature = 20;
String message;
message = (temperature >= 15) ? "It's warm outside!" : "It's cold outside!";
Console.WriteLine(message);
Console.ReadKey();
}
}
}
{
class Program
{
static void Main(string[] args)
{
// string interpolation = allows us to insert variables into a string literal
// precede a string literal with $
// {} are placeholders
Console.ReadKey();
}
}
}
{
class Program
{
static void Main(string[] args)
{
// object = An instance of a class
// A class can be used as a blueprint to create objects (OOP)
// objects can have fields & methods (characteristics & actions)
human1.name = "Rick";
human1.age = 65;
human2.name = "Morty";
human2.age = 16;
human1.Eat();
human1.Sleep();
human2.Eat();
human2.Sleep();
Console.ReadKey();
}
}
class Human
{
public String name;
public int age;
////////////////////////////////////////////////////
///////////////////////////////////////////////
namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
// int num = int.Parse("25");
//Console.WriteLine(num + num + "");
//Console.WriteLine("{0}{0}", num);
}
}
}
///////////////////////////////////////////////
namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
WriteLine(asterikLine);
WriteLine("*{0, 20}: {1, -40}*", "Name", userName);
WriteLine("*{0, 20}: {1, -40}*", "Department", dept);
WriteLine("*{0, 20}: {1, -40}*", "faculty", faculty);
WriteLine("*{0, 20}: {1, -40:F2}*", "AvgGrade", avg);
WriteLine(asterikLine);
///////////////////////////////////////////////
using System;
using static System.Console;
namespace _1175Lab1
{
class Program
{
static void Main(string[] args)
{
WriteLine();
//Age Decrement operation postfix
Write("Please enter your age as a whole nunmber: ");
int age2 = int.Parse(ReadLine());
WriteLine("Lets try postfix decrement operator...Age after fix decrement
is {0}", age2--);
WriteLine("Age after the previous decrment operator is " + age2);
//conjunctive concatenation
WriteLine();
string msg = "Hello, ";
msg += "How are you";
WriteLine("New Message is...{0}", msg);
}
// all user-defined class methods are defined outside the Main method
//but must be inside the Program class
//all class methods must be defined as static and must be defined here
//class methods are defined here, and may be called by other methods including
the Main method
}
static void SayHello()
{
string helloMsg = "Hi, Priya"; //variable is local to this method
WriteLine(helloMsg);
}
}
///////////////////////////////////////////////
using System;
using static System.Console;
namespace ChiHangY_Assign1
{
class DogFood
{
public static void Main(string[] args)
{
WriteLine("***************************************************************************
");
WriteLine("* WoofYum Online Dog Food Ordering System
*");
WriteLine("* Place your order now!
*");
WriteLine("***************************************************************************
");
WriteLine("***************************************************************************
");
WriteLine("* Dog Food Brands in stock given below
*");
WriteLine("* Can Unit Price for Zignature: $5.25
*");
WriteLine("* Can Unit Price for Royal Canin: $4.99
*");
WriteLine("* Can Unit Price for Purina: $3.99
*");
WriteLine("***************************************************************************
");
WriteLine();
WriteLine();
WriteLine("Let us begin by entering the number of cans you need need for
each of these dog food brands...");
WriteLine();
WriteLine("Enter the number of cans for Zignature: ");
int can1 = int.Parse(Console.ReadLine()); // ReadLine to get user input
WriteLine("The number of cans for each brand name has been entered");
WriteLine();
WriteLine();
Start:
WriteLine();
WriteLine();
WriteLine("What would you like to do?");
WriteLine("Press 1 for View Order, Press 2 for Update Order, Press 3 for
quitting the application:");
int num = int.Parse(ReadLine()); //ReadLine getting user input
if (num == 1)
{
double result; //declare variables
double a = can1 * 5.25;
WriteLine("**********************************************************");
WriteLine(" BrandName: Zignature ");
WriteLine(" UnitPrice: $5.25 ");
WriteLine(" NumCans: " + can1, "" );
WriteLine(" BrandTotal: ${0:F2}", + a ); //F2 is
2 decimal points, and $ is dollar sign
WriteLine();
double b = can2 * 4.99;
WriteLine(" BrandName: Royal Canin ");
WriteLine(" UnitPrice: $4.99 " );
WriteLine(" NumCans: " + can2, "" );
WriteLine(" BrandTotal: ${0:F2}", +b );
WriteLine();
double c = can3 * 3.99;
WriteLine(" BrandName: Royal Canin ");
WriteLine(" UnitPrice: $3.99 ");
WriteLine(" NumCans: " + can3, "");
WriteLine(" BrandTotal: ${0:F2}", +c );
WriteLine();
WriteLine();
result = a + b + c;
WriteLine(" Total before discount: ${0:F2}", result);
WriteLine("**********************************************************");
}
}
goto Start; //Jumps to "Start:"
}
else if (num == 2)
{
WriteLine("Updating your dog food order");
WriteLine();
WriteLine();
WriteLine("Press 1 to update number of cans for Zignature");
WriteLine("Press 2 to update number of cans for Royal Canin");
WriteLine("Press 3 to update number of cans for P" +
"urina");
WriteLine("Enter the number (1, 2 or 3): ");
int update = int.Parse(ReadLine());
if (update == 1)
{
}
else if (num == 3) // if user press "3" , it will go to the end right
away.
{
WriteLine("Thank you for placing the dog food order. Good Bye!");
}
}
}
}
///////////////////////////////////////////////
static void Main(string[] args)
{
int x; // declaration
x = 123; // initialization
int z = x + y;
Console.ReadKey();
}
}
}
///////////////////////////////////////////////
static void Main(string[] args)
{
// type casting = Converting a value to a different data type
// Useful when we accept user input (string)
// Different data types can do different things
double a = 3.14;
int b = Convert.ToInt32(a);
int c = 123;
double d = Convert.ToDouble(c);
int e = 321;
String f = Convert.ToString(e);
String g = "$";
char h = Convert.ToChar(g);
String i = "true";
bool j = Convert.ToBoolean(i);
Console.WriteLine(b.GetType());
Console.WriteLine(d.GetType());
Console.WriteLine(f.GetType());
Console.WriteLine(h.GetType());
Console.WriteLine(j.GetType());
Console.ReadKey();
}
}
}
///////////////////////////////////////////////
Console.ReadKey();
}
}
}
///////////////////////////////////////////////
friends = friends + 1;
//friends += 1;
//friends++;
//friends = friends - 1;
//friends -= 1;
//friends--;
//friends = friends * 2;
//friends *= 2;
//friends = friends / 2;
//friends /= 2;
Console.WriteLine(friends);
Console.ReadKey();
}
}
}
///////////////////////////////////////////////
static void Main(string[] args)
{
double x = 3;
double y = 5;
Console.WriteLine(a);
Console.ReadKey();
}
}
}
///////////////////////////////////////////////
using System;
namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
//if statement = a basic form of decision making
// -------------------- Example 1 --------------------
if (name == "")
{
Console.WriteLine("You did not enter your name!");
}
else
{
Console.WriteLine("Hello " + name);
}
Console.ReadKey();
}
}
}
///////////////////////////////////////////////
using System;
namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
// switch = an efficient alternative to many else if statements
switch (day)
{
case "Monday":
Console.WriteLine("It's Monday!");
break;
case "Tuesday":
Console.WriteLine("It's Tuesday!");
break;
case "Wednesday":
Console.WriteLine("It's Wednesday!");
break;
case "Thursday":
Console.WriteLine("It's Thursday!");
break;
case "Friday":
Console.WriteLine("It's Friday!");
break;
case "Saturday":
Console.WriteLine("It's Saturday!");
break;
case "Sunday":
Console.WriteLine("It's Sunday!");
break;
default:
Console.WriteLine(day + " is not a day!");
break;
}
Console.ReadKey();
}
}
}
/////////////////////////////////////////////////
using System;
namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
// && (AND)
// || (OR)
///////////////////////////////////////////////
using System;
namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
// while loop = repeats some code while some condition remains true
Console.ReadKey();
}
}
}
///////////////////////////////////////////////
using System;
namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
// Count up to 10
for (int i = 1; i <= 10; i++)
{
Console.WriteLine(i);
}
///////////////////////////////////////////////
using System;
namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
// nested loops = loops inside of other loops
// Uses vary. Used a lot in sorting algorithms
Console.ReadKey();
}
}
}
///////////////////////////////////////////////
using System;
namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
Random random = new Random();
bool playAgain = true;
int min = 1;
int max = 100;
int guess;
int number;
int guesses;
String response;
while (playAgain)
{
guess = 0;
guesses = 0;
response = "";
number = random.Next(min, max + 1);
if (response == "Y")
{
playAgain = true;
}
else
{
playAgain = false;
}
}
Console.ReadKey();
}
}
}
///////////////////////////////////////////////
using System;
namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
do
{
double num1 = 0;
double num2 = 0;
double result = 0;
Console.WriteLine("------------------");
Console.WriteLine("Calculator Program");
Console.WriteLine("------------------");
switch (Console.ReadLine())
{
case "+":
result = num1 + num2;
Console.WriteLine($"Your result: {num1} + {num2} = " +
result);
break;
case "-":
result = num1 - num2;
Console.WriteLine($"Your result: {num1} - {num2} = " +
result);
break;
case "*":
result = num1 * num2;
Console.WriteLine($"Your result: {num1} * {num2} = " +
result);
break;
case "/":
result = num1 / num2;
Console.WriteLine($"Your result: {num1} / {num2} = " +
result);
break;
default:
Console.WriteLine("That was not a valid option");
break;
}
Console.Write("Would you like to continue? (Y = yes, N = No): ");
} while (Console.ReadLine().ToUpper() == "Y");
Console.WriteLine("Bye!");
Console.ReadKey();
}
}
}
///////////////////////////////////////////////
Console.WriteLine(result);
Console.ReadKey();
}
static double Multiply(double x, double y)
{
return x * y;
}
}
}
///////////////////////////////////////////////
using System;
namespace MyFirstProgram
{
class Program
{
static void Main(string[] args)
{
//params keyword = a method parameter that takes a variable number of
arguments.
// The parameter type must be a single - dimensional array
Console.WriteLine(total);
Console.ReadKey();
}
}
}
///////////////////////////////////////////////