For this blog we will go over conditional statements, if, if-else, and nested loops.
The first one will be a simple if statement:
A simple comparison of two variables will print out. The format to create this if statement is extremely similar to that of Java.
So we will use this structure:
if (evaluation)
{
statement;
}
Notice that there are still parenthesis and brackets for the if statements in C. Also, the operators are as follows:
< (less than),
> (greater than),
== (equal to),
!= (not equal to),
<= (less than or equal to),
>= (greater than or equal to.
The next picture will have an example of an if-else statement. It is the same code as before but we add else {statement} after the brackets of the if-statement.
Now we can add another conditional to have an if- else_if - else:
Another type of condition is called a case-switch.
A value is entered into a case and depending on what it is a different output is selected, or if there is not a case for that value then there is a default. The structure is as follows:
switch(expression)
{
case value1:
statement(s);
break;
case value2:
statement(s);
break;
case value3:
statement(s);
break;
default:
statement(s);
}
Also this program shown will use a function that will scan in user output. All that is needed to be done to use it is to code: scanf( "", %variable).
On looking back much of this syntax is basically the same as that of Java in order to perform conditional statements.
Wednesday, February 17, 2016
Wednesday, February 10, 2016
Constants and Variables
First up for this blog is how to create a constant in C.
Open a new project and at the top under the #include we are going to create a constant. In order to do so write #define CONSTANT_NAME 5. the structure is the name of the constant in all caps then it's value is right after it. Constants can contain both string and numbers.
Also a very important part of these printf() functions is the use of conversion characters. For integers we want to put %d in the string, for float use %f, or for a string, use %s. These help the program read what is being entered in from the constant or later the variable.
And the end code should look like this:
Next will be variables.
The way variables are instantiated in C is very similar to Java.
int x = 5;
or
double large = 56789123;
It is the type of variables followed by a name, then equals sign and the value.
Running this code should produce the following output:
Here I would like to make note of something I noticed while working on this program. In the run screen there is a \n to signal a newline, but there is no corresponding \n in the code. I ran a previous build and that had the extra \n, but did not re-build the code when I ran it again. Be sure to build the code each time it is changed, the program will not do that automatically.
Here is another program that will go through assigning variables through arithmetic.
Added:
Here is another example for basic arithmetic: volume of a cylinder.
Open a new project and at the top under the #include we are going to create a constant. In order to do so write #define CONSTANT_NAME 5. the structure is the name of the constant in all caps then it's value is right after it. Constants can contain both string and numbers.
Also a very important part of these printf() functions is the use of conversion characters. For integers we want to put %d in the string, for float use %f, or for a string, use %s. These help the program read what is being entered in from the constant or later the variable.
And the end code should look like this:
Next will be variables.
The way variables are instantiated in C is very similar to Java.
int x = 5;
or
double large = 56789123;
It is the type of variables followed by a name, then equals sign and the value.
Running this code should produce the following output:
Here I would like to make note of something I noticed while working on this program. In the run screen there is a \n to signal a newline, but there is no corresponding \n in the code. I ran a previous build and that had the extra \n, but did not re-build the code when I ran it again. Be sure to build the code each time it is changed, the program will not do that automatically.
Here is another program that will go through assigning variables through arithmetic.
Added:
Here is another example for basic arithmetic: volume of a cylinder.
Tuesday, February 2, 2016
Hello World! -First Programming Project.
For the first project in learning how to program C we will do a print out of Hello World. Also I am using Beginning Program with C for Dummies, by Dan Gookin to go through this project. Start up Code::Blocks http://www.codeblocks.org/ and click Create a New Project. Then double click on Console Application.
On the next screen click next, and then be sure to select C as the language we are using and not C++. Choose a location to save the file and use Hello__World as the file name. Hit next and on the following page unclick the create "Debug" configuration and continue.
On the far left there is a guide through the project just created. Double click on sources and then on the file main.c. It will open the file which is default set for Hello World. This language has a main{} function in it so that everything in it is what will run. The #include that is at the top allows for statements such the printf to work.
After seeing this code here is all correct, go to the top to the menus and go under build to build (or ctrl +F9) to build the program. This must be done for all C programs before running them. Afterwards run the program. The output will be displayed in a command prompt box.
Hitting any key will close the prompt.
Developing this code doesn't seem too hard it has similarities to others, but it is also just a simple print statement. The code would be almost the same if it were written in Java, but as the two languages are not the same I'm sure that following programs will introduce more code that is C specific and less general.
On the next screen click next, and then be sure to select C as the language we are using and not C++. Choose a location to save the file and use Hello__World as the file name. Hit next and on the following page unclick the create "Debug" configuration and continue.
On the far left there is a guide through the project just created. Double click on sources and then on the file main.c. It will open the file which is default set for Hello World. This language has a main{} function in it so that everything in it is what will run. The #include that is at the top allows for statements such the printf to work.
After seeing this code here is all correct, go to the top to the menus and go under build to build (or ctrl +F9) to build the program. This must be done for all C programs before running them. Afterwards run the program. The output will be displayed in a command prompt box.
Hitting any key will close the prompt.
Developing this code doesn't seem too hard it has similarities to others, but it is also just a simple print statement. The code would be almost the same if it were written in Java, but as the two languages are not the same I'm sure that following programs will introduce more code that is C specific and less general.
Subscribe to:
Posts (Atom)