Stack1's Posts
Nairaland Forum › Stack1's Profile › Stack1's Posts
1 2 3 4 5 6 7 8 9 10 11 12 (of 12 pages)
... a few more for loop examples |
This code is similar to the last except the updateStament counts downwards
Here is the output: Count down started 5 ! 4 ! 3 ! 2 ! 1 ! We have ignition! the statement secs-- is a post-decrement (as opposed to increment) and it decreases the value of variable sec by one after each execution of the loop body |
lets see another basic for loop usage... |
when executed the program prints the numbers 1-10 on a new line, so exactly how does it do this... in the main function we declared a integer varable named count and wehave a single for loop written as
From our general definition of the for loop , count = 0 here acts as the initializationStatement, as it initializes the count variable to 0, note that this happens only once no matter how many times the loop is executed The statement
is the testExpression, so each time before the body of the loop is executed we test to see if the variable count is less than ten, and every time this test passes the body of the loop is executed i.e the current value of the count variable is printed followed by a new line, The statement count++ is a post-increment statement that increases the value of the count variable by one each time the loop is run so when the loop first runs count is initialized to 0, the test statement count < 10 passes and the loop body is run then the [count] variable is incremented to 1 by the updateStatement count++ on the next run of the loop, the testStatement count < 10 would now be 1 < 10 since count is now 1, then since its true the body of the loop is executed and count is once again incremented this goes on till the test count < 10 fails and the loop is terminated Note if the updateStatement had been ++count instead of count++ , the count variable would be incremented before the body of the loop is executed, we call this pre-increment |
lets see a very basic example of using the for loops, this example counts and prints from 1-10 |
My apologies if i have seemed to loose anyone in between so first lets look at loops and a few operators in detail The for loop To loop simply means to execute repeatedly. C's for loop are one of its control structures. The general form of a for loop is
the initializationStatement is executed only once the testExpression is a condition that needs to be met each time before the body of the loop is executed, when condition of this testExpression isnt met the loop is terminated the updateStatement is updated until the test expression is false |
romme2u:hmm, ok well in this case, the next post's would go over C's operators, control structures and loops, i had already discussed integer and char data type, that should do for now till we go into advanced stuff like structures and arrays/pointers |
ok, before i go into the details of the line counting program, can anyone provide us summary of how it works? |
/** program to count how many lines are there in the input **/ #include <stdio.h> int main() { int c, nl; while ((c = getchar()) != EOF){ if(c == '\n') ++nl; //increase count on each new line } printf ("%d", nl); getchar(); } |
The last example seems a bit more involved first we create two variable n which would be our counter, and c to store character input now the main stuff happens in the while loop
lets break this down first c = getchar()reads a character from the key board input and stores it in c remember characters in C are stored internally using their numerical (ASCII) values so ((c = getchar()) < 8 || c > 13) does this: take the ASCII value stored in variable c and check if its less than 8 or greater than 13, the operator || means OR in C So why less that 8 or greater than 13, characters between this ASCII range (look it up in the ASCII table link) are all white space characters, that is they do not get a visible printed representation like normal letters, so the code while ( ((c = getchar()) < 8 || c > 13) && c != EOF), checks to make sure that the character entered is not within the range of white-space characters ( because the main aim of the program is to count normal (printable) characters so if the character isn't white space the next check is
the symbol && means AND , so if the character isn't the END_OF_FILE marker (aka we haven't reached the end of the input) So together the while loop checks that the entered character isn't a white space and it isn't EOF, if this two conditions are met it means we have a normal character so in the body of the while loop we increment the value of the counter variable using the statement n++ which is called a post-increment then after the loop ends the final character count is printed |
/** a character counting program **/ #include <stdio.h> int main() { int n = 0, c; //make sure the return value of getchar isnt in the range of ASCII whitespace values \n\f\v\r while ( ((c = getchar()) < 8 || c > 13) && c != EOF) { n++; } printf ("%d", n); getchar(); } |
Ok so we introduce more new stuff in the last paragraph. Here we have 6 paragraph elements with their respective id set But notice the CSS selector where we just wrote p, what we are doing here is using a tag name (a paragraph tag) in this case as a selector, now what this does is to apply any style we define for this selector to all paragraphs in the page, and as you can see we have
and these styles get applied to all the paragraphs, Next we apply different CSS text properties in each paragraph The first has a custom rgb color value the second uses the CSS letter-spacing property to control the spacing btw individual letters the third uses the CSS text-align property to control the alignment of the text (other acceptable values here are center and left) its left to the reader to figure out the CSS text properties in paragraph 4, 5 and 6 |
<!-- Lets look at some text properties that can be controlled via CSS -->
|
The last example creates different DIV and P elements, notice that each element has an id attribute set, this allows us to target the elements for styling in the CSS section, all elements are centered in the page using the <center> tag DIV 1 which has an id of div1, and thus its selector in CSS is #div1 and the element is given a width, height and background-color style, as shown lenghts (like width and height) can be specified in different units such as px (pixels), em and percentages (which are both relative units) Colors in CSS and be specified uusing the color name such as blue, the hexadecimal value for the color e.g[b] (#fff or #ffffff)[/b] which represents white, rgb colors e.g rgb(255,0,0); which is red and on some browsers even hsl, hsla, cmyk etc and other color spaces, see a comprehensive list of supported CSS colors here http://www.w3schools.com/colors/colors_names.asp For the first and second paragraph elements, you'll notice they are not properly centered the space around them have been altered using the margin-left property, which can add or remove-space from the left of an element by using positive or negative values, there's als a margin-right property for controlling space on the right (remember this works only on block elements) The example also highlights the use of the background-color and color properties to set the background and text color respectively |
<!-- Here we create four four block elements of different sizes and apply colors and other style properties to them -->
|
Hi all, by now we should have understood the fact that an HTML document is made up of tags, now this tags which can also be refered to as Elements have different modes of display. some tags/elements when used in an HTML document forces a line break, that is they start on a new line by default, this elements are known as Block Elements. other tags do not cause a line break, but instead are displayed on the same line next to their previous element sibling, this type of elements are known a inline elements ) as they are displayed inline they are ways (mostly using CSS) to force an inline element to display as a block element and vice-versa for examples of block elements in HTML5 see here https://developer.mozilla.org/en/docs/Web/HTML/Block-level_elements for examples of inline elements in HTML5 see here https://developer.mozilla.org/en-US/docs/Web/HTML/Inline_elements block-level elements have several advantages and uses 1. They can be sized, using the CSS width and height property 2. they can be given backgrounds (colour and images) and custom borders 3. the space around them can be controlled using the CSS margin property, also the space inside them can also be controlled using the CSS padding property Lets see some more examples... |
bestiyke:aiit, but em, sorry o, u using *dear in that sentence, i be male o ![]() |
bestiyke:aiit, but em, sorry o, u using *dear in that sentence, i be male o |
bestiyke:you are right and i'm doing that on purpose the while loop is a very powerful construct and very compact and smart programs can be written with proper use of while, its use (to newbies) would become more clearer as we proceed |
hey guyz, thanks for the feedback, would be posting several more examples later today, i'm usually a bit busy during the day, but would definitely posst by evening/night |
jidez007:permitted bro |
pls i'm a bit busy today but would definitely post during the night, we'll be looking at most text formatting options, elememt borders and background, i might also post on links and images |
odiagabros:tried that didnt work oo |
see the system logs, it would provide you more info on the error, looks like a port conflict or the sql server wasn't properly configured/installed |
ok, that was damn funny, but OP you get time oo.. |
when it works it seems to well, i have downloaded between 1.2-1.4 Gig severally but recently i experienced two cases where it stops working after about five minutes from activation and in both cases i had only used 45mb and 90mb |
shirgles:hmm, i guess.. |
Just registered bro.. good concept/implementation and things actually work so far.., but definitely still has room for improvement, i did notice the design isn't -responsive- though, you should really use CSS media queries + any responsive framework ( aka bootstrap) et.all, for any new web project you work on. I personnaly like and use http://www.w3schools.com/w3css/ |
I'll prefer PHP anytime any day, NodeJS tends to be quicker to pickup for those already well versed in client side JS, however NodeJS wasnt meant for very large scale projects or projects where script perform long running tasks, or parse large amount of data ..see this page for some more thoughts https://www.quora.com/What-are-the-disadvantages-of-using-Node-js |
If you ran both programs you'll notice they both do the same thing, you type in some input and it prints it back.. now lets look at each program in more detail Program listing 03 starts with the #include preprocessor directive (which we have earlier covered) and so the line #include <stdio.h> simply includes the header file for the standard input/output library that comes with all C compilers, the library contains a plethora of functions related to input and output and so by including its header file we have access to this input/output functions tells us several things, first the int means that this function is meant to return an integer, and void between the parenthesis tells us the function takes no arguments next we declare a variable called c and using the char gectchar is a function from the input/output library that reads one character at a time so the line c = getchar();reads the first character entered by the user and stores it into the variable c next we have
the while loop as explained before continue to executes the statements in its block till its condition doesnt evaluate anymore so in this case while(c != EOF) means while the value (or character) stored in the variable c isnt EOF , the statements in the while loop's body should keep executing So what exactly is EOF, in C and C++ this means End Of File, and its simply an indicator that we have reached the end of an input source now if you run the program and enter a line of text the program simply prints back the text and again waits for you to enter a new line, and the process repeats again and again.. EOF is traditionally useful when reading from actual files on disk or maybe when receiving data from a network port, in case of command line program like ours to indicate EOF to the program so it can exit you press Ctrl+Z and enter The body of the while loop is simple it prints the last character entered using the putchar function and and prompts for another character The second program does the same this as the first the only difference is that it is more concise read through and lets discuss this programs before we move on.. |
Lets start with two similar programs (please for anyone following try to input & run this programs, to be sure you fully understand how they work) program listing 03
program listing 04
|


. i believe treating operators, primitive data types and loops with common library functions will make ur programs to be self-explanatory.