The Value of the Masks

OK. Once again, I’m not a scientist, an epidemiologist, a doctor, etc. But I have a personality quirk that compels me to research interesting (to me) topics and then write about them. You may feel free to ignore my post, or take it with a grain of salt. But as usual, I will try to provide the best information I can have. If you can find ample evidence to contradict me on this, please feel free to share. I love learning (weird for a person who struggled with school grades, I know) and information and ideas always intrigue me enough to listen.

I’ve seen a LOT (and I mean an insane) amount of posts regarding face masks and the corona virus on Facebook. Some of them have been pretty well reasoned, even if somewhat flawed or incomplete. In some cases, they’re memes with the line “I’ll wait”. People post these things up and think they’re so snappy, or witty… and in most cases, they’ve tried to distill a topic that is fairly elaborate and complex down to a sentence or two, and overlook a lot of facts that actual change quite a bit.

Face masks have been a particularly complexly debated topic. Are they effective against the coronavirus? Are they helping or harming us? What is the point of using them? Which ones work, which ones don’t, etc.

I can’t offer you sound medical guidance, but I can try to explain the scientific reasoning behind the face mask. There is sound medical logic, but you need to understand that it’s not something you can sum up with a sentence; not unless you understand that the sentence will likely only answer a single direct question, and not provide details on a more complicated question on their own.

So to understand the issue here, people are asking “what’s the point of wearing a face mask?”.

Do they offer any protection against the coronavirus or any other viruses? Do they present more of a risk or hazard than they prevent? Why do we need to do social distancing if we have face masks? Why do we need to wear face masks if we have social distancing. Now I’m going to try to break this down as best I can, and I’m not going to call you an idiot for questioning this. If you’ve waited for your explanation, this is it. It’s long; it’s not hard to understand, but it involves a LOT of moving pieces to fully appreciate. So let’s get started.

The short version is: the face mask does NOT guarantee you you won’t get sick from a virus. It’s true that many virus particles on their own are much smaller than most filtration masks handle. But the virus particles do not travel on their own. They can survive on certain surfaces for extended periods of time, some are resistant to UV exposure (sunlight). But to travel, most of the particles that make up a virus need to travel through the air in an aerosol form. And this is where we need to understand what the masks are trying to accomplish.

Here’s an experiment to conduct. It’s fairly simple, might only cost a couple of dollars, but it will teach you a valuable lesson. Get a spray bottle; the type that you put a household cleaner in, and fill it with water, and add a bit of food dye. Set up a piece of poster board outside. Now, measure out distances of 1 foot, 6 feet and 25 feet. Move to the farthest point out (25 feet), aim the sprayer at the cardboard, and spray. Nothing’s appeared on the poster board, unless you have a REALLY strong gust of wind heading in that direction. And even then, it’s unlikely to carry that far. Move to the 6 foot range, and repeat. You’ll probably seem some light speckling on the poster board, maybe even some moderate spots appear (depending on how strong a spritzer you have). Now, do the same thing at one foot. Obviously this close you’ll see a big spot appear on the poster board.

So what happened? Plain and simple, the particles from your spritzer dissipated too much at 25 feet to make it to your target. At 6 feet, the particles were larger, so the concentration of the particles was much greater and could hit the target better; however, the fact that the concentration was so tight means that a smaller obstacle could block a fair amount of it. At 1 foot, the concentration is VERY tight, but again, that means a smaller obstacle will catch and prevent it from passing through to the target.

Now, repeat the sequence of tests, but put a washcloth or dishrag just in front of the nozzle. If the rag gets soaked after a spray, move it so that you have a fresh, dry bit of cloth in front of the nozzle.

The spray will almost entirely end up caught by the cloth or dishrag, and very little if any will end up on the target at 6 foot. A bit more might get through and hit the target at 1 foot, but in all likelihood you’ll see less particles from the spray through the mask at 1 foot than you would without it at 6.

For one more test, you can repeat the test by putting the piece of cloth in front, and then holding a piece of wood in front of it. The piece of wood simulates an arm, especially around the inside of the elbow. Again, the amount of spray, if any, that reaches the target, will be VERY minimal.

Now some people might say “Well, yeah, but you moved the cloth in front of the nozzle each time. I can’t carry enough masks to keep switching them.” Yes, that might seem like cheating. But look at it like this: how often do you have a sneeze that’s as high in content of moisture as a spray bottle? Probably you don’t sneeze with the volume of liquid coming out. Honestly, if you do, it means a high volume of mucus and other bodily fluids coming out. Without a face mask, you might sneeze into your hands… or the mucus may be coming out of your nose or even (this is always gross) your mouth… and do you just leave it there? No, you go and get some tissue paper, and white it off. Probably if it was fairly bad, you’ll wash your hands and face. After all, isn’t that what we tell our kids in most cases? Wash your hands if you sneeze into them. More often than not, now, we try to teach them to sneeze into the crook of their arms whenever possible. And we tell them not to give someone a hug after that; forget the virus part – it’s just gross to get a hug and be covered in goop, right?

OK, so there’s less force coming out of your typical sneeze than a spritzer bottle, and that means that you’re not pushing out the volume of moisture that the bottle does. If there’s a smaller volume of moisture to begin with, suddenly those obstacles are made much more effective.

So now we have a reason for using some sort of barrier over our mouth or nose.

The next big thing that people bring up is that wearing the mask spreads the disease, because people tough their faces, or the mask, or whatever, and then touch other things, other people, etc. Yes, there is that risk; but the quantity of virus particles making it through a half-way decent mask is already greatly reduced. Further, touching your face, even on the mask, doesn’t spread the virus until you start touching lots of other things. That’s why stores and a lot of public places have stands and dispensers for sanitizer around, and you’re encouraged to apply a bit every so often. This isn’t a guarantee to stop this virus (or any virus), but it’s a form of redundancy to mitigate the risk.

Look at it this way: most cars now have safety belts, various air bags, and crumple zones designed into to collapse certain parts of a vehicle, while providing greater integrity to the passenger compartment. Safety belts on their owns usually save lives. Admittedly, sometimes they have cost lives, but over the years, as some accidents have happened, engineers and designers have researched improved ways of making the seat belts safer, and the potential for injury or death from the safety belts has generally gone down. Air bags have had the same history. Early designs could have some dangerous flaws – it took careful consideration, sometimes redesign, to fix those issues and mitigate risk factors. Crumple zones have failed, or just not been strong enough to properly handle impact. Again, designs were looked at, testing was performed, the ideas were improved upon.

Any one of those safety devices on their own might save you, or might cause you higher risk. The combination of those safety tools help to reduce the overall risk to a much greater degree. And the reality is that you may not be able to save everyone from a deadly accident. If a train hits your Honda Civic, you’re not looking at a high chance of successfully walking away. But if you’re a reasonably safe driver, and practice good defensive driving habits, and if other people are doing the same, the risk factor for deadly accidents is reduced, and the outcome of those accidents will likely turn more towards people surviving, possibly unharmed.

Yes, wearing a face mask could cause some of the germs to spread. That’s not wrong. But proper hygienic practice means that you combine wearing the mask with other actions to reduce the risk. You wash your hands or use sanitizer whenever possible, you wash your mask or replace them daily, and you have to actively push yourself to be aware of your actions more. This doesn’t outright stop the disease, but it helps to mitigate a LOT of the risk of spreading it.

Now some people still ask: why do we need to practice isolation or social distancing if we wear the masks? And if we wear the masks, why do we need to perform isolation and / or social distancing. If you look back to the experiment, the further you were from the target, and the more obstacles you put in front of the “source of a sneeze”, the greater the reduction in the spread of the virus particles. The guidelines that the CDC and WHO put out were based on the idea that the more space between people, and the better the barrier protection, the greater a reduction in potentially spreading the germs. To be honest, 6 foot might not quite be enough to entirely eliminate the spread. An article I read recently said that the truth is that a distance of 25 or more feet is probably closer to the necessity to fully eliminate that risk. The reason for the 6 foot rule, though, was to cut down the risk as much as possible, while looking at the reality of space concerns. Supermarkets can’t possibly guarantee being able to space things out so that we can always be 25 feet from everyone else. In fact, in most public places, you can’t do that; outdoors it’s a bit different, but if you’re outdoors, chances are that the UV light of the sun will help to kill more of the particles. And remember that UV light rays can still penetrate cloud cover to some extent, so they do get through to help out, even on a cloudy day.

So why is the government mandating lock-down even outside if we have all these precautions? Good question: first, we’re not really in a true lock-down, nationally. In a real lock-down, you wouldn’t be allowed to leave your house… at all… for any reason. The steps the government took in most cases was to issue an order to remain at home as much as possible, to avoid unessential travel, and to avoid crowded spaces. In some very specific instances, there were period were some locations had stricter requirements. For example, in New Jersey where some friends of mine live, there were restrictions that you had to fill out a form to keep with you when you went out for an errand, such as getting groceries, or picking up a loved one from work. The reason for this was simple. The government saw that people weren’t taking the guidance voluntarily, so they needed to temporarily enact some laws to mandate behavior, because they’re trying to look out for the best interest of the population.

And that leads to the second part of the answer. The issue with how viruses spread is that it’s not usually the severely sick who spread it; it’s those with minor symptoms or no symptoms that risk spreading the disease. Similar to how vaccines for diseases like Polio work, if you have 100 people, perhaps 90 percent of people could be infected and never have known. The virus isn’t strong enough to even temporarily give them any noticeable symptoms. Another 5 – 7% will show some symptoms, but be able to resist it, and their bodies can recover from it. The last 3 – 5% may fall into the category of immuno-compromised. They can’t fight off any infections, or if their bodies are fighting with that, a secondary illness can come in and lead to fatality. That’s how AIDS leads to death a lot.

Now people who are immuno-compromised can’t even get a vaccine; they could get ill even from an inactive version of any virus being in the body. While their body is trying to eliminate the virus particles, which would normally let the body “learn” how to protect itself, the body is too exhausted to start dealing with a second illness which is active, and that can be lethal. However, people without a compromised immune system can fairly easily “learn” that method of killing / removing the virus particles. If you give them the vaccine, the body learns rapidly to fight the infection, and then can actually kill and remove it rapidly; rapidly enough so that it can’t survive long enough for you to pass it on. If enough of the people who can safely receive the vaccine do, it maximizes the chances of preventing the virus from being carried around, and therefore infecting (and potentially killing) the people who cannot.

The masks are meant to do the same thing. If the water particles from your sneeze or cough are too big to actually pass through your mask, then they can’t get into the air and travel far enough to get to other people. If you’re practicing social distancing, that risk is further reduced. If you’re avoiding public places and adhering as much as possible to the rules to avoid gatherings and crowds, you’ve reduced that risk further, still.

The other things about viruses is that they can mutate. So far COVID-19 seems to mutate slowly, so the risk of this isn’t high. But enough people passing it around, and enough people’s systems fighting it off, it could lead to changes that cause new strains of the virus. By minimizing the risk of exposure while a vaccine does not exist for this virus, we reduce the chance of it being mutated. Then, if / when there is a vaccine, most people’s bodies will eliminate it and flush it from their system; if it’s happening rapidly in most people, the chances of it changing are reduced even more.

Getting back to the masks, they help to contain the virus if it is being expelled from your body. If you follow proper guidelines, you have contained most of the virus; you’ve kept distant enough from people to mitigate the risk of a few smaller droplets getting out into the atmosphere; by not being out unnecessarily, you have both allowed for people to not have to go into crowded spaces and risk spreading the disease even more; if we do this for long enough, we may but the time necessary for a safe vaccine to develop and be deployed.

Now while this has lead to a lot of tough situations (people being laid off from jobs, small companies struggling vastly, etc), it was really a matter of making sure that this was contained as much as possible while long-term solutions could be found; and we couldn’t be sure exactly how dangerous this was right away. However, given the nature of how this virus could effect people, the concern and caution was wise – it might not have been completely necessary, but what if we didn’t take these actions and were wrong? As of today (May 19, 2020) this virus has killed 320,000 worldwide. 92,000 of them here in the U.S.A. Now what if we hadn’t taken the precautions, including wearing the masks when we went out in public? There is the potential that the virus could have spread far more, and more people who are at risk could have become symptomatic – including those with critical conditions. Our health system has finite resources, it is entirely possible that some hospitals might have been overwhelmed by cases of this virus; and then the risk is that people with other conditions and injuries could NOT get the help they need. That leads to other people who could potentially have a dangerous issue turn fatal. That could have lead to a much higher death toll. The typical flu season in the United States is considered to run from November through April (sometimes coming on as early as October, and lasting as long as May). That means the flue season, when the risk of infection is high and the degree of fatalities is also high, is 6 – 8 months. You can look at these estimated numbers on flu deaths vs infections. The word season in the last 10 (not including 2019 – 2020) the worst was the estimated value in the 2017 to 2018 season, at 61,000. That is higher than the rest of the years by a margin of at least 10,000 – the next highest year was 2014 – 2015. Source: https://www.cdc.gov/flu/about/burden/index.html So in a typical 6 month span, the worst outcome was 61,000 – just over 10,000 a month. In the US alone, to date, the death rate was 92,000. And the actual dates being considered start only back on January 22nd. 3 months, 27 days. Half the length of the typical flu season, and 50% more deaths than the typical flu – and that’s WITH the extensive protective measures in place. There’s no way to be sure what might have happened if this virus went on a full 6 month cycle, and even more, no way to know how bad it could have been had we not put the practices into place to protect people.

So the government largely erred on the side of caution.

There are other viruses that have the potential to spread and cause wide-spread devastation. Ebola is a good example – it takes a number of days to incubate (grow) and then it is extraordinarily dangerous; the mortality rate, depending on the strain of the virus, is anywhere from 25% – 90%. And the average means about 50%. Think that over. Take half the people you know (including family members) and eliminate half of them. And since the incubation period for Ebola is about two weeks, that means that you could be spreading it in all that time. The fortunate thing about Ebola is that it isn’t airborne – you can’t easily spread it by sneezing or coughing. It usually requires blood or saliva; it’s far more vulnerable to UV light, it’s not resilient enough for most environments, and the source of the virus is believed to be a region that doesn’t have a high degree of air travel – namely nations in central and mid-northern Africa. We’re lucky about that.

So the conclusion to this: the masks won’t stop you from catching the disease – they stop you from spreading the disease if you do catch it, especially being important if you’re lucky enough to be symptom-free. If the population at large all take the necessary steps, then few people if any are spreading the disease before their body can kill and flush it. That means people who have a harder time with illness are less likely to be exposed and infected (because many of them take these measures of staying home or distant as a regular course of action) and we’ve bought that much more time to find solutions that will let us resume our daily lives. I know it sucks. I want to go out and take train photos and videos. I miss a lot of social activities like going to the train club I belong to, going to movies and going to parks. I’m lucky I have a job that allows for me to work from home. I know a number of people who have made their entire living around a small business that they started and have run for years – and quite a few of them have minimal resources to survive during this time. Some of them are watching their work and effort crumble. I do know what that’s like. I’ve had a small business of my own, before. And I’ve seen the effects for myself on how fast a lack of work can lead to collapse. I won’t tell them they’re wrong for wanting to have their life’s work survive. Most of them are not rolling in the dough; but they chose the line of work that they did because they enjoyed it. If we really care about helping them out, we’ll need to jump back to the opportunity to make use of their services once again when this is over – that may be tough, because a lot of people will still be struggling with their own finances after being furloughed, perhaps laid off permanently… a lot of people are going to feel the economic crunch. It’s not selfish of small business owners to want to prevent that; but that means that the population at large needs to follow the guidelines set out for prevention as quickly and as much as possible, until we can get that better solution.

That’s not a guilt trip – that’s a fact. I doubt anyone wants to see others die. It’s not about you being at fault; it’s about you contributing to a solution.

I hope that this article has given you some better insights. I hope that you’re able to understand better what the masks are for. You can call it a sign of subservience that could lead to eroding of your rights. The Declaration of Independence was set out with the line:

We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty and the pursuit of Happiness.

https://en.wikipedia.org/wiki/United_States_Declaration_of_Independence

Life is the most precious of these rights. It’s not about “infringing on the rights” of others; it’s about promoting the chance for others to experience things. If you’re frustrated, if you’re upset, if you’re angry, it’s OK – the situation is frustrating, upsetting, and being angry about it is perfectly natural. You can’t be told it’s not any of that reasonably. It is about trying to prevent a much larger and riskier situation. It’s unlikely that we’ll have this go on for many years. Too many resources are already being directed to eliminating the risk. It’s also unlikely that another virus like this could crop up quickly. Even if there is, we’ll have a better understanding of what we can do to prevent the spread, and the next time, if people participate in the efforts like masks and social distancing to a greater degree, early on, the more severe lock-down strategies might not come forward. We might be able to go on in a more regular fashion, allowing for time to stop pandemics.

Try to think of this as a chance to contribute to a solution for the welfare of more people. And when we’re passed this, make sure you do as much as you can to help out those small businesses that were suffering through this. They are worthy of saving, just like our lives.

That G Looks Shady…

Folks, it’s time for a bit of education. I have seen at least 50 posts this morning about 5G signals and how they’re contributing to the Coronovirus. This is the same thing that happened with swine flu, bird flu and SARS in conjunction with the release of 4G and 3G cell phone services in the past decade. So I’m going to explain a few things with me. I’m sure some of you won’t believe it, but I’m going to go with the assumption that many of you will bother to read through this article and STOP TO THINK for a moment about what I’m saying. I don’t work for a cell phone company. I don’t work for a telecommunications company. I have no stock in those companies. I’m not actually profiting from any of this, so when I say “you can trust that I have no ulterior motive”, it is something you can actually believe.

I am a software engineer; I’ve studies computer systems and all manner of technology related to communication – not just what is in use today, but what has been a part of communications on this planet for the better part of 100 years. I make my living writing software at an insurance firm; software that is used for tracking quotes on commercial insurance. Prior to that I’ve done software for banking, I’ve done software for medical, software for shopping carts, websites for promoting a seamstress, a website for a restaurant, and a website for a confectioner (candy store). I’ve taught technology related to video production, 3D animation, editing photos, making websites, writing video games, and designing buildings.

The computer systems and communications degrees might sound like the most important factors here, but the truth is, the most important factor in my ability to explain this comes from the fact that I can read and write. So with that said, let me explain this to you – it’s not hard, and it’s important to know what you’re really looking at.

Radio waves, the ones that carry radio signals, are a form of non-ionizing radiation. What is non-ionizing radiation? Non-ionizing (or non-ionizing) radiation refers to any type of electromagnetic radiation that does not carry enough energy per quantum (photon energy) to ionize atoms or molecules—that is, to completely remove an electron from an atom or molecule. Read that carefully. Non-ionizing radiation does NOT carry enough energy to remove an electron from an atom or molecule. For any form of change to a substance to take place, you need to alter the composition of the elements that make up that substance. Changing the state (solid, liquid, gas, plasma) is NOT the same as changing the electro-chemical structure. Something going from solid to liquid to gas to plasma is about how much energy is applied to those substance, but NOT about how many electrons the substance has. In other words, it’s a physical change, not chemical, nor electrical.

That means that radio waves do NOT change anything about the properties of any substance. Why is this important to know? Because the 5G signal from your latest iPhones and Samsung Galaxy devices is, in fact, a radio signal. There are a number of different frequency ranges that are used for 5G service, depending on the service, and the country – and each country generally handles identifying ranges of frequencies which they make available to the various cell phone carriers. So T-Mobile would have one set of frequencies, Sprint would have another, Verizon another, AT&T another… and many of the smaller “budget” carriers then license subsections of the frequency range of a bigger carrier; or they allow the bigger carrier to handle the responsibility of keeping things organized; and the bigger carriers charge more simply because they prioritize their direct carriers to have the best access. That’s why you pay more for the “brand name” service of a cell phone carrier.

But that doesn’t mean that the 5G signal is safe… does it? Well, actually, it might not in-and-of itself, but you need to understand that the same radio frequency concept that drives your cell phone is also driving the FM radio in your car or home. In this case, the FM radio has a range of 88 to 108 MHz, and radio stations all use a specific frequency with about 200 Khz of separation between one radio station and the next; and if they’ve set up properly, they should be smack-dab in the middle of that frequency.

That’s why you have radio stations like: 88.1, 88.3, 88.5, 98.5, 101.3 – notice how they all end in odd numbers? Why is that? Well, let’s say you have a radio station. Your station is assigned to the 93 MHz band, specifically you’re supposed to broadcast on the frequency 93,300 KHz. That means that your signal should be heard between 93,200 KHz and 93,400 KHz. The next station up would be from 93,400 KHz to 93,600 KHz, and that would mean dead center is 93,500 KHz. Now modern car radios are digital, so they tend to lock on specifically to the frequency… So your radio station is 93,300 KHz (or 93.3 MHz), and that means your radio in your car is, theoretically, going directly to 93.3 MHz. In practice, signals can be affected slightly by atmospheric distortion, fluctuations in power to the transmitter, and other forms of radio distortion that make a signal “noisy” – adding static. So your digital receiver will try to adjust itself to lock on to the signal where it is strongest, without leaving that 200 KHz window for your signal. So if, briefly, 93,298 KHz is where the signal lies, the receiver will try to adjust to that frequency to give you a clean signal.

The 88 MHz to 108 MHz signal range means that, with the proper 200 KHz buffer, you’d be limited to 100 stations. Last I checked, there are more than 300 cities in the United States of America alone, and of those 300 cities, all of them have more than one radio station… so how can there be all these stations if there are only 100 stations with of range?

The fact is that radio frequencies don’t necessarily have THAT much range in terms of physical distance. On average, they get around 30 to 40 miles. That’s why you can tune to a station in one city, and drive to a city about an hour’s drive a ways (if you do 60 MPH speed) – and the radio station will either no longer be the one you were listening to, or it’ll simply be static… and maybe an occasional blip of a signal from the one in your home town. At about the 35 mile mark, you’ll start to see a fairly rapid degradation of the signal. It’ll start being fuzzy, full of static, and eventually just drop right out. And if you were listening to a hard rock station, you might now be listening to NPR’s All Things Considered… or just random static. If there is a radio station in your town that is licensed to operate at 93.3 MHz, and one in the next city that also has this frequency, the fact that they are 60 miles apart means that the signals will barely interfere as it is; and commonly the FCC tries to keep things organized so that the signals won’t overlap at all; I mean, I like the idea of crossover music, but Beethoven and GWAR don’t entirely mix.

OK, so great, we understand radio signals for FM. What does that have to do with Cellular signals? The cellular phone services are just in another part of the same range of radio frequencies. Depending on which service and which generation of technology we’re referring to, they could be in a number of different places on the range of frequencies that make up the radio spectrum. They all have one thing in common, though – they are all, still, non-ionizing radiation – and that means, again, that they can’t rip away or add on an electronic to any atomic / molecular construct. And that means, there is nothing changing about the substance. That means that the substance stayed precisely the same! That’s right, your cell phone can’t cause cancer, because cancer is a growth of abnormal cells caused by a change in the atomic / molecular composition of those cells, which can only come from a change of chemical composition which would require either direct chemical interaction or ionizing radiation… and your cell phone doesn’t work on that form of radiation.

Even more importantly, a virus which mutates and can suddenly infect humans can’t have been mutated, manipulated or engineered by a cell phone signal because, once again, those sorts of changes would be chemical in nature – and they would require ionization to cause that sort of reaction WITHOUT a chemical driving the change.

So I’m betting now, some people are saying “but what about radiation from sources like Nuclear Power Plants”. Yes, those forms of radiation are dangerous – potentially lethal, in fact. But that’s not energy on the radio-frequency ranges. The radiation produced in nuclear reactions is, in fact, ionizing radiation. It’s caused from the breakdown of the physical fuel sources with electronics being freed from the fuel to either crash into each other and either causing a fission reaction (where they split) or fusion reaction (where they merge) to generate heat. And the heat is then used to run a steam turbine, which turns a generator, which produces the electrical current that goes out to electrical grid serving your home and business.

The radiation produced from these sorts of fields is ionizing, and VERY dangerous. That’s what made Chernobyl such a scary thing – the ability to contain that ionizing radiation was lost, and the whole of space around it was suddenly alive with a form of energy which can easily change the composition of atoms and molecules. That’s what killed many people, whether rapidly because of the near immediate expose to changes in their body that would be lethal, or through the gradual change of their body structure to create tumors and cancers. Truthfully, none of the deaths happened due to radiation immediately, but due to fire and physical explosion at the plant. The radiation-related deaths took at least several hours to happen; not that it made it less horrible. In fact, those were probably the more gruesome deaths. But I digress.

The fact is that the forms of radiation that come from nuclear power and similar applications is SEVERELY dangerous; but it’s not the same thing as radio-frequency radiation. Radiation is probably the word that scares everyone anyway. Everyone hears it and thinks of “Chernobyl” or “Hiroshima” or “Nagasaki”. But frankly, radiation also means your microwave oven, your stove top, your television set, your furnace, your hot-water heater, and even your own body! OH NO, MY BODY?! MY BODY IS PRODUCING RADIATION? Yes, it is – in the form of heat. In fact, anything generating heat is really generating a form of radiation. That word, radiation – it just means the release of energy. So anything that releases any form of heat, light, sound, or radio waves is, in fact, producing radiation.

OK, so wait a minute. Are you saying that all these radio signals aren’t dangerous? Yes. But aren’t they unnatural? I suppose you could say they are, because of the source… but these same radio waves exist in nature already. Want proof of it? Fine. What is the Arecibo Observatory?

The Arecibo Observatory is a radio telescope in the municipality of Arecibo, Puerto Rico. It is a giant collector of radio signals, used for making astronomical observations of the universe. In fact, it’s commonly thought that telescopes only take “pictures” of things. But what they really do is receive a signal as some form of radiation. Remember that light is a form of radiation as well. Are you afraid of the lamp in your living room giving you cancer? No? Why not? Because it’s a non-ionizing form of radiation. Unless you touch a hot bulb, it can’t hurt you. The light is something that doesn’t damage your body. At least, not that sort of light. Visible light doesn’t cause damage to you. And it’s part of the same sort of spectrum of electro-magnetic radiation. Back to Arecibo, that telescope collects radio-waves that come in from outer space and is used to look for repeating signals. Those signals have pretty much all been naturally occurring signals; radio waves generated by stars, mostly. And those waves have been bombarding the earth since the beginning of the planet’s existence. If radio waves were dangerous, life would never have existed at all. The average star has a lot more power than all of the cell towers in the world combined.

So does that mean there is no government conspiracy involving radio waves and viruses and all that sort of stuff? Yes, sadly, it’s all fantasy.

But wait, what about the weapons that were being designed for use on crowds. Stuff to cause nausea, pain, vertigo…. all that stuff? What about microwave weapons?

Well, I can go into more detail on that if you want at another point, but the short version is: most of the weapons of that nature either used ionizing radiation – which lead to them being outlawed by Protocol I of the Geneva Conventions of 1977, or they make use of things like bright lights which are non-ionizing radiation which simply overload your nerves temporarily. These are physical effects, not chemical effects, and while they are dangerous, they aren’t the cause of radio-waves. They’re an entirely different spectrum of electro-magnetic radiation that works VASTLY differently. Also, if the government really intended to use that sort of stuff, why did some of the systems that were developed and tested in 2003 – 2004 disappear? They weren’t effective; they were energy intensive and not nearly as effective, plus the risk was too high of the wrong folks getting that technology, so it was decided that it wasn’t worth deploying… and that was the end of it. But they don’t work anything like your cell phone or radio.

Well, OK, I suppose your radio can cause nausea, but I blame the music itself, not the radio that broadcasts it.

OK, so let’s sum all of this up.

Cell Phones, FM Radio, Wireless Internet and even Bluetooth technology all use radio-frequency radiation, which is non-ionizing radiation. That means that it cannot cause atoms or molecules to be manipulated into losing or adding electrons, meaning that the structure and makeup cannot change, meaning it CANNOT cause you to get a virus, bacteria, tumor, cancer or anything else.

The only form of communication that can be dangerous in this fashion is lasers, because they light frequencies they use can cause burns to your eyes. So don’t ever look into the source of a laser. They have use as medical tools in restoring site, but they are always operated by doctors under carefully controlled conditions. Some lasers do cause ionizing radiation, but they aren’t effective for communication devices in a general sense, because communication depends on wide area broadcast, and lasers only work effectively with a tightly focused beam. So they won’t likely ever be used for communication that way.

Finally, for all the conspiracy buffs, I have two points to make. First, the problem with the idea of a government conspiracy is that for it to be effective, the concept has to be VERY simple, and involve the fewest number of people to execute said conspiracy. Second, there has to be an outcome that justifies the effort put into the conspiracy – and an outcome that makes it easier to maintain order. Causing chaos that leads to needing to spend more money to help people would be working at odds with the very conspiracy that’s being concerned.

Now, if you do want to learn about a possible conspiracy and cover up related to radio that IS interesting, look into the conspiracy of numbers stations. That is a conspiracy that has been going on for quite some time, and it makes a whole lot more sense.

PS – If you’ve made it this far, I’m attaching a link to the United States Frequency Allocations chart for the Radio Spectrum on Wikipedia. Understand that the signal for 5G in the spectrum is somewhere between the 2 and 6 GHz ranges… and that range varies by country, so if 5G is the supposed problem, then every single part of the spectrum would be a danger, because the allocations might be different in other countries, meaning everyone is being exposed to those frequencies from different sources, so cell phones wouldn’t be the only problem – literally everything would be the problem.

The 2016 radio frequency chart for the United States

Common Concepts of Programming Part 6 – Object-Oriented Programming Concepts 1

Introduction

Before we get into the grit of this topic, I suggest setting up a new project in our HelloWorld solution. We can call the project HelloObjects. Object-oriented programming is a vast topic, and there is a LOT of ground to cover. The actual depth of this topic goes far beyond what I can put into a single article. So we’ll start here just by creating a single object and interacting with it a bit.

Advanced concepts will be covered down the line to build on this topic; so understanding what we cover here is critical to the process in the long term.

What are Objects?

The concept of object-oriented programming is, as mentioned before, vast. But really what it boils down to is the simple concept that every structure in your program is an object. Well, that sounds interest, but what does it mean?

An object, in programming terms, is a programmatic structure that describes a related set of properties (variables) and methods (functions). Additionally, an object can contain other objects. In more advanced cases, objects can also consist of events – notifications that fire when something has happened within or to the object. That’s something we’ll discuss much later in this series of articles.

Objects also provide the ability to support ENCAPSULATION, which is a way of protecting access to the data that is part of the object, restricting direct control over that data, and more. In many languages, including C#, objects are commonly described in the form of a CLASS. A lot of languages use the CLASS structure t collect a series of related properties and methods, and make them available as a complex data type – they can work much like the string or numeric data we already have used. But whereas a string or numeric value is relatively simple, and really only represents a single value, classes typically are intended to be containers of more complex data.

A simple example of an Object

A great way to examine this concept is to define an object that describes a person. You’re a person (or perhaps a cat walking on the keyboard). I’m a person (most of the time, anyway). You may have a child that is an example of a person; a spouse that is an example of a person. Your mom and dad are also examples.

What do we have in common? We all have a first name, a last name, a date-of-birth, and probably a middle name, too. Those values, our names and date-of-birth, are all properties we have in common. We can all communicate. We can all listen. We can all move (or at least we all have a defined capacity to move). An adult person can have a job. A child person can be a student. A much older adult may have a former occupation. These can be properties as well. But let’s start out by talking about the simple common parts of all (or most) people.

Creating a person class

A person, in programming terms, could be defined with a class containing some properties that all people should have in common. So if we wanted to make a simple C# class for a person, it might start out looking like this.

class Person
{
    public string firstName;
    public string middleName;
    public string lastName;
    public DateTime dateOfBirth;
}

The above code indicates that we have a class called person. This class has 4 properties; 3 strings representing names, and a DateTime data type representing dateOfBirth. This code doesn’t represent a specific person, but is essentially a template for what a person consists of. If we had an application that was going to make sure of this, we could then create a variable of type person.

using System;

namespace MyNamespace
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a variable of myPerson; the type of the variable is a Person.
            Person myPerson = new Person();
            //Set value of names
            myPerson.firstName = "John";
            myPerson.middleName = "Michael";
            myPerson.lastName = "Doe";
            //Set date of birth for person
            myPerson.dateOfBirth = new DateTime(2000, 01, 01);

            //Dsiplay back out the person variables values.
            Console.WriteLine("Person's Name: {0} {1} {2}.  Born on {3}", myPerson.firstName, myPerson.middleName, myPerson.lastName, myPerson.dateOfBirth);
            Console.ReadKey();
        }
    }

    // Person class - this is a template of a "person" object
    class Person
    {
        // A property for a person's first name
        public string firstName;
        // A property for a person's middle name
        public string middleName;
        // A property for a person's last name
        public string lastName;
        // A property for a person's date of birth
        public DateTime dateOfBirth;
    }
}

The output of this application would be:

Let’s break down our application example. By now you should recognize that we are using the System library for output. Additionally, you’ll already be familiar with the idea of having a namespace that matches the name of our project: namespace HelloObjects.

And of course, you’ll recognize having a class named Program, with the usual static void main in it. We’ll discuss what’s happening in here in a moment. Let’s jump past it to line 24 and beyond. On line 25, we declare the creation of a class called “Person”.

On lines 28, 30 and 32 we create three string variables; firstName, middleName and lastName. In the terms of object oriented programming, these variables are part of our “Person” object, so we will refer to them as properties of the person object. Finally on line 34, we are creating a property of the type DateTime called dateOfBirth.

You will notice that all of these properties start out with the keyword PUBLIC. We will discuss this keyword and it’s meaning more at a later time. For now, we just need to understand that these properties are directly accessible any time we create a “Person” object. You might also notice that, unlike the “string” keywords, DateTime has a capital D and T, and instead of being blue in Visual Studio, they’ll be green (the colors might be different depending on the theme you selected for appearance, but the significance of a different color is more important than the colors themselves).

A DateTime is a complex type of variable / property. Rather than being something simple like a numeric value, like an INT or FLOAT, or even a STRING, a DateTime is a complex object that has additional properties of its own. Essentially, a DateTime is another type of class, just like our “Person” class. That means it has methods and properties that are part of its structure.

It also should demonstrate to you that an object can actually have other objects as child elements. This is a big deal, simply because it means you can create many different types of structures, which become part of other structures, which in turn become part of other structures, allowing for all sorts of possibilities for complex problem solving.

For now, we’re just treating this as a simple property of our person, and we’ll use it as one more piece of data associated with the Person. I also want you to pay close attention to the fact that our person class does not sit within the program class, but sits separate from it, but still within the same “namespace” container. This means that the Person object is a class that exists independent of the program itself. Later on, we’ll learn how to put these classes in a general space that makes them available in other places in a program.

I also want to note, quickly, that you COULD in theory put the Person class inside of the program class, but that would prevent you from being able to use the person at any point other than inside of the program class. While it might not seem like a big deal, it’s something you should probably avoid doing, as it can lead to all sorts of headaches when you’re trying to solve problems.

Now that we have a definition of a Person object, we’ll go back to the main method for our program, and look at what we do with this object. It’s not a lot, yet, but it’ll demonstrate the basics pretty easily.

On line 10, I create a new “Person” variable called myPerson. We are setting it to a new instance of a Person object. What if we just did the first part and created the myPerson variable, but not the “new” part? Well, nothing would go wrong on that line, but as soon as we tried to set a value of firstName, we’d have a problem. Attempting to assign a value to a property of an object that is not initialized to a new instance of the Person object would give us an error: Use of unassigned local variable myPerson. When we create a variable of a complex type, we need to actually set it equal to a new copy, or sometimes an existing copy, of that object. Without doing this, we’re really saying that we are reserving a placeholder for that type of object, but not really indicating that there is an actual object in that space.

Think of it light a parking space. We say that a parking space is for a vehicle. But if someone tells you to get into the vehicle that is in that parking space, and there is no vehicle there, you have a problem… you can’t get into what doesn’t ACTUALLY exist. So if we were to instead say “This is a parking space with a parked vehicle in it”, we have now initiated the space to have a vehicle that we can then interact with.

So line 10 creates our variable, it creates a new instance or copy of a Person, and we are ready to proceed.

Lines 12 through 14 each set the property of first, middle and last name equal to a value.

Then, we get to line 16. Many of the properties of the DateTime objects are read-only. Year, month, date, hour, minute, second… these are all properties that we cannot directly change. We can only set these values directly when we initialize the DateTime object, which is what I’m doing on that line. I am saying that the value for dateOfBirth will be a new DateTime object with a year of 2000, a month of 01, and a day of 01.

What if you needed to change it later? The simplest solution would be to just set the property equal to a new DateTime with new values. We’ll worry about that sort of complexity at another time.

Finally, we get to line 19, where we print back out the data we stored into our object. Since all of our properties are public, we can just directly pull those values by calling the myPerson object with the .property approach. And of course, line 20 is there so we can see output on the screen without the program immediately exiting.

Conclusion

Although it might not seem it, yet, we just took a MAJOR step in programming. Object-oriented code is a big piece of the underpinnings of most programming languages, especially those used in the software side of development. They are important in hardware development, too, but the value of creating objects and instances will become manifest as time goes on.

The next phase of this process will build on our “Person” object by introducing the idea of “access modifiers”. That is, we start to learn about the significance of the PUBLIC keyword we used above, and start learning the alternative options.

Common Concepts of Programming Part 5 – Loops and Repetition

Introduction

During the prior article, we looked at the basics of using logic to control the flow of our program. The logic involved testing for certain values in variables, and executing different blocks of code based on the outcome of those tests.

This is valuable, to be sure; but what are we to do if we need to repeat a particular segment of code multiple times? Do we want to set the same code into our application over and over? Unless we have a fixed number of repetitions that is very small, this is anything but efficient.

For example, what if we wanted to execute our HelloAdvancedWorld project multiple times without needing to restart it each time? What if we wanted to say hello to multiple people at once? What if we want to force a person to enter a name to say hello to, and we refuse to move forward until they do?

What we’re describing in each of these cases is a case of repetition; and we accomplish the process of repetition in programming by using a loop. For the following lessons, we’re going to set up another new project, this time called HelloLoopWorld. Make it part of the same solution, yet again, and make sure to set it to be the default project, using the drop-down menu next to the Start / Play button. If you need, go back and look at how we did this in the prior article – you should be able to do this in a fairly straight forward manner now.

After that, we’ll be ready to start diving into loops.

What is a loop?

There’s actually only a few things to discuss when it comes to the subject of loops. Essentially a loop is a block of code that repeats until some condition set is met or while some condition set is not met.

In some rare cases, a loop may actually be continuous. This is referred to as an infinite loop. In most cases, this is something that should be avoided; infinite loops can potentially cause a system to lock up, or to run out of available processing power or memory, leading to a crash of the computer. However, there are some instances in programming where such loops are necessary.

For our purposes, we’ll look at normal loops; those that are terminated by condition sets.

Loop Structures

Loops are generally accomplish with one of 3 general approaches: while loops, do-while loops, and for loops. Each of these approaches involves a key word or key words, with a set of brackets used to contain the code that is to be repeated. They key words will be accompanied with a set of conditions inside of a series of parenthesis, similar in many respects to the if statements we encountered in the prior lesson on conditions. In fact, the syntax within the while and do-while loop is identical in every respect to the conditional controls, because a loop’s translation is “if this condition is not met, do this again” or “if this condition still exists, do this again”. So when you have a handle on the if statement, you have a major portion of the while and do-while loops already figured out.

The other loop, the for loop, is a little bit more complex, but ultimately, the approach to coding with them can achieve the same thing. We’ll examine this in more depth later in this article.

It’s also good to note, there are a few other specialty case loops we’ll encounter in more advanced coding later on. A variation of the for loop, called the foreach loop, is found in more intricate programming methods. It’s not really any more difficult than the standard loops we’ll start with, but it requires some more elaborate data work to be of any significance to us.

Additionally, we’ll learn about a more sophisticated topic, RECURSION, which can be a bit tricky to get a handle on, but offers a LOT of valuable functionality with more advanced types of data.

In this lesson, we’ll discuss the FOR loop, but we’ll only do a serious implementation of using a WHILE or DO-WHILE loop. Seeing this form in use will give us a handle on the concept, and being aware that there are other approaches will suffice for a solid starting point.

While Loops

As stated before, loops are simply a method of repeating a set of instructions until some condition is met. And a “while” loop is as simple a loop as possible. The general structure of a while loop translates into “While this condition needs to be tested, do these instructions”. Here’s an example that will “count” to 10, taking a break each time.

//Created a "counter" integer and set it to 1
int i = 1;
while (i <= 10)
{
    Console.WriteLine("I am at {0}.  Press any key to continue", i);
    Console.ReadKey();
    i++;
}
Console.WriteLine("We have completed the loop.  The value of the incrementer is {0}", i);
Console.ReadKey();

You can put the code above into our Main method, and execute it. Each time a line is printed, you can press your enter key, until you see our final message. indicating the loop is completed.

So what happens is that we initiate an integer variable named “i” and set it equal to a value of 1. The next line indicates that we are creating a WHILE loop that will repeat while the value of “i” is less than or equal to 10. The loop consists of writing to the console the value of i, and instructs the user to press any key for the next step.

The method of print the message here is generating a sort of preformatted message. We are indicating that the line will print out message with a placeholder at {0}. By doing this, we can then substitute in a value, by adding that value at the end of the command, before the closing parenthesis. We could do similar things by printing a message like:

Console.WriteLine("This is variable 1 {0}, this is variable 2 {1}", var1, var2);

And the contents of var1 and var2 would get substituted into {0} and {1} respectively.

Back to the loop. We wait for the user to press any key, and then proceed on. In this case, we increment the value of i. Since it is a numeric value (specifically, a whole number) we can either write i = i + 1; or i++; Both of these accomplish the same thing – adding 1 to the current value in ‘i’. Then our condition in the while loop tests again. If the value of ‘i’ is less than or equal to 10, the process will repeat.

This brings up an important lesson as well. This test is happening before each set of instructions. In this case, we started with a value of 1. We print 1, then we increment to 2. The value is tested again, and is less than or equal to 10, so we print 2, increment to 3. Then the value is tested again, resulting in a 3, which is less than or equal to 10. So we print 3, and increment to 4. This continues through the point where we increment ‘i’ to 10. We do the test, and find that ‘i’ is less than or equal to 10, so we print 10, then increment again to 11. Now, the value of i is no longer less than or equal to 10. It’s 11. So we skip past the loop code, print out our “completed” message, and wait for a key press to end the program.

Now, what will happen if we put i++; BEFORE our ConsoleWriteLine to print out the value?

while (i <= 10)
{
    i++;
    Console.WriteLine("I am at {0}.  Press any key to continue", i);
    Console.ReadKey();
}

You can give it a try to see how the output changes; just make sure to restore our application to this before you move on. Our full application should look like this:

using System;

namespace HelloLoopWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            //Created a "counter" integer and set it to 1
            int i = 1;
            while (i <= 10)
            {
                Console.WriteLine("I am at {0}.  Press any key to continue", i);
                Console.ReadKey();
                i++;
            }
            Console.WriteLine("We have completed the loop.  The value of the incrementer is {0}", i);
            Console.ReadKey();
        }
    }
}

Do-While Loops

As mentioned before, the WHILE and DO-WHILE loops are fairly similar in nature. So if we wanted to write the above code with a DO-WHILE loop, the code would look like this:

//Create a new counter integer and set it to 1
int j = 1;
do
{
    Console.WriteLine("I am at {0}.  Press any key to continue", j);
    Console.ReadKey();
    j++;
}
while (j <= 10);
Console.WriteLine("We have completed the loop.  The value of the incrementer is {0}", j);
Console.ReadKey();

The code looks remarkably similar to the regular WHILE loop. In fact, the only major change is that we moved the “while” part to the end of our loop block, and ended it with a semi-colon, then put the word “do” before the loop block. The only other change of any sort is that we changed the name of the variable “i” to “j”, just so we could keep them distinct in the same program. If you put THIS into the application and run it, it’ll do the first set of repeats, then do the second set, and they’ll come out exactly the same.

So what is the point of having two different structures for the exact same thing? Well, the truth is, they’re not EXACTLY the same. to understand what I mean, change the lines for int i = 1; and int j = 1; to both have an initial value of “11”. Right now, the code will look like this (note that I commented the value of i for each integer so I know what the original value is supposed to be).

using System;

namespace HelloLoopWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            //Created a "counter" integer and set it to 1
            int i = 11; // normally set to 1 - changed to 11 for demonstration;
            while (i <= 10)
            {
                Console.WriteLine("I am at {0}.  Press any key to continue", i);
                Console.ReadKey();
                i++;
            }
            Console.WriteLine("We have completed the loop.  The value of the incrementer is {0}", i);
            Console.ReadKey();

            //Create a new counter integer and set it to 1
            int j = 11; // normally set to 1 - changed to 11 for demonstration;
            do
            {
                Console.WriteLine("I am at {0}.  Press any key to continue", j);
                Console.ReadKey();
                j++;
            }
            while (j <= 10);
            Console.WriteLine("We have completed the loop.  The value of the incrementer is {0}", j);
            Console.ReadKey();
        }
    }
}

Now, when you run the code again, you’ll get this:

Wait, the first loop skipped and just gave us the expected message of the incrementer being 11. The second one actually printed a message at 11 and then told us the incrementer as 12. What happened here?

The answer is fairly straight forward. If we initially ran through with the value started at 1, loop 1 would run through, and after i became a value greater than 10, it would skip the loop and print out the post-loop message. When we ran through the second loop in the same fashion, it would start at 1, print the messages through 10, and then when it was greater than 10, print the post-loop message.

When we changed the starting values to 11, the first loop immediately tested and found the value greater than 10, bypassed the loop entirely and went to the post-loop message. On the second loop, the value started at 11, and the message was printed, and then it was tested to see that the value was greater than 10, so it did not repeat.

The short version is, a WHILE loop tests before it ever executes the loop. DO-WHILE loops execute 1 time BEFORE the test is performed. So in summary, a WHILE loop will immediately test before it attempts to run through the looped instructions, and a DO-WHILE loop executes the instructions at least once BEFORE it attempts to test. If you want a piece of code to execute once before you test for some value, use a DO-WHILE loop.

Input Loop Demonstration

Let’s see if we can demonstrate the value of this a bit more. Create another new project called HelloLoopInputWorld. When the project is created, we’ll create some code in the Main method to demonstrate WHILE and DO-WHILE logic.

First, let’s create a variable called myName of type string. Then, use the console commands to request the user’s name, and to read in their input to our variable

string myName;

Console.WriteLine("What is your name?");
myName = Console.ReadLine();

Next, we need a loop to check the value of the input, and determine if it has been populated with a non-blank character.

while (myName.Trim().Equals(string.Empty))
{
    Console.WriteLine("You must enter a name.");
    myName = Console.ReadLine();
}

Finally, we can put in a post-loop command to say hello. And after that, add a ReadKey() command to hold position in the program until we’re done.

Console.WriteLine("Hello, {0}!", myName.Trim());

Console.ReadKey();

If you’ve written your code properly, you should have a program that looks like this; I’ve added comments to clarify each section in relation to the lesson.

using System;

namespace HelloLoopInputWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create Variable
            string myName;

            //Request user enter name
            Console.WriteLine("What is your name?");
            myName = Console.ReadLine();

            //Check if name is an empty string when trimmed, and repeat until it's not
            while (myName.Trim().Equals(string.Empty))
            {
                //Prompt user to re-enter their name
                Console.WriteLine("You must enter a name.");
                myName = Console.ReadLine();
            }

            //Post-loop response
            Console.WriteLine("Hello, {0}!", myName.Trim());

            //Wait for any key to end
            Console.ReadKey();
        }
    }
}

If you run the application a few times, you can test to see what’s happening with the logic. On the first run, enter a value, like your name, or even the word “Test” immediately when prompted. The application will never display the line about how “You must enter a name.” If you run it a second time and hit enter, or a few spaces followed by enter, you’ll notice that it keeps prompting the user until they put in an actual, acceptable value.

Now that we have done a WHILE loop this way, let’s make a DO-WHILE loop as well. We can use the same project for this. After the Post-loop response, but but before the final Console.ReadKey() command, add in a few blank lines.

We can re-use the myName variable since we don’t need to preserve that information for anything else. However, we’ll set the variable to NULL. NULL is a value that means that the myString is set to non-existence, which is actually different from saying it exists, but is empty. Doing this is as simple as sying “myName = null;” Create a DO-WHILE loop, and make the condition the exact same as it was in the WHILE loop. After the loop, we can place our message to print out the name, exactly as we did for the WHILE loop.

//Set myName to non-existent value
myName = null;

//DO WHILE loop
do
{
    //REPEATED CODE IN HERE
}
while (myName.Trim().Equals(string.Empty));

//Post-loop response
Console.WriteLine("Hello, {0}!", myName.Trim());

Now there are a number of ways we could handle getting the data from the user and printing responses. We can have the input read into the variable, and if the input is empty, print out a message. We could use a condition to check if this method is a repeat. and if it isn’t provide a different message. We could do an IF-ELSE approach to checking for what was put in. The truth is, there’s no single way to do it that’s necessarily “better” than another, so long as we keep it as simple as we can.

In this case, since I have already set the value of the variable to NULL before the loop starts, I can use a check on that to change the message that comes out inside the loop. Like so:

//REPEATED CODE IN HERE
if (myName != null)
{
  Console.Write("You didn't enter a name. ");
}
Console.WriteLine("Please enter your name.");
myName = Console.ReadLine();

The entire program should now look like this:

using System;

namespace HelloLoopInputWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create Variable
            string myName;

            //Request user enter name
            Console.WriteLine("What is your name?");
            myName = Console.ReadLine();

            //Check if name is an empty string when trimmed, and repeat until it's not
            while (myName.Trim().Equals(string.Empty))
            {
                //Prompt user to re-enter their name
                Console.WriteLine("You must enter a name.");
                myName = Console.ReadLine();
            }

            //Post-loop response
            Console.WriteLine("Hello, {0}!", myName.Trim());

            //Set myName to non-existent value
            myName = null;

            //DO WHILE loop
            do
            {
                //REPEATED CODE IN HERE
                if (myName != null)
                {
                    Console.Write("You didn't enter a name. ");
                }
                Console.WriteLine("Please enter your name.");
                myName = Console.ReadLine();
            }
            while (myName.Trim().Equals(string.Empty));

            //Post-loop response
            Console.WriteLine("Hello, {0}!", myName.Trim());

            //Wait for any key to end
            Console.ReadKey();
        }
    }
}

If you’ve put together your code properly, when you run it, you’ll be prompted for your name, and then get a response. Then it’ll prompt for your name again. The new approach will Tell you that you didn’t enter a name, and prompt you again, repeating this until there is a non-emtpy, non-null value.

For Loops

FOR loops aren’t really a drastic difference from the WHILE / DO-WHILE loop logic; in general they can accomplish many similar things. Though there are a few, small, key differences in how they work and are used. First, let’s talk about the setup.

FOR loop controls typically consist of 3 pieces of logic: initialization, condition test, and iteration. So rather than having to create an initial value on one line of code, then do a loop for testing that condition on another line of code, and having a place where logic increments a value like a counter on a third line, we can do them all in one comprehensive step. So for example, if we wanted a simple counting loop like so:

int i = 0;
while (i < 100)
{
    Console.WriteLine ("Loop value {0}", i);
    i++;
}

We can instead combine all of these into

for (int i = 0; i < 100; i++)
{
    Console.WriteLine("Loop value {0}", i);
}

These example will produce the same exact code; but there are differences in what a WHILE and FOR loop do to accomplish things. First, in a WHILE loop, the actual iteration (in this case, the incremental increase of an integer) happens as part of the body of the loop, where as in the FOR loop, it’s a part of the loop structure itself. This means that in a WHILE loop, you’re responsible for that step at some point in the logic of the body of the loop itself.

Second, in FOR loops, the logic of the iteration happens after the body of the loop is created, and can not be skipped in any fashion; it’ll be precisely the same increment at each step. In a WHILE loop, however, we can use conditions to change what happens in each loop cycle; we can use this to prevent iteration, or iterate by more than a default value.

Finally, if you write a FOR loop without a condition to test, you can lead to an infinite loop; you might desire this, but you do need to be careful of the impact of such code. Meanwhile, in a WHILE loop, you MUST have a condition to test, or you application will not build.

It’s not uncommon to hear people say that for loops are best when you know a select number of iterations that will be needed for a loop, and while loops are best used for instances where you don’t know what the number of repeats will be, but that’s not entirely true. Most types of iterative objects (objects that can be looped through) provide a mechanism to get the size, or length or some other value indicating the maximum number of times to iterate through something, so in most cases, this isn’t a concern, strictly speaking.

And in most cases, you can actually go with either approach. Realistically, the alternate logic for iterations is probably the most important aspect of such logic. This will become more obvious in later lessons when we work with more advanced data structures. For now, seeing the different approaches is the most important lesson to be learned.

Conclusion

As we wrap up this lesson on the fundamentals of loops and looping, you might wonder why it is that we didn’t do any more detailed examples than what we have so far. We really didn’t implement an example of a FOR loop in a project, we didn’t really do a whole lot with even the WHILE or DO-WHILE loops.

The reason is that we have a few more things we’ll need to cover to make more effective use of these logical blocks. We could write a simple demonstration, and that would be all well and good, but it would be a LOT of work to set up static loops to accomplish very little of value. The next lesson we’ll have will introduce us to working with OBJECTS, and a further lesson will expand on that topic. When we’ve done both of those things, we’ll have more information and have a better use for the techniques of looping.

Common Concepts of Programming Part 4 – Conditions

Introduction

In this segment of the introduction to programming concepts, you’ll learn about the basics of some additional common programming elements by adding in some conditions. When this section is done, you’ll be ready to start discussing more significant programming concepts, and will be able to handle the solid fundamental lessons, building some simple programs that build up core skills.

Hello, World – enhanced

Up to this point, we’ve only worked in a limited fashion with actual code; it was very important to discuss key concepts before we dove right in. Now that we have looked at concepts like data types, we’re ready to expand the function of our HelloWorld program. Rather than modifying the existing code, however, we’re going to add another project into the solution we started, and use that to build a slightly more advanced version of the original application.

Open up Visual Studio 2019. You’ll end up at the launch menu, where you should see the HelloWorld.sln solution we made in our prior lesson. Go ahead and click on that entry to get started.

Our existing application should open up with the source code for the HelloWorld file.

We’re going to create a second project within the solution, and use that to make our expanded program. From the File menu in the upper left hand corner of the window, select New > Project. Since we have already created a Console App (.NET Framework) before, it will show up under “Recent project teamplates”. Click it once to highlight it, and click the “Next” button.

We are presented with the “Configure your new project” screen.

For Project name, enter HelloAdvancedWorld. Change “Solution” from “Create new solution” to “Add to solution”. The location should default to the fold containing the existing HelloWorld solution. The solution name field will be grayed out, but will say “HelloWorld”. Finally, make sure you’re set to the latest Framework. For my system, this is .NET Framework 4.7.2. Click on the “Create” button. A quick note, it is also possible to add projects in using the “Solution Explorer” panel on the right side of the Visual Studio Screen. You simply right click on the solution name, scroll down to “add” and click on “New project”. The process will be nearly the same, though the “Configure your new project” window will look like the one below, instead of the one we saw above.

You should now notice a few changes. First, in the “Solution Explorer” panel, we’ll see our new project, “HelloAdvancedWorld”, along with the existing “HelloWorld”. Each project will have a few elements underneath: Properties, References, App.config and Program.cs. Now at first, it might seem odd that there are two entries for each of these, but realize that each project has it’s own folder in the solution (both in the application and on your hard drive), and the elements of each project are independent of each other.

Additionally, the main toolbar up above now has an additional drop down menu in it. After “Debug” and “Any CPU”, we should now see “HelloWorld”, followed by the play arrow / Start button.

If you click on that drop-down, you’ll see our other project, HelloAdvancedWorld, as an option.

If you change to our new project, you’ll be able to run that project instead of the original one. And you can always change back if you need to. Also note; if you change which project is the Active one in this menu, the title of that project will take on a bold font face in the Solution Explorer.

In the new Project.cs, replace the existing code with this:

using System;

namespace HelloAdvancedWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, world!");
            Console.ReadKey();
        }
    }
}

This is the same as the code from the original project, with two small differences. First, we’ve removed all of the using statements we don’t need. Second, you should see that the namespace on this version is HelloAdvancedWorld. If you recall in the Getting Stated with Visual Studio 2019 article, I mentioned that a namespace is a sort of container for code. The common practice in C sharp and many other C-styles languages is to have a namespace that matches the project name. A namespace allows you to have multiple blocks of code in there which are then all part of the same “package”. The advantage of this is, if you use a meaningful naming convention for your namespace values, if you have a component that has a similar name to a component from another project or library, the C sharp compiler will understand that you mean this specific version; it’s a way of clarifying which blocks of code you are referring to when names of those code blocks could overlap.

For now, HelloWorld and HelloWorldAdvanced are sufficient. In later projects, we’ll look at better ways to organize using the namespace approach, and you’ll be able to see how it benefits you to have unique namespace values.

If we run this program, nothing new is going to happen yet, because it matches the original HelloWorld application exactly. It’s time to change that a bit.

Let’s replace lines 9 and 10 with some new code:

Console.Write("What is your name? ");
string myName = Console.ReadLine();

Console.WriteLine("Hello, " + myName + "!");
Console.ReadKey();

Make sure you switched that drop-down to “HelloWorldAdvanced”, then go ahead and run the project.

You should be prompted for your name. Go ahead and type in your first name, then hit enter.

It might not seem like much, but you actually did accomplish something important. You received input from the user, and you used it to display back out a message. It’s an important first step in building something much larger. Go ahead and exit the application by pressing any key, or closing that window.

So what did we do? Let’s look at the code. I’m only going to focus on the 4 critical lines, but here’s the total code we have.

using System;

namespace HelloAdvancedWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("What is your name? ");
            string myName = Console.ReadLine();

            Console.WriteLine("Hello, " + myName + "!");
            Console.ReadKey();
        }
    }
}

Line 9 – We’re writing out the message “What is your name? “. The extra space character at the end is to allow us to make the input look a bit neater. Note how this line is “Console.Write” instead of “Console.WriteLine”. I mentioned in a prior article that WriteLine automatically adds a carriage return, or a new line character, at the end of the display process. Write does not add on the character. By using the “Write” command, we can then read in the input directly after the display; it’s a nice way of setting up an input form. It doesn’t affect anything, logically, but it’s nice to know we have that sort of option.

Line 10 – We start out by declaring a variable named myName, with a type of “string”. This means we’re creating a placeholder and assigning it an easy to remember name. Next, the = sign indicates that we’re going to store something into that variable. In the simplest of terms, we’ll need to store in a value that is of the string type. Remember that numbers can be included in a string, but if you use them, they’re being used for non-calculation functions; in other words, they hold no significant mathematical value, they’re just more character that we’re going to display.

Line 10 finished off with Console.ReadLine(), followed by a semi-colon. The Console object, part of our System library, allows for both input and output to the command line / console window. In this case, Console.ReadLine() indicates that the program will continue reading in characters until it encounters a carriage return / new line character – indication that you have pressed your enter key. So the entirety of this line reads as: create a new string variable, call it, myName, and use it to hold the value that the Console reads in, up until the point that the user presses the enter key. It might sound like a lot, but the syntax actually is pretty simply if you stop to think about it.

Line 11 is just a blank line, and remember that the computer will just ignore those blank lines and spaces. It’s convenient to us for organizing code, but the computer doesn’t care if you’re not ordering it to do anything there; it just moves to the next instruction.

Line 12 – Now that we have a value for “myName”, we are going to print it out as part of a message. We indicate that we are going to perform a Write to the Console. We’ve used Console.WriteLine before, so let’s take a look at the information that we’re sending in between the parenthesis. “Hello, ” + myName + “!” turns into “Hello, Greg!” if we type in my name when prompted. The data in the parenthesis is a string; but it is one that is concatenating a variable into the rest of a hard-coded string; in other words, we’re using the actual text “Hello, ” and concatenating (or adding on) the value of the myName variable; then we add on another actual text string, the final exclamation point. When working with string, you can concatenate multiple string together by using a + and either a literal string encased in double quotes, or a variable that can output data as a string. It is possible to output actual numeric variables as string as well. There are other ways of printing out the data, using essentially a substitution approach. I’ll cover that in just a little bit.

Line 13 – This should also be familiar from the original HelloWorld application. This command is essentially pausing for more input; the difference is that this time, the input will be any single character. Whatever we press will qualify as a read key, and the program will move to the next instruction. However, given that there aren’t any more instructions, the program will simply end. If we did not include this line, the prior line printing our message would execute and the program would exit immediately. And in all likelihood, no one would have been able to read the message fast enough to know what it said. So this is useful to make sure you can see everything that appears on the screen, and not quit until you’re really ready to do so.

So much code, so little action

It might seem like we had to go through a lot to make that little bit happen; and in some ways, that is very true. However, the fact is that we’ve learned how to process both input AND output, and that covers a big chunk of what we do with programming. Remember that a computer is primarily a device to speed up mundane, repetitious processes, or to handle complex calculations rapidly without error. So being able to read and write output is a major part of the process; having the ability to process data would be of little use if you could never set up what to calculate, or never see the results of those calculations.

And the truth is, as we build more sophisticated programs, some (or much) of the code will be repeatable and reusable, so while a simple program like Hello, World seems like a lot of work for a very minimal payoff, remember that it’s really serving as a foundation for building something more meaningful. Boiling water isn’t exactly exciting or special, but if you don’t learn how to boil water properly, there are a lot of dishes you’d never be able to cook properly, if at all.

Success doesn’t mean having an answer…

So what do we do if a person is prompted for data and just hits enter? Well, under our current program, the output would be: “Hello, “. Well, that’s not really correct, is it? They could put in any sort of value and it would at least seem plausibly correct. But what about if they leave it blank? Maybe we should do something different. Let’s use a condition to handle that.

We’re going to modify our code a bit. Replace the code on line 12 with the following:

if (myName == string.Empty)
{
    Console.WriteLine("Hello.  You didn't tell me your name!");
}
else
{
    Console.WriteLine("Hello, " + myName + "!");
}

Now, run your program again, and when prompted for your name, just press enter.

Now, by not putting in any value, we get a different message. Let’s break down the new code.

Line 12 – This is a test for a condition; commonly, we can use the key word “if” to indicate that we need to test one or more conditions for truth – hence the name “if statement”. If statements are one type of conditional test; we check to see if one or more conditions evaluates to true, and if they do, we will execute the code nested in curly braces following the “if” line.

Now, some languages will let you state “if” followed immediately by the condition. Most proper languages, however, require you to put your test inside at least a single set of parenthesis. So in this case, we’ll check to see if myName is equal to an empty string. “if (myName == string.Empty)” is literally able to translate into “if the value of myName is equal to an empty string”. The double equal sign is critical here. Remember from earlier, we used a single equal sign to set myName to the value of the Console.ReadLine. If you used a single equal symbol during the condition test, you’d be trying to say “if I store an empty string into myName”. First, this would mean that you’re overwriting the value that was already stored in your variable; second, it would conceivably mean that any time you hit this logical test, the result would always be true – you’d always be able to store the variable. Some languages allow this sort of syntax; C Sharp, however, will thrown an error, because it is expecting the conditions in the parenthesis to be something it can evaluate. Storing a variable is an action, not an evaluation, so the compiler won’t even let you do this. A double equal (==) always indicates comparison. The string.Empty portion of our condition actually is a property that translates into a string with no characters. This means that there is technically a string, it’s just that it has no significance. It is possible to have a string variable that is set to nothing (also called null) which has a different meaning. Null values are something we will discuss another time. For now, we’ll move on.

Line 13 is an opening curly brace that is matched by the closing brace on line 15. And the line in between is what will happen when we have met the condition of an empty string.

Line 16 – Else is the alternative, default condition that is assumed if the conditional check is not met. It is a ways of saying “if any other test we did did not succeed, we’ll do this instead”. The else statement must always follow the closing curly brace of a prior “if” condition. Then the bracketed code following it will execute instead of the initial conditional block.

There is a problem with this code, however. If you run the application, a user might decide to hit the space bar instead, and then hit enter. This is a problem; we’re only alerting them that they didn’t enter a name if they have a string that has no characters. We haven’t accounted for a string that has characters that are all blank spaces.

Go ahead and run the application and you’ll see what I mean.

Well, that isn’t any good. What we need to do is test to make sure they’re not just entering a string consisting of blank characters. But there is a problem; that string could be up to 2 billion or more characters in length; and if we want to check for a string with that many possible spaces, it means we’d need to test each case individually. That means having to check for 2 billion possibilities!

Fortunately, we don’t need to do something crazy like that. Instead, we’re going to modify line 12 from this:

if (myName == string.Empty)

to this:

if (myName.Trim() == string.Empty)

Trim() is a method that exists on all string variables, and on all methods or functions that generate a string as a return value. The Trim() method in it’s basic form removes any blank characters before the first non-blank character, and after the last non-blank character. So for example, if you had a string with a value of: ” Test “, the Trim() method would turn that into “Test”, without any spaces before or after the first and last T. By applying that method to our variable, we will only print our hello message if we have a value in myName that consists of visible characters.

Comment your concerns

Let’s be honest; you’re probably not going to carry this website around with you all the time – so you won’t necessarily have my documents ready to explain what we did here handy immediately. If only there was a way to leave a note for ourselves in our programs that explains what we did, but will not actually be treated like instructions for the program itself. Well of course, we can do that!

The notes we want to leave for ourselves are called “comments”. And every language has them in some form. In most C-styles languages, comments can be made in one of two ways. The first, is to put to forward slashes next to each other, like so:

//this is a single line comment

The single line comment starts the moment you have the two slashes, and continues on until the end of that line. The nice thing about this is that you can put them after an actual programming command, like so.

int i = 0; // Initialize the variable to 0

The second option is to use /* and then use */ at the end of the comment. This allows for multiple-line comments. This way, if you have a longer explanation, you can prevent a need to scroll sideways a long distance to read the whole message.

/* This is an example of a multiple-line comment.  It makes it
   very easy to read this comment if you don't need to scroll sideways
   to read all of it.  This is a great way to leave detailed explanations
   in your code.  That's a very good habit to get into. */

All of those lines will be ignored by the compiler now. There is one other great use of the commenting system – it’s good for temporarily disabling a block of code for testing, or to disable a block of code if you’re replacing it with a new block of code, so you can compare what was there previously. Like so:

   /* if (i == 0)
   {
       Console.Write(" Print out a value ");
   } */

   if (i < 1) // Changed to this to handle negative values - less than 0
   {
        Console.Write(" Print out a value ");
   }

Now that you know this, I will give you a piece of advice – DO NOT HESITATE TO LEAVE YOURSELF COMMENTS. Trust me when I say, commenting your code, regardless of the language, regardless of the platform, is an invaluable capability. Never feel that it’s ridiculous to leave yourself a lot of notes. The more effort you put into leaving detailed, meaningful notes, the less of a strain it is later to remember what you did, and why.

Nesting Code

We’ve already discussed nesting code to some extent, using the curly braces { and } to block out code. Let’s take that a step further by getting some additional information from the user and nesting our code to process it. First, comment out the “if… else” block on lines 12 – 19. You can either do a series of single line comments, or use the multi-line comment approach by placing /* at the start of the block and */ at the end of the block.

//if (myName.Trim() == string.Empty)
//{
//    Console.WriteLine("Hello.  You didn't tell me your name!");
//}
//else
//{
//    Console.WriteLine("Hello, " + myName + "!");
//}
/*if (myName.Trim() == string.Empty)
{
    Console.WriteLine("Hello.  You didn't tell me your name!");
}
else
{
    Console.WriteLine("Hello, " + myName + "!");
}*/

One neat trick to make you aware of with Visual Studio is that you can select a bunch of lines, press the control key (Ctrl) and then press the k key, then release and press the c key. This will turn all of the selected lines into comments. By using the Ctrl key, pressing k and then u, you can un-comment those same lines. It’s a handy shortcut to know.

OK. Let’s add a comment in to keep track of out code. On line 10, before the creation of our variable, hit your enter key, so we can put in a new line. Then go back to the new, blank line, and type in a comment that says “Get variable for name”. That should leave your variable creation on line 11. Create an extra line on 12 so we can space out the next block of code. Then add in a console write for “What is your gender (m/f) ?” Go to the next line and leave a comment that says “Get variable for gender”. Finally, add in a line to create another variable of type string, call it myGender, and set it equal to “Console.ReadLine();” When you’re done, the new block of code should look like this.

Console.Write("What is your name? ");
//Get variable for name
string myName = Console.ReadLine();

Console.Write("What is your gender? ");
//Get variable for gender
string myGender = Console.ReadLine();

Next, let’s write some more code to handle using this input. First, we’ll determine the output based on the gender. This will introduce us to a few concepts. We’re going to do a test for multiple conditions. We want to see if the gender is equal to an ‘m’, an ‘f’, or any other value; we will then print out an appropriate message. Here’s the code we can plug in to do this.

//Check if myGender is set to an f or an m
if (myGender.Trim() == "f" || myGender.Trim() == "m")
{
    //Start printing the message for a specified gender
    Console.Write("You specified a gender of: ");
    
    //If gender is f
    if (myGender.Trim() == "f")
    {
        //finish print - female
        Console.WriteLine("female.");
    }
    //If gender is m
    else
    {
        //finish print - male
        Console.WriteLine("male.");
    }
}
//If gender is not f or m
else
{
    //Print alternative message
    Console.WriteLine("You specified a gender of other or did not specify a gender.");
}

Phew! That’s a lot of code, isn’t it? This code should follow the line where we read in the myGender variable. I put in an extra blank line just to keep things a bit easier to read. Go ahead and run the application 3 times. For each run, enter a different value for the Gender question. Try m, f and o. If you want, you can try any other value for the 3rd test. You can even leave it blank.

You should be able to get an answer of male, female and our “other” message. Now, try running it one more time, and this time, make your gender a capital M or capital F. You might see something you don’t want.

We put in M. Why didn’t we get the male message? Our condition was case sensitive. We have a few ways we can deal with this. The first is to do multiple tests, the second will allow us to streamline our coding. But first, let’s talk about our “if” statements.

When we did our first test, we said:

if (myGender.Trim() == "f" || myGender.Trim() == "m")

This translates into: if we trim the value of myGender of blank space and get a value of f, or if we trim the value of myGender of blank spaces and we get a value of m, we’re going to proceed into this part of our logic. The alternative condition(s) will be met about 18 lines later, with the line that simply states “else”. The double pipe characters || indicates a logical “or” statement. On most models of keyboard, the pipe characters are found on the same key as the backslash key \, which is usually near the backspace key. A pair of these characters will always be a logical OR. If you wanted to say AND, you’d use two ampersands instead &&.

So here we’ll checking for an f or m, and a failure to match those values will jump down to the else statement. Inside, we print out the star of our message about the gender. Note that I used the Console.Write and NOT Console.WriteLine – this way we can print the actual answer on the same line. This isn’t necessary, it’s just an aesthetic choice. The only problem here is, a user might put in a capital M or F… and though it’s the same letter, the fact that it’s capital means that our code will not execute.

One way we could do this is to test for those two additional conditions. First, we could modify our initial test to say this:

if (myGender.Trim() == "f" || myGender.Trim() == "F" || myGender.Trim() == "m" || myGender.Trim() == "M")

This works, and if you do that, you’ll have part of the solution we need. Of course, we’ll also need to modify the blocks that finish the print, specifically the condition that tests for female. Our else will handle either option for male. But there is an easier way.

C sharp allow for operations that are “chainable”. In other words, we can have the results of one operation passed to another operation on the same data object. And with data types like strings, there are actually a number of additional methods that we can chain on. In this case, we can use a method called “Upper” or “Lower” to transform all the characters in the string to one case, and then we can compare against just that single case. So we could convert our logic to this, which will be shorter, and yet perform the same exact operation.

if (myGender.Trim().Upper() == "F" || myGender.Trim().Upper() == "M")

We could then do the exact same thing for the further test for printing the female message. Again, we don’t need to do anything for the male message, because we’re not testing an individual condition.

Now, that we have that part done, we can expand our “alternative” answers. What if we wanted to differentiate between non-specified and other? Enter the next step of conditions… else if.

Else if let’s us chain a series of conditional tests, one sequence after another. In this case, we can modify our block to work like this (please note, I am skipping the internal content for the male and female option to focus on the else if portion).

//Check if myGender is set to an f or an m
if (myGender.Trim().ToUpper() == "F" || myGender.Trim().ToUpper() == "M")
{
    //Our logic to clarify M and F still goes in here.
    //I omitted it to keep this shorter.
}
//If gender is not f or m, but was specified
else if (myGender.Trim().ToUpper() != "F" && myGender.Trim().ToUpper() != "M")
{
    //Print alternative message
    Console.WriteLine("You specified a gender of other.");
}
//If gender is not f or m or other value
else
{
    //Print unspecified message message
    Console.WriteLine("You did not specify a gender.");
}

If you run a series of tests, you’ll see our logic split out more, and the answers are a bit more detailed when it comes to the other / no answer options. Likewise, a capital M or F will yield the correct response.

Another way we could write this code would be to split out the Male and Female options, like so:

//Check if myGender is set to an F
if (myGender.Trim().ToUpper() == "F")
{
    //Write female
    Console.Write("You specified a gender of: female");
}
//Check if myGender is set to an M
else if (myGender.Trim().ToUpper() != "M")
{
    //Write female
    Console.Write("You specified a gender of: male");
}
//If gender is not f or m, but was specified
else if (myGender.Trim().ToUpper() != "F" && myGender.Trim().ToUpper() != "M")
{
    //Print alternative message
    Console.WriteLine("You specified a gender of other.");
}
//If gender is not f or m or other value
else
{
    //Print unspecified message message
    Console.WriteLine("You did not specify a gender.");
}

We could also switch the “non-specified message” and the “other” message and simplify the cases as well.

//Check if myGender is set to an F
if (myGender.Trim().ToUpper() == "F")
{
    //Write female
    Console.Write("You specified a gender of: female");
}
//Check if myGender is set to an M
else if (myGender.Trim().ToUpper() != "M")
{
    //Write female
    Console.Write("You specified a gender of: male");
}
//If gender is not specified
else if (myGender.Trim() == string.Empty)
{
    //Print unspecified message
    Console.WriteLine("You did not specify a gender.");
}
//If gender is populated, but not recognized
else
{
    //Print alternative message
    Console.WriteLine("You specified a gender of other.");
}

There is even an option so we don’t need to use the double equal == sign for comparison. There is a chainable method on string objects called “equals” which take in the parameter of a string, or can take the string.Empty property.

//Check if myGender is set to an F
if (myGender.Trim().ToUpper().Equals("F"))
{
    //Write female
    Console.Write("You specified a gender of: female");
}
//Check if myGender is set to an M
else if (myGender.Trim().ToUpper().Equals("M"))
{
    //Write female
    Console.Write("You specified a gender of: male");
}
//If gender is not specified
else if (myGender.Trim().Equals(string.Empty))
{
    //Print unspecified message
    Console.WriteLine("You did not specify a gender.");
}
//If gender is populated, but not recognized
else
{
    //Print alternative message
    Console.WriteLine("You specified a gender of other.");
}

Why are there so many alternative ways to do this?

At this point, you’re probably wondering why all these alternative methods exist for accomplishing essentially the same thing? The answer is that more elaborate conditions to test for are more optimally achieved through the use of alternate tests. Much like you can often describe the route to get between two different places using a number of different streets and routes, you can use different combinations of logic to achieve a desired outcome.

Which is the right way to do it? The answer may annoy you at first, but it’s the one that works. What I mean is, the approach you take needs to be the one that accomplishes what you want, but your best bet is to lay out the conditions on paper, and try to keep the rules as simple as you can. When you do that, you’ll fine that the conditional logic will be easier to handle.

Frankly, that last sample of logic is probably the simplest looking of all the ways I’ve shown you. It kept each condition to its own block, and minimized the number of cases to test for. This might not be the best way to do it in all cases, but given that it’s simple to walk through, this approach works just fine here.

Go ahead and add back in the responses to the name, however you’d like to. You can just uncomment the code we had before, though if you’d like to specify more details, or to play around with the logic some more, go ahead! This will help you to get a better handle on playing with logical conditions.

Our final version of this file will look like this:

using System;

namespace HelloAdvancedWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("What is your name? ");
            //Get variable for name
            string myName = Console.ReadLine();

            Console.Write("What is your gender? ");
            //Get variable for gender
            string myGender = Console.ReadLine();

            //Check if myGender is set to an F
            if (myGender.Trim().ToUpper().Equals("F"))
            {
                //Write female
                Console.Write("You specified a gender of: female");
            }
            //Check if myGender is set to an M
            else if (myGender.Trim().ToUpper().Equals("M"))
            {
                //Write female
                Console.Write("You specified a gender of: male");
            }
            //If gender is not specified
            else if (myGender.Trim().Equals(string.Empty))
            {
                //Print unspecified message
                Console.WriteLine("You did not specify a gender.");
            }
            //If gender is populated, but not recognized
            else
            {
                //Print alternative message
                Console.WriteLine("You specified a gender of other.");
            }

            //If no name was specified
            if (myName.Trim() == string.Empty)
            {
                //Tell us that we didn't remember the name
                Console.WriteLine("Hello.  You didn't tell me your name!");
            }
            //Otherwise
            else
            {
                //Say hello to the name.
                Console.WriteLine("Hello, " + myName + "!");
            }
            Console.ReadKey();
        }
    }
}

It’s a lot of code, and it doesn’t go too much; but we’re actually accomplishing a few things here that make for a good demonstration. At this stage, we have a good starting point for our logical demonstration. We understand a bit about multiple conditions, we’ve looked at how we can simplify that process, and we’ve learned about how we can have multiple cases with a fair amount of ease (using the if…else if…else approach).

Conclusion

So far we’ve expanded our functionality from a very simple hello application that just prints a message into one that processes some input and churns out responses based on what we typed in. Our next step will be to learn about another step of programming; namely loops and repetition. That will be another long lesson, but it’s another major step towards being able to produce more expansive projects.

Common Concepts of Programming Part 3 – Structure

Introduction

In this article, we’ll examine the anatomy of a C Sharp program in a bit of detail, and relate in some of the concepts we’ve discussed prior to this.

A Basic Application

If you recall the prior application we wrote, our “Hello, world” application, we had a bunch of lines of code that were used to make something relatively simple happen. We will use this simply application to understand what elements are essential for an application. The answer is, not much.

Open up Visual Studio and when you are prompted to open an existing project or start a new one, click on the solution for the prior application we created (the one called HelloWorld). You should see our code from last time open again.

using System;
using System.Collection.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
	class Program
	{
		static void Main(string args[])
		{
			Console.WriteLine("Hello, world!");
			Console.ReadKey();
		}
	}
}

This example, which I covered in the article where we set up Visual Studio, is a simple application which opens up a console (command line) window, prints “Hello, world!” and then waits for you to press any key on your keyboard. When you do, the program ends. Simple, right? It’s not fancy, but it demonstrates that applications need to be complex.

In the case of this application, we’re not really intending for anything critical to happen. So let’s look at the actual breakdown of what’s happening, and why.

The first 5 lines of the application all start with “using”. and have some text after it. The first line in Visual Studio will be bright, whereas the other 4 will be more of a gray color. These lines are declarations of libraries that will be used in this particular file of the application. Or rather, these are the 5 default libraries that Visual Studio THINKS we’re likely to need for our application. The 1st line is the only actual one that is used. That’s why it’s shown with lighter text.

Initially when we created the application, the file did not include the lines that start with “Console”. At that stage, all of the “using” statements were dimmed to indicate that none of them was in use. If we remove the 2 – 5 lines and press the play button at the top of our Visual Studio window, the application will run exactly the same as it did the last time, without any errors. So does that mean we didn’t need those lines? Yes… in this case. If you stop the program, try removing the “using System;” line and try running the application again. You’ll get a message that says that the build failed, and will ask if you’d like to run the last successful build. You can answer no and let’s look at the code again.

You should now see a red, rippled underline on both of the Console lines. That red underline indicates that there is an issue with the code. If you hover over it, you’ll see a warning that “The name ‘Console’ does not exist in the current context”.

If you click on the link that says “Show potential fixes”, you’ll get a further menu.

This gives you options for how you can fix your code. The first option is “using System;” and clicking it will re-add the line to the top of the file for the System library.

Now, if you hit the play button again, the application will build and run.

Libraries

You really don’t want to have to create all of the necessary underlying code necessary to print text on a screen. It’s actually a lot of work to build a fundamental base set of functionality like that; it’s a lot of effort, and frankly, there are already sets of code that do the work for you. That’s what a library is. It’s a collection of logic and instructions for how to perform one or more operations, conveniently packaged up for your use.

In simpler terms; all the stuff you need to regularly do are available to use without you needing to manually create it for each project. The System library contains many of the basic functions that are used throughout your application, especially in terms of handling input from a user and displaying output back to them.

Other libraries exist which handle functions of a different nature. When we need those functions, we can add them simply by adding the appropriate library with a using declaration.

The significance of the semi-colon

You may have notices, looking at the code in the demonstration program, that numerous lines ended with a semi-colon. The semi-color character ( ; ) is used as an end-of-instruction indicator. This is actually a common trait in many languages that follow the syntax of the programming language of C – C Sharp is one such language.

The semi-colon is not used on every line. You’ll notice that it’s not at the end of the line that states “namespace HelloWorld” or “static void Main(String args[])”. They are found at the end of all of the using lines, and at the end of the Console lines. These are the lines that actually indicate a step to be taken. Other lines like the namespace or static void main are indicators of a point within a program, or a collection of instructions, but do not actually act as direct instructions themselves.

You’ll come to understand that in time, when you are explicitly indicating a function the computer is to perform at that step, THAT is when you shall need a semi-colon.

Curly Braces

Much like the semi-colon, the curly braces { and } are a special character used for structuring an application. The curly braces are, in fact, a matched set. They indicate a block of code. If you look carefully, you will notice that after we have the namespace HelloWorld text, the next line down is a left-hand or opening curly brace. At the very end of the program is a right-hand or closing curly brace. This is one of the simplest points about organizing your code. These curly braces indicate that everything contained within them is a part of whatever it is that precedes the opening brace.

In other words, the void static main and everything else within the first set of curly braces are a part of the namespace. Additionally, everything within the curly brace set that follows void static main is a part of void static main.

This is known as nesting or blocking – we are indicating that the contents are nested as part of a structure, and that block is nested in a larger structure. The finer details of this we will cover over time, but for now, the most important thing to note is that whatever you open, you must close, and in the appropriate order. The appropriate order is something we’ll cover over time.

One More Pass

So we’re going to look at HelloWorld one more time, and dissect this code with a bit better an understanding of what some of this is. So going back to our full code:

using System;
using System.Collection.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HelloWorld
{
	class Program
	{
		static void Main(string args[])
		{
			Console.WriteLine("Hello, world!");
			Console.ReadKey();
		}
	}
}

Line 1 – indicate that we need the System library

Line 2 – indicate that we need the System.Collection.Generic library

Line 3 – indicate that we need the System.Linq library

Line 4 – indicate that we need the System.Text library

Line 5 – indicate that we need the System.Threading.Tasks library

Reminder: we included the libraries on lines 2 through 5, but we didn’t ACTUALLY make use of them, so they will show in a dimmer coloring in Visual Studio to indicate that they are not in use. We will discuss at a later time when it’s appropriate to remove those statements.

Line 6 – a blank line. This is not a requirements, but it makes it far easier to organize your code if you segment it some. And extra line returns don’t end up meaning anything in this language, so having some extra space might just make it easier to read.

Line 7 – Indicates that all of the code within the next paid of opening and closing curly braces is part of the namespace of this application. We’ll talk about namespaces more at a later time, and once we do in the proper context, you’ll see a tremendous value in them as an organization and containing element.

Line 8 – the opening curly brace for the HelloWorld Namespace.

Line 9 – indicates that this is a class; a sort of advanced data structure object which we will cover in the near future. The class is of type “Program”. Again, this will be important in the near future. For now, just be aware that the line of code is there.

Line 10 – the curly braces indicating all the following content is part of our “Program” class.

Line 11 – a static method named ‘Main’ that returns nothing (void). Inside of the parenthesis are the words string args followed by a pair of square brackets [ and ]. This is an indicator that our ‘Main’ function (actually method) takes in a parameter that is a string – if you recall from the last article, a string is a group of characters. The [ and ] indicate that the value being passed in is something called an array. We’ll talk about arrays more soon as well. As this is a void function, when it is completed, no data comes back from it. There are other options as well when it comes to this.

Line 12 – a curly brace indicating that the following content is part of the “Main” method;

Line 13 – a call to the Console object that is part of our System library, telling it to write a line of Text to the output window. The output is the literal text “Hello, world!”. This method also automatically causes a carriage return (new line) at the end printing the output. There is another version of this method called “Write” which works very similar, but does NOT include a new line at the end. If you were to have another Console.Write command following it, the text would begin immediately after the end of the first block of text, with no space, and no new lines.

Line 14 – this line is a call to the Console object to wait for the first key to be pressed on the keyboard. Once a key is pressed, the program will continue on to the next instructions. This is a simple way to have a “press any key to continue” hold in your program. And you can make it fun for yourself by waiting to see if a friend searches for the “any” key.

Line 15 – end of the structure that belongs to line 11; closing to the open brace on line 12.

Line 16 – end of the structure that belongs to line 9; closing to the open brace on line 10.

Line 17 – end of the structure that belongs to line 7; closing to the open brace on line 8.

Conclusion

Believe it or not, we actually now have enough fundamentals down to start working on a program in more detail. The next lesson of the introduction will be to modify the HelloWorld program to take a bit of user input and then to return output as a result. After that, you’ll be ready to start working on more intricate and advanced topic. Check back soon for part 4 of the introductory articles.

Common Concepts of Programming Part 2 – Data Types

Introduction

In the prior article, I introduced you to some common concepts and terms in programming. In this article, I’m going to expand on that a bit further. You’ll learn about data types and their affect on functions and methods.

What is a Data Type?

Simply put, a data type is an indicator of what sort of values are used in a particular situation. The simplest way to examine this is to consider variables. In the last article, we learned that a variable is a placeholder for some piece (or pieces) of data. It can be any sort of value we wish to store. But we must understand that there are different types of values. This shouldn’t be entirely surprising. Your first name and last name may be similar types of values, but your birthday is NOT.

What I mean is that if you wanted to store your name into some piece of memory on the computer, your first name and last name would consist of similar types of data; characters. You can’t add your name, or subtract your name. You can really only check if your name matches a certain value, or see if your name contains certain characters. However, there are mathematical functions that can be used with your birthday. We can see how many years have passed since you were born, how many days it is before your next birthday, and more.

Data types give us a way to store and process these pieces of data be forcing them to maintain a standard and structure. In that way, we can know what operations we can perform with that data, and just as important, we can know which operations we CAN’T.

Basic Data Types

There are a few common categories of values, and those common categories can then be broken down further into the specific data types that programming languages can handle. It’s important to note that although the general concept of data types apply through programming, some languages will differentiate data types further than others. We’ll talk about that more in a bit, but let’s start with the general categories.

Numeric
Fairly straight forward in concept, numeric data types are exactly what they sound like: those that hold numbers. Whether it’s a whole number (1, 2, 3) or decimal values (0.1, 0.2, 0.3), numeric fields are obviously important because they serve as the foundation of performing computations – and that’s a primary purpose of computer programs.
Character
Character data refers to everything from alphabetic characters (a – z) to punctuation (?,!) to numbers… wait, numbers, too? Yes. Technically, a number can be part of a character data set. In this capacity, you don’t generally use them for mathematical calculations. Your phone number doesn’t play much of a part in a mathematical formula; it doesn’t have to do with alphabetic order, it doesn’t translate into the position of your house… adding 1 to a phone number doesn’t give you any indication of who that number is or where that person can be found; not directly, anyway. So a number as a character is more about recording a value, not about performing a calculation.
Date and Time
Date and time are generally treated as another explicit category of data type, separate from numeric or character data. This is because there are aspects of both numeric and character data combined in these forms of data. You can use dates for calculation, but they can also be about recording a discrete value and not about specific mathematical functions.
Boolean /ˈbo͞olēən/
Boolean values are both a utterly mundane yet very important form of data. They’re utterly mundane in that a boolean value contains one of two values, and that’s it. Their importance is that the two values are TRUE and FALSE. This might not seem like such a big deal, but realistically, they are the basis of every single logical step of a program. Comparing data and determining if it meets certain criteria ultimately always results in a form of a TRUE / FALSE value, and as a result, a boolean value is essentially the indicator of what way to proceed.

Interestingly, you can also look at booleans as a representation of ON and OFF, 1 and 0, YES and NO, and any other bipolar choices. In fact, the entire idea of how computers work is a form of a boolean. Power flowing through a circuit means it is on… this also means that the presence of power is 1, indicates a value that is true, and a value that is YES. If there is no power flowing, it is FALSE, OFF, 0 and NO.

Even more interesting, next time you are in your kitchen or bathroom, take a look at some of your appliances (small ones like blenders, hair dryers, etc). You may notice that the power switch has a 0 and 1 on it, to indicate Off and On. In some cases, the power switch will have a symbol that looks like a 0 with a 1 running through it; those tend to be found on switches that are push button, where as flip buttons tend to have the separate symbols.

Now you have some idea of what they do that. Your appliance is on or off. This will make for a great example later on with our programming.

Specific Data Types

Now that we have a starting idea of how data is defined, we can talk about the specific data types that exist. We’ll start by talking about the data types associated with numbers.

Numeric Data Types

The numeric data types, as described above, cover the range of data used in mathematical calculations. That means everything from simply addition and subtraction all the way to complex formulas like those used in calculus. Don’t worry, we’re not going to make you do all sorts of crazy math problems here. In fact, the whole point of using the computer is to reduce that load to a manageable format.

Integer (Int or Int32)
Integers are whole numbers. That is straight forward enough. The limit to use of integers is that they can only be up to a certain size. That size is a total of 4,294,967,296 values. What that means is that you are limited to what the largest (and smallest) numbers are that you can hold. Integers, as with all numeric fields in programming, are either signed (meaning they have a positive or negative) or unsigned (meaning only positive numbers are possible). If you’re using an unsigned integer, that means you have a range from 0 to 4,294,967,295. If you’re using an unsigned integer, you can hold from -2,147,483,648 to 2,147,483,647.

An integer is created as an INT, and an unsigned integer is created as a UINT
Long (Long or Int64)
A long is a second form of a whole number. The difference is that a long is actually able to handle a much larger range of numbers. The range of numbers allows for up to 18,446,744,073,709,551,616 values. So unsigned long values can go from 0 to 18,446,744,073,709,551,615 ; signed long values can go from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Variables of these types are created as LONG and ULONG.
Short (Short or Int16)
A short is a whole number with a range of 65,536 values. They are divided up similarly to the INT and LONG formats, and can be declared as SHORT and USHORT.
Byte (Byte and SByte)
Not quite as commonly used in every day calculations, the byte is a holdover, to some extent, from the early days of having to manually program hardware. A byte is a block of 8 binary bits. That means a range of values from 0 (represented as 00000000 in binary) to 255 (11111111 in binary). Binary is a separate lesson to work through, so for now it’s enough just to be aware that bytes are a data type that exist. One difference between these and the other forms above is that a byte is unsigned by default, and to get a signed version, you need to indicate sbyte; this is as opposed to all the other types that are signed in their default form with an explicit override for unsigned.
Decimal
Of course, we don’t only deal with whole numbers when it comes to mathematics in programming. Otherwise we’d never do anything with monetary values because, honestly, that would be a mess to keep organized. The decimal is a standard format for numbers that have, well, decimals. A decimal number has a range of -79228162514264337593543950335 to 79228162514264337593543950335. But wait, where’s the decimal in that?

Well, the decimal concept gets a bit trickier, because let’s remember that now instead of a fixed increment going up or down (whole numbers are fixed increments going up 1 whole unit or down 1 whole unit) we now have to consider all of the decimal places that appear before OR after a number, so they sizing can be a bit crazy. The fact is that you typically see an ability with a decimal number to go out to 28 or 29 places (before or after a decimal point, or combined before AND after). That means that a value like 0.0000000000000000000000000001 (28 places after the decimal) will actually print as it is. If you have one more decimal place before that 1, the one will disappear off the number. Likewise, if you add numbers BEFORE the decimal, the number of spaces AFTER the decimal will also be truncated (cut off).

0.0000000000000000000000543210
2.0000000000000000000000543210
22.000000000000000000000054321
220.00000000000000000000005432
2200.0000000000000000000000543
22000.000000000000000000000054
220000.00000000000000000000005

Line 1 and Line 2 have the same number of places, so even changing the ones place from 0 to 2 doesn’t do anything. However, adding a second place in front of the decimal means that a place at the end of the number is truncated. Keep in mind that truncation is NOT rounding. It is literally the place where the number just drops off.

While it is possible to have longer numbers using scientific notations, for the purposes of programs we’d write, this is more than sufficient, so we will not worry about those specialty cases. If you’re writing a recipe program, a check book program, and even a video game, the chances of getting to numbers more precise than this is limited at best.

Frankly, if you have a recipe calling for a measurement more precise than 0.0000000000000000000000000001 teaspoons of an ingredient, I don’t think I’m going to be following your recipes.
Single and Double
As is the case with the whole numbers, there are a few variations of these decimal numbers as well. The SINGLE and DOUBLE value are versions of a decimal that store numbers with lower limits to the number of places. A single (sometimes called a float) can hold 6 to 9 places, and a double holds 15 to 17).

So the question is probably brewing in your head: why all these versions of numbers? The answer is that in earlier days of programming, computer memory and processing power was far more limited than what it is today.

The processing power of machines at the advent of computing would seem almost laughable now. In fact, the average hand-held calculator holds as much processing power as some of the earliest computers. If you have a smart phone, you probably are holding more processing power in your hand than what the government had available in some of it’s best machines in the early 1950s.

Concerns of storage and memory space and processing power made it critical that values were only made large enough to hold what was really necessary. And it still makes sense to do that today. Why get a 3 ring binder capable of holding 1000 sheets of information if the only thing you intend to store is a piece of paper with your first, middle and last name?

While we don’t need to be quite so strict today in limiting use of memory as was needed 50 or more years ago, it still does slow down and use up the resources of a computer each time you declare a variable. While it might not be significant in one case, thousands of variables could begin to tax a computer over time.

So these different size data types still exist so we can program for what we will need, reasonably, in the future. And if we find that we do need to expand a variable’s size at some later point, it’s not a horrible ordeal in most cases to do that.

For most of the examples we’ll look at for the foreseeable future, we’ll use Int and Decimal values. If there is a special case where we need to use something else, we can review the reasons why at that time.

Character Data Types

As was the case with the numeric data types, there are a few different character data types to consider as well. For our purposes, we’re just going to look at two types for now.

Char
You might hear people pronounce this like the word describing the burned / black portion of barbecued meats. While people will probably overlook that, the truth is that the word is actually pronounced like “care”. It’s actually short for Character, and it is exactly what it sounds like – a single character value. The uses of single character fields might seem minimal, but in actuality, you can use them for quite a few purposes. For now, though, we’ll look at the more common data type.
String
The string is actually a more commonly used character-based data type in many languages. It is a block of character data, and can be used to hold up to 2 billion characters. Yes, you read that right, 2 billion character is the maximum theoretical limit. I saw theoretical, because it’s highly unlikely you’ll actually allocate that much, and frankly, it’d be bad to do so. 2 billion characters is a LOT of data, and having to load and process that much in a single value would be taxing to nearly any computer system. Even if you’re writing a novel that’s about 350 pages, you’re not going to get to those sorts of numbers. A typical page on a standard hard-cover book is about 300 to 350 words. At an average of 5 characters per word, plus one for a space, that translates into 1500 to at most 2100 characters. Even if you double that and say 2400 characters on a page, that still means 735,000 characters in total for a book. That doesn’t even come close to 1% of our memory max limit for a string.

So a string is a pretty versatile form of data in it’s capacity. Strings can be used for storage, can be searched, and compared against other strings. That allows us to do the vast majority of what we need to write most programs. We will use this data type a LOT.

Data Types and Functions or Methods

We’re just going to introduce this concept briefly for now, because there is a lot more we can and will learn about when it comes to data types. But it’s important to understand that data types have importance beyond use for variables. They also have significance in use with Functions or Methods.

When it comes to producing code, we tend to rely on functions and methods, as described in the prior article, to preform multiple tasks or steps that need to be repeatable. In other words, a task to calculate someone’s age based on a birthday is something we don’t want to have to write the code for manually EVERY time we need to use it; for one thing, programs that are meant to handle multiple people, a growing list of them, would never be manageable this way. For another, doing anything like that would defeat the purpose of using a computer at all – automation of process is a big factor in why computers are so valuable.

If we’re going to use functions or methods, it is likely that we’ll need to get feedback or results from those functions and methods at least some of the time. Data types are a part of that process, because they indicate what type of data will come back from a particular function or method. For example, a method that performs a calculation of the age of a person based on their date of birth could just spit out a number to a screen. But what we need that number for some other process later on?

The function could be set up to return the value to us, and we could decide what to do with it later. Initially, many methods are set up with a return type of VOID. Void means there is no return type. We perform some process and then move on. But if we had our age calculating function, we would set it up to take in a date, and return an integer indicating how old the person is; perhaps in years, perhaps in day; maybe it would go deeper depending on the time of birth.

But the fact is, we can set up our functions or methods to indicate what type of data will come back from a set of tasks. When we write code like this, we declare the return type of the method or function to use one of the aforementioned data types. How we do that is something we’ll cover soon. But for now, it’s enough to know that these pieces fit together. The how we’ll cover in time.

Conclusion

This is a good place for us to stop; there is a lot to absorb before we get into writing actual programs, so it’s important that we have groundwork laid out before we just start typing stuff up on our screens. The next article will discuss the actual anatomy of a program in more depth, and we’ll look at the structure of blocks of code and talk about how the terms of part 1 and the data types in part 2 fit into that picture. Once we’ve done that, we can begin to make some serious effort at writing a simple program, and know what we are doing, and why.

Wait! Do you know what you asked for?

I was going to write an article about the Coronavirus and how it is affecting the world, but I realized that 1) people are panicking enough about it that I can write about something else that can make people nervous and 2) people are apparently at risk of catching the Coronavirus just for discussing the Coronavirus. In fact, there was a doctor in China who died from the Coronavirus after talking about the Coronavirus; I’m not making this up, by the way. But you can look it up. I don’t want to talk about the Coronavirus any more and risk catching the Coronavirus.

Instead, I’ll write about something else. Like the fact that my son is now traveling with his classmates to Canada. They’re going on a very fancy bus, enjoying more comfortable accommodations than I do trying to fit in an airline seat (excuse me, I asked for the SMALL sardine can, thank you). They’re on a class field trip for 8th graders to Quebec, Ontario where the official motto is: Va-t’en, idiot américain / Go away, you American jerk. Remember, in Canada, everything needs to be written in French AND English, to increase the chances that people standing next to each other will be given conflicting information.

While there, the students have the opportunity to see how people from another country, who speak a different language, and a moderately different culture, hate the average American.

I can’t say I entirely blame people from other countries for hating us; after all, we tend to hate them, they tend to hate each other. Realistically, international relations could best be summed up as 195 sovereign nations staring at each other crossly while secretly giving each other the finger.

All that aside, my son is getting to travel to a place that actually has COLDER weather and potentially more snowfall than Buffalo, NY – and quite often, that’s saying a lot. Of course, it’ll be no surprise if after the trip he comes back feeling a bit run down; travel is taxing on the body; my wife and I could vouch for that fact after our honeymoon. That’s a story for some other time; like after getting proper therapy and medical treatment.

For the kids (probably not the chaperones), it’s one of those enjoyable trips where they will get to learn about variations to culture, and the significance of language in shaping the lives of people from a region. Or at least it can lead to miscommunication when you place an order at a restaurant by asking for what turns out to be “Tax Not Included in Price”. At least, that’s what would happen to me.

I was never really good at foreign languages. They always seemed so… different. Which I realize is the point to learning them. But there is a vast difference between being able to understand a few key words or phrases, being able to speak or write less than half of them, and being able to do something other than saying you don’t speak the language, ask where there’s a restaurant (or bar), ordering something, realizing you need to get to a bathroom FAST, and then asking where a hospital might be, because there was definitely blood.

My son and daughter seem to be much better at stuff like this than I am (I mean foreign languages; not bleeding from digestive trauma). I think that my wife is generally better than me at languages as well. Which is good; they all like to travel, and being that they enjoy traveling, the are likely that they’ll be fine. I, however, am not much for leaving my hometown (except, maybe sometimes if it involves trains). Statically, I will be the one in a position to require new kidneys, and perhaps to cause the declaration of a travel ban against citizens of the United States of America. It’ll be entirely an accident, I won’t be trying to anger anyone. I’ll intend to say “I really like your family’s tacos”, but it will likely translate into “I wish to fondle your wife’s taco”. You might see a problem with that statement; if not, do not attempt to complement anyone on sausage, either.

So of course, I’ll need the thing about where to find a hospital, in the local language. When trying to communicate with people who speak different languages, you’re probably in a different country or at least a different region of your own country; or you’re in Hell, where everyone is yelling at each other incomprehensibly until the end of time; or you’re in line at the DMV – which is like Hell, but without the ambiance, and lasts much longer. Chances are, too, that there is also a local water supply that your body is not acquainted with, and you will NEED to have some help with issues of a medical nature.

At any rate, my son is traveling abroad, and in 4 days he will come back and share his experiences with the family. He’ll have explored a new place; he’ll have photos of wondrous sites; he’ll have some gifts, perhaps, for family; he might have a case of pneumonia or hypothermia, because, remember, it’s Quebec. And we’ll all laugh. And then he’ll cough a bit and we’ll all freeze because we’re back to thinking about the c**********… if you know what I mean.

Common Concepts of Programming Part 1 – Terminology

Introduction

I’ll be forthcoming on this; programming draws a lot from mathematics. And while that may sound scary for some people, the truth is that most of it is no more complicated than the simple math that makes up daily life. For the average program, the math gets to nothing more than Algebra. If you were always one to struggle with math, don’t panic, and don’t let yourself get turned off by the idea of having math being a part of this process. I struggled at a lot of points in dealing with math education, and I can vouch for the fact that most applications won’t get that complicated on you.

At worst, if you have to deal with advanced math concepts, it’s only in developing something like a game. And even there, the computer is set up to help you a lot. You aren’t doing math so much as learning to plug in an equation. The computer will actually solve the problem, you just tell it which problem you want solved.

At any rate, programming comes down to some logic, and solving simple equations. Before we get to doing that, though, we need to understand some core concepts about programming languages.

This article will contain a list of common terminology.

Common Terminology

Most software is fairly complex when you look at it as a whole. But the simple fact is that programming is essentially a forest; and that means that a program consists of individual trees. Yes, trees have some complexity to them, but individually, they’re not so hard to understand. That vast majority of challenge comes from learning how these trees combine to become that forest.

There are a number of concepts that are common to nearly all programming languages. I say nearly all, because there are a few languages that may not have these as readily identifiable concepts. But the truth is, if you’re discussing a language that doesn’t include these concepts, that means you’re dealing with a highly unusual or very specialized language, and not one you’re likely to encounter outside of highly theoretical or advanced development work. I work as a software engineer, and I have for 20 years. I have never used those unusual languages in a professional context. However, what I am covering here is stuff I use on a daily basis.

Variable
A variable is essentially a placeholder for some piece of data. As an example, the name of a person can be a variable. So could a value such as X in a simple math equation such as 4 + X. If X is 1, the equation would be equal to 5; if we change X to a value of 3, the result is now 7.
Function
A function is a series of instructions that are used regularly for calculation or action; rather than having to write individual instructions repeatedly, we can build a function that can perform the process we need over and over again.

For example: if we needed a way to calculate your age based on your birthday and the current day, we could do the math for it in a single block of code. But what if we wanted to do that for multiple people with different birthdays, the amount of code we have to write would scale up very quickly, and make our program inefficient, bloated and messy.

A function could be put to take your birthday in as a variable, and return the resulting age.
Object
Objects are part of a concept called object-oriented programming. Building on the idea of reusable code, an object is a sort of structure that can be used to define complex re-usable idea. For example, you can create a person object, which would have space for a first name, last name, date of birth, address and phone number. Then, for each person, you’d create a new copy of this object, and populate it with its own data. The proper name for these copies is instances.
Property
Variables that are a part of an object are generally referred to as properties. They essentially are the same as a variable, but because they form a specific part of a type of object, we call them by this name to indicate that they are “owned by” that object.
Method
If a property is a variable in an object, a method would be a function in one. A great example would be to build that age method into the person object. Then when you needed a particular person’s age, you don’t need to get the date from the object and send it into a separate function to get back the age. You can call the method on the person and get their date back directly. In other cases, instructions for an object that is very specific to a person could be a part of the object. For example, you can have a stand-up function. When it’s called on a particular person, that one stands up.
Condition
Conditions are logical rules to help control the flow of information. Programs that can’t respond based on conditions are really not useful, because they would run in a linear way and not allow for different operations based on what a person can do.

For example, if a person’s age is under 5, a person goes to daycare. if they are over 5 and under 18, they go to school. Over 18, and they could go to college, trade school, get a job, and more.

If statements (sometimes referred to as if-else or if-else if-else) are a common control approach to provide choice control and reaction.
Loop
Loops are a control structure that allows for repetition of a task based on a set of conditions. For example, if you needed a person to take steps until they get to a door, you would need one of two things; either you need to know the number of steps to the door and have to program the take-a-step method to happen that number of times or; you could use a loop that checks to see “is the person at the door” and if they aren’t, they take another step. That loop would continue until the condition of that person being at the door is met.
Logical Operations
Commonly used as parts of conditions and loops are logical operations. These can be a bit tricky, but commonly, there are 3 that we start being concerned with: AND OR and XOR (also called exclusive or).

AND is pretty simple. You must meet all conditions for the AND statement to be met. For example: if the person is at the door and the door is closed, open the door. If the person is not at a door, the condition fails at that point and is skipped. If the person is at the door but it is already opened, the step of opening the door is still skipped. Only if the person is at the door AND it is closed can you open the door.

OR (also known as an inclusive or) and XOR are slightly more complicated, but only because the reality is that in a language like English, we tend to assume that we mean OR when we mean XOR. OR means that 1 or more conditions are met. XOR means that only one of the conditions is met.

The best way to understand this is with a “restaurant” example. If you go to a restaurant and order an entree (please get one for me, too), you will probably be asked if you would like soup OR salad. Now what English tells us is that we can have the soup, or we can have the salad, but we can’t have both. In programming, OR means you can. The translation of this would be “You can have the soup” or “you can have the salad”. If the first case is true, then you don’t even worry about the second one..

With an exclusive or, if you have the soup, it is exclusive, so you cannot have the salad. If you have don’t have the soup, then you can have the salad. If you have both or if you have neither, the condition on that fails.

Most programming languages have a direct implementation of AND and OR (in logic, they are displayed as && and ||, respectively). However, not a lot of languages have a direct XOR operation. However, you can emulate that process by saying “if soup is true and salad is false, do something; if soup is false and salad is true, do something else. And if the person tries to say soup AND salad, or neither soup, nor salad, nothing at all is triggered.

Of course, I don’t recommend that if the waiter says “would you like the soup or a salad” that you answer with “sure”! They might not find it funny.
Comment
A comment isn’t actually a part of code, in the sense that it doesn’t “do” anything. A comment is exactly what it sounds like; it’s a statement of a generic sort – when you build a project, you can use comments to note what you did and why. The compiler just ignores them, and go right on past them. But if you used a comment properly, then in several months when you go back to look at your code, you can leave a note for yourself on why you did something, when you made or changed something, where things come from and more.

Comments are the most useful ignored feature in programming, precisely because they let you track what’s going on, exactly where it’s going on, without actually doing anything to influence the outcome of the program itself.

Conclusion

This isn’t our only terminology; not by a long shot. But this is enough for the moment. In the next part of this set of articles, we’ll talk about the various types of variables, and their relationship to the concept of functions and methods.

Getting Started with Visual Studio Community 2019

A few quick notes before I dive into the meat of the article.

  1. I will not automatically show you the best technique for advanced programming; not because I want to make you inferior, but because I want to slowly introduce you to more and more advanced concepts over time. So there may be some things I show you now that I’ll later tell you not to do normally. Don’t panic or get upset; those lessons are meant to help you understand important concepts, and it’s just as important to learn what NOT to do as it is to learn what you CAN do.
  2. Most of the stuff I’ll show you will be using Visual Studio 2019 Professional. For the most part, you’ll see the same tools as I would, but if something is missing or seems odd, don’t worry! I won’t use anything that isn’t available for the Community Edition – and if somehow I manage to do that, just send me a message and let me know; I’ll get you the info you need to proceed.
  3. Don’t panic about the complexity of what you’ll see. Remember that a key to something complex is to focus on the small pieces. If you don’t panic and worry about the big picture, you’ll have the Sistine Chapel before you realize it.

Launching Visual Studio

Assuming you’re using Windows 7 or 10, go to your Start menu and look for “Visual Studio 2019”. You’ll get asked if you’d like to sign up for a Microsoft account.

You can skip past (click Not now, maybe later). Then you’ll get presented with this screen:

This will let you select the configuration options for using the software in an optimal way. for the “Development Settings:” drop-down menu, choose Visual C#. You can choose the color scheme you think you’ll like. You can always change it later if you like. At some point I will do an article on playing with the preferences for the IDE; and there are a LOT of options. For now, I’m going with Dark. It’s easier on my eyes, so that’s what I tend to stick with. The theme won’t affect what tools are available, so just pick whatever seems best to you for now.

Welcome to… another menu screen…

OK, this is actually a good thing. From here, you’ll be able to start building your first application. You won’t initially see anything on the left side of the screen under “Open recent”; that’s because you haven’t done anything yet. In the future, programs you’ve written will be available here for you to reopen.

For now, we’re going to Create a new project (the button on the lower right of the screen). Good grief! What is all this?

Again, relax. This is where we select the type of project we’re going to work with. This give you an idea though of what sort of capabilities you’ll have for development. You may not see as much as I have here, but you’ll still likely see a lot of options in that right-hand list. Let’s filter down to something more manageable. Under “All languages”, select C#. Under “All platforms” select Windows, and under “All project types”, select Console. At this point, you should see a MUCH smaller set of options. The ones you’ll most likely see are “Console App (.NET Core)”, “Console App (.NET Framework)” and “Workflow Console Application”. As long as you see the first two options, we’re in good shape. Select “Console App (.NET Framework)” and click “Next”.

This is the option to set up a project that will work specifically on Windows, and run in what you may recognize as a command-line interface (some people will call it a DOS interface, and while that’s fine, understand that DOS really refers to an operating system that is really no longer in use). Regardless of whether you call it command line or DOS, this will let us build a simple text based application.

Egads, another screen?! Yes!

Yes. But don’t worry, we’re almost through this initial set of steps. On this screen, we’ll have a few options for what we can do. First, let’s give our project a name. It default to ConsleApp1, but that’s not an entirely useful name. You’d like to have SOME idea of what the app is for. It’s not going to be much, in this case, but we should have something better than ConsoleApp1. Change this to say HelloWorld. Note, there shouldn’t be any spaces in this field if you can avoid it. You will probably notice that as you renamed the project, the Solution name field also changed to HelloWorld. Let’s explain this just a bit.

Projects? Solutions? Huh?

OK, this isn’t really all that bad, but it’s valuable to know. Most of the time when you want to build a larger sort of application, you won’t want to slap everything into a single big file. It makes it hard to sort through problems, it makes the program potentially WAY larger, and if you want to make some of your code re-usable, this isn’t going to make that task easier.

A Solution is a container for a series of related projects. Some projects will be actual applications. Some will be collections of functions you may choose to reuse later. Some projects might be for building a website. So you could, for example, say you have a project for an application, a project for a website, and a project that has some code used by both. Each part is it’s own project, but they are all part of the solution. Later, you might have an entirely new solution, but you’ll want to reuse the library project; can you can do that, by copying it from one place to another.

If you want an analogy: imagine you’re hungry. You want to make dinner, and a dessert. You have ingredients in your fridge. Well, some ingredients (like milk or eggs) are used in many different recipes. You probably don’t keep separate batches of eggs and milk in different fridges. You keep them together, and when you need an egg, you get it. When you need milk, you get it. What about salt or sugar? Well, you don’t need to keep them in the fridge, but you’ll have them in another cupboard. Again, not all recipes need sugar or salt, but if you do, you can get them out of the right place. You’ll have a recipe, which is the instructions of how to use those ingredients to make your recipes. And you don’t just mix all the ingredients for both recipes together (if you think strawberry jelly goes well with fish, maybe you would, but frankly, your nuts).

So you follow your recipes, produce each of the parts of your meal. And when you’re done, you can now enjoy the fruits of your labor. Congratulations; you solved the problem of being hungry.

The places we store ingredients, and cookware and whatnot; those are all library projects. The recipes might also be a form of library. The actual process of preparing the recipes are also projects, but instead of being libraries, they’re programs. They are the direct control over the process of making your meal. And the entirety of making the meal and dessert? Well, your problem was that you were hungry. The solution was to make the recipes.

So a solution is a container for projects. In the case of HelloWorld, we don’t need to separate them any more than this, because we’ll only have one simple project in here. They can have the same name and it won’t be an issue.

For the Location, you can leave it to default to wherever it is. More than likely you’d see something like “C:\Users\someuser\sources\repos”. For the time being, that’s just fine. Later on we can look at other ways and places to store things.

You may have noticed a checkbox that says Place solution and project in the same directory. Frankly, I don’t recommend that. If you leave it unchecked, you’ll have a folder for the solution, and then each project will have it’s own sub-folder. I find that easier to deal with, personally. Finally, you’ll see a drop-down box that is labelled for Framework. If you click on that, you’ll see a ton of entries like .NET Framework 2.0, .NET Framework 3.0, etc. It’s not likely you’ll ever need to go to a much older version than what’s current, but it is useful to have all of the options in there in case you later download a project from the web and want to try to update it to what’s current. As of this article, the current version is .NET Framework 4.7.2. If you did your install of Visual Studio right in the last article, you can just select this value if it’s not already set, and click “Create”.

You’ll now have a much more complex looking screen, with a big window in the center with a lot of text in it.

Before you can say “What is all this?” let me explain what all of this is.

The Visual Studio Interface

Remember, first, that Visual Studio is an IDE (integrated development environment) and that means that it’s a collection of tools related to the task of writing programs. So there’s a lot of stuff that will show up on your screen. I’ll cover the important ones to be aware of here; don’t panic if you see some additional stuff on my screen and you don’t have it on yours. I do a lot of development in here, so I’ve moved some stuff around for convenience. You’ll be able to see the most important stuff right away, and at another point, I’ll describe what you can do to switch things around. Don’t worry, it’s not likely that you’ll hurt anything by doing this.

Across the top is a typical menu bar. File, Edit, View, etc… all things we see in most of the programs on a computer. Below that is a toolbar with a set of icons. Initially you’ll see ones you recognize, like new, open, save and the undo and redo buttons. Next to that you’ll see a couple of drop-downs. Once will probably say “Debug”, the next will say “Any CPU”. Then you’ll have a Play button (a green arrow on my screenshot) and the word “Start”. And then there will be some more icons. The biggest one to be aware of is that Play / Start button. We’ll use this to actually run our first program. Over to the right of the screen, you’ll see a panel called “Solution Explorer”. You may also see a tab for “Team Explorer” and “Properties”. You can switch tabs if you’d like just to see what’s in them, but make sure you’re back on Solution Explorer when you’re done.

If that panel looks like the next image, you can click and drag the “Properties Tab” down; you’ll see a little set of icons appear, shaped like a plus. Drag over the lowest box, and it’ll separate so you can see it AND the Solutions / Team Explorer tabs separately.

On the left side of the screen, you’ll see a panel labelled “Toolbox”. That’s something we’ll cover later. At the bottom of the screen you’ll see “Output” with two tabs. One is called “Output” and the other is “Error List”. You might see some more tabs, too, but again, we’ll worry about that all later.

Finally, smack in the middle of the screen, you’ll see a window labelled “Program.cs” and have the results of a cat running across your keyboard… no, not really. You’ll see a bunch of text with some different colors and a series of numbers down the left side.

This is called the source code of your program. If you have a solution that has lots of libraries and files to it, you’ll see more tabs open across the top bar over time; but for now, we’ll just focus on what’s here.

The numbers on the left side of this window are actually line numbers. If you’re writing a program and something fails, this will help you to find where your mistake is.

Lines 1 – 5 on my example start with the word “using” followed by a few more words, and then a semi-colon. These are usually called “directives” or sometimes “includes”. Essentially, this is a collection of libraries of common code that your application may (or will) need. They’ll probably all look kind of dim-colored; that indicates that we aren’t actually using any of them just yet. This program will only really use one, but we can leave this as is.

Next, you’ll see the word “namespace” followed by “HelloWorld”. A namespace is a sort of container used for coding. We’ll get into more detail on that later as well.

Next, we see “class Program”. This the starting point of the actual application. We’ll discuss classes more soon, but something you’ll here in a lot of languages is the phrase “everything is a class” or “everything is an object”. What this really just means is that every part of a program written in these languages is essentially a sort of component that is used for operation. The initial class for C sharp is Program. That indicates that this is the program container.

Next, you’ll see “static void Main(string[] args)”. If you’re wondering what all this means, don’t worry, we’ll get to it when we’re ready. Lastly, you might notice a series of characters that look like this: { } Commonly they’re referred to as curly braces or brackets. Note how they’re placed. They indicate a sort of start and stop to a logical “container”. As we expand our skills, we’ll see their relevance quite quickly.

OK. What does this program do? Nothing! If you tried to run it now, it’d start and end… and chances are it would happen so fast you might not even see a window show up for it. If you do, it’ll be a sort of black box, and it’ll immediately disappear.

Let’s actually make something happen.

One Quite Note

Be aware that programming languages are typically case sensitive, especially when it comes to the names of functions, variables, and the like. Make sure you’re careful about names and spacing when you type things in, because quite often you can make a mistake by forgetting to make something uppercase. A key example will be the commands “Console.WriteLine” and “Console.ReadKey”, which we will use below. If you forget to capitalize the L in WriteLine, or the K in ReadKey, the program will NOT work.

One nice feature that helps with this is called IntelliSense. It’s a feature in Microsoft products (along with numerous others for programming) that will help you to select the right command and instructions. As you type, it will pop-up a list of possible values and try to guide you to the best choice. I’ll cover that in more depth at another point, but based on some feedback I received, I figured it was important to note that before we got too far into this.

Adding To The Code

In between the curly braces under the “static main” line, hit your enter or return key a few times. Go to the first line after the opening curly brace { and type in the following:

Console.WriteLine(“Hello, world!”);
Console.ReadKey();

Your program will look like this:

Go ahead and hit that “Start” button at the top of the screen.

Congratulations – you just wrote a program. It doesn’t do very much, but that’s not a problem. It actually did something. To exit, you can either hit the “Stop” button in Visual Studios, click the X on the windows to close it, or press any key on your keyboard and it’ll finish and close itself.

Down at the bottom of the screen, you’ll see in the “Output” window something like this:

This indicates that the program ran; when our programs get longer, we’ll see more information here that we can use to track through steps and do what’s called “debugging”. If you look at your code window, you’ll notice now that the first line “using System;” is now brighter than the other lines. That’s because we’re actually using code from that library. The Console.WriteLine and Console.ReadKey code is part of a general library called “System”.

We’ll dissect this program in more depth in a few articles. But for now, “Hello, world!”. Go ahead, and show it off! You made a computer program.