codeasy .net
or Sign Up

Explorer

The day started like usual - get up, eat a meager meal, drink some weak coffee. Then Ritchie called for me; he said he had a new task. "I need a key from the Grand Tower," he said, "and that gives you a good chance to advance to Level 5 in the Resistance."

They told me that everything I could want is located inside the resistance base, but even now, it was starting to feel a bit constricting. I decided to agree to Ritchie's task, figuring it would give me a good chance to see what surrounded the base. It was a bit unnerving to leave my underground home, but I had been training for a while now, and I was ready to put my skills to the test against the cruel outside world.

Before I made it 100 meters outside the gate, I heard a hushed voice.
"Hey, you! What are you doing here?"

c gang

The C Gang

"Easy there," I said, "I don't want any trouble." I didn't expect to run into anyone, particularly this close to the Resistance base, but I tried my best to hide the fear in my voice.

"Are you a time traveler? I heard that they have a new one. Man, do they torture you?" the man asked.

"No," I said, realizing now that the stranger was more curious than dangerous, "far from it. In fact, they're teaching me C# programming!"

I heard a chuckle from the man in the shadows. "Let me guess, do they tell you about comments, naming styles, and namespaces?" he asked in a mocking tone.

"Well... yes," I replied, figuring he had more to say on the subject.

"I tell you what. Screw classes! Screw namespaces! Give your program a choice!" he said.

Now was my time to be the curious one. What an odd way to talk about coding, I thought. His excitement for the subject alone made me want to know more. "Why don't you like classes? A choice? What do you mean? What choice are you talking about?"

He replied, "I'm from the C gang, kid. In case you didn't know, C is a programming language itself, without your fancy sharp part! There are no classes or namespaces in C. Why constrict yourself to such complicated nonsense? We let the program have a damn choice!"

Now his excitement was starting to wear on me. I wondered if he had a history with the Resistance, whether they had rejected him, or thrown him out at some point. "Give me an example." I said. "Show me what you mean."

The man took out a small tablet. While it was slowly booting up, he said "Here, this code is same for C and C#. I'll show you a clear example. But it should be a secret, man. Only between you and me, man. Understand?"

c sharp joke

"Alright," I agreed, figuring that a little knowledge couldn't hurt.

"Start here!" he said, "You can execute different code depending on a condition."

if (condition)
{
    // Code block 1
}
else
{
    // Code block 2
}

The code looked like more of a conversation than any of the C# I had learned so far. "What does this do?" I asked.

"It gives the program a choice, like I said! This construction is called an operator if. If the condition is true, the first code block is executed. If the condition is false, the second code block is executed. Choice!" he exclaimed.

"Can you show me a working example?" I asked, not quite absorbing the idea of a program having a choice.

He began poking around on the tablet to find another program. "I've got plenty!" he said.

if (5 < 6)
{
    Console.WriteLine("Executing this code");
}
else
{
    Console.WriteLine("Executing another code");
}

Outputs:

Executing this code

"Aha, I see!" I said. "This code checks whether it's true that 5 is less than 6, then outputs 'Executing this code' to the screen."

"You got it!" he said, clearly thinking I had become a full-fledged convert.

Although I wasn't entirely sure where all this would end up, I played along for the moment. "And say I change the 'if' statement to something that is wrong, like 10 < 6, then it would output 'Executing another code'?"

Nodding, he gestured at the tablet. "Try it yourself!"


Change the code to get the output "Ifs in C# and C are working exactly the same!"



"Well done, man! In conditions, you can use all kinds of operators." Here are just some of the possibilities:

  •   <   less than
  •   >   greater than
  •   <=  less than or equal to
  •   >=  greater than or equal to
  •   ==  equal to
  •   !=  not equal to

"You can even use multiple in the same line." he said. "Here - try an example of using 'not equal to':"

if (144 != 12 * 12)
{
    Console.WriteLine("Math doesn't work in this world, kid");
}
else
{
    Console.WriteLine("Yo! Surprise, man!");
}

Outputs:

Yo! Surprise, man!


"Ok," I said, "so this code checked if 144 is not equal to 12 times 12. If that statement was true, it would have output 'Math doesn't work in this world, kid'; but because 144 is equal to 12 times 12, it output 'Yo! Surprise, man!', right?"

"Yes!" he said, even more excited than I at this point. "Damn, you're catching on fast!"

Having all but forgotten the task that I had set out to do for the day, I had to admit that this simpler way of coding was appealing on the surface. But I didn't yet understand where this could lead. "What's the point of comparing two numbers in the code," I said, "if I already know if the comparison is true?"

He was ready with an immediate answer, and I realized that this was the point he had been building towards since he first called out to me. "You can compare variables, man! You can also compare strings and string variables, not only numbers. This was just a basic example. Look here, kid."

if ("I love programming" == "I hate programming")
{
    Console.WriteLine("Love is hate and hate is love");
}
else
{
    Console.WriteLine("Love is not hate!");
}
// Outputs: Love is not hate!
string name = "Teo";
if (name == "Teo")
{
    Console.WriteLine("The key of the 5th Level from Grand Tower is: 42");
}
else
{
    Console.WriteLine("No permission to the key");
}
// Outputs: The key of the 5th Level from Grand Tower is: 42
int a = 10;
int b = 10;
if (a == b)
{
    Console.WriteLine("This way if a equals b");
}
else
{
    Console.WriteLine("This way if a and b are different");
}
// Outputs: This way if a equals b
int temperature = -10;
if (temperature < 5)
{
    Console.WriteLine("Wear a coat");
}
else
{
    Console.WriteLine("Wear a jacket");
}
// Outputs: Wear a coat

"Choice!" I exclaimed, understanding now. "Now my programs have a choice!"

"That's the truth, kid. Solve the following tasks to show me that you understand."

if statement choice


An inattentive worker swapped a mother's age with her son's. Write an if statement that fixes this mistake. The program should output the following:
Mother's age is 40.
Son's age is 10.



You need to change the code to find a minimum among two integer numbers. It should output: Minimum is 100.



"You got it!" he said approvingly. "See how much simpler this C is without the fancy sharp? Forget about classes and namespaces - come join our C gang! Travel around the world with us and hack computer systems!"

It was certainly tempting at that point. After all, what did I really owe to the Resistance? They had pulled me out of my past to serve in their future war. Carefully, I said "I respect you man, and I respect C. But I still have some things to learn from the Resistance."

"Ok, kid," he said, gearing up for a final sales pitch. "Before you go, let me tell you how an experienced C programmer would write if statements. You can use it in C# as well, yo!"

"I'm all ears!" I said.

"You can skip the 'else' statement if there is nothing to write there. You can also leave out brackets {...} if there is only one command line inside. Here's an example."

if (motherAge < sonAge)
{
    int temporary = motherAge;
    motherAge = sonAge;
    sonAge = temporary;
}
else
{
    // Currently not used
}

Is equal to:

if (motherAge < sonAge)
{
    int temporary = motherAge;
    motherAge = sonAge;
    sonAge = temporary;
}

And, for a final example:

if (a == b)
{
    Console.WriteLine("Here if a equals b");
}
else
{
    Console.WriteLine("Here if a and b are different");
}

Is equal to:

if (a == b)
    Console.WriteLine("Here if a equals b");
else
    Console.WriteLine("Here if a and b are different");

"Thanks," I said, "I'll think about your offer."

"Practice, practice, practice!" he said, "Practice is the mother of talent!"



Write a program that calculates the minimum number of your drones needed to destroy enemies. Make it simple - your Main method should not contain brackets {...} or use an 'else'.



Write a program that compares strings in variables myName and bestProgrammerEverName. If they are equal, output "Happiness!" to the screen.



This site uses cookies. By continuing to browse this site you are agreeing to our use of cookies.

Got it!