3 C# Operators & Conditionals

 

This chapter covers

  • Introduction tooperators and expressions in C#
  • Types of operators and expressions
  • Operator associativity and precedence
  • Introduction toConditional Selection statementsin C#
  • Types of Conditional Selection statements
  • Development process of a random number comparator

In the previous chapter, we made a bit more of a custom-made project. We learned about variables, datatypes, and the random class. We also learned about concatenation and how we can use it to get custom results. However, for now, we only got to the point where we displayed a predefined value. Even though including randomness, we just displayed a value between two set numbers. We have not yet started implementing any logic into our program.

In this third chapter, we will finally add some complexity to our algorithm. Using operators and conditional statements, we will be able to get a specifically selected result depending on our preset situations.

For our third tiny C# project, we will generate two random numbers and then compare them to determine which one is bigger. Then, we will check if the bigger one is higher than a set value and display in a Console.WriteLine() the result of both conditions.

This algorithm will use everything we learned previously, such as datatypes, the random class, concatenation, and everything we will learn during this chapter.

Let us start this chapter with a bit of theory, with the definition of an operator in C#.

3.1 What are Operators and Expressions

 
 

3.2 Types of Operators

 
 

3.3 Operator priority

 

3.3.1 Operator precedence

 
 

3.3.2 Operator associativity

 
 

3.3.3 Operand evaluation

 
 

3.4 Conditional statements

 
 

3.4.1 The if statement

 
 
 

3.4.2 The ternary operator ?:

 
 

3.4.3 The switch statement

 
 
 

3.5 A random number comparator

 
 
 

3.5.1 The project

 
 
 
 

3.5.2 Our algorithm

 
 

3.6 Summary

 

3.7 References

 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest