Syntax:
WriteLine() will print the output into a new line where as
Write() will just put it onto the same line
If you don't use the using System line, you would have to write System.Console.WriteLine() to print/output text.
C# is also case sensitive so capitals and non capitals can make a huge difference.
Comments:
Comments are useful as they allow you to explain you code within the code and or keep you code organised so you can explain what a certain function does. All you need to do to make a comment in C# is put a // before you comment.
Variables:
Variables are used to store data for example an Int will store whole numbers, a String stores text when in double quotes and a bool will store true or false values.
To declare a value you must state your variable name and the value (int or bool)e.g.
Distance=int;
or
LightSwitch=bool;
Data types:
a data type determine the size and type of variable values, it keeps your code maintainable and readable. The most common types of data types are int, long, float, double, bool, char and string. To create a variable with numerical:
long myNum = 1500000000L;
Console.WriteLine(myNum);
Type casting:
Implicit casting is automatic and converts a small type to a larger type size.
Explicit casting is manually and coverts a larger type to a smaller size type.
User input:
To allow user input you need to create a variable to store the data in for able this string for a username input.
Console.Write.Line("Enter username:");
String userName = Console.ReadLine();
Console.WriteLine("Username is: " + userName);
Operators
Operators are used to perform operation on things like values and or variables e.g.
int x=100+50;
other operators include
+ = Addition
- = Subtraction
* = Multiplication
/ = Division
% = Modulus
++ = Increment
-- = Decrement
Math
The math class has many ways that will allow you to do mathematical tasks on numbers e.g.
To find the lowest or highest value you would use
Math.Min(1, 10);
=1
Math.Max(1, 10);
=10
or to find the square root
Math.Sqrt(64);
=8
or to round a number
Math.Round(55.99);
= 56
Strings
Strings are used to contain a collection of characters when they are contained by "" e.g.
string greeting = "Hello world";
Booleans
Booleans are a data type that has only two values e.g.
Yes/No
True/Flase
On/Off
Conditions and If Statements
Uses logical conditions from mathematics e.g.
Less than = Apples > Oranges
Greater than= Apples < Oranges
Less than or equal to= Apple <= oranges
Greater than or equal to= Apple >= Oranges
Equal to= Apple == Oranges
Not equal to= Apple != Oranges
IF statement is a block of code to be executed if the condition is True e.g.
IF (30 > 15)
{
Console.WriteLine("30 is greater than 15")
}
Switch
Switch statement to select one of many code blocks to be executed. The switch expression gets evaluated once and is compared with values of each case and if there is a match the block of code with be executed.
While Loop
This will loop through the block of code as long as it is true e.g.
int i = 0;
while(i < 5>
{
Console.WrittenLine(i);
i++;
}
For loop
A for loop is similar to a while loop but its for when you know exactly how many times that you want to loop through the code.
Statement 1 is executed (one time) before the execution of the code block.
Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has been executed.
for (statement 1; statement 2; statement 3) { // code block to be executed }
Statement 1 sets a variable before the loop starts (int i = 0).
Statement 2 defines the condition for the loop to run (i must be less than 5). If the condition is true, the loop will start over again, if it is false, the loop will end.
Statement 3 increases a value (i++) each time the code block in the loop has been executed.
Break and Continue
a Break can be used to stop looping by putting
break;
under something like a while loop.
to do a continue
continue;
and then it will continue into the next iteration loop.
Arrays
Arrays are used to store multiple values in a single variable instead of having separate variables for each individual value. Arrays can also be used for many things like sorting out number, you can have a data set and by using an array you can get the lowest, highest and total sum of your data.
namespace MyApplication { class Program
{ static void Main(string[] args) {
int[] myNumbers = {5, 1, 8, 9}; Console.WriteLine(myNumbers.Max()); // largest value Console.WriteLine(myNumbers.Min()); // smallest value Console.WriteLine(myNumbers.Sum()); // sum of myNumbers } } }
Should output 9, 1, and 23.
コメント