₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,325,052 members, 8,420,072 topics. Date: Thursday, 04 June 2026 at 10:41 AM

Toggle theme

Stack1's Posts

Nairaland ForumStack1's ProfileStack1's Posts

1 2 3 4 5 6 7 8 9 10 11 12 (of 12 pages)

ProgrammingRe: Lets Learn C by stack1(op): 2:56am On Jul 26, 2016
... a few more for loop examples
ProgrammingRe: Lets Learn C by stack1(op): 2:53am On Jul 26, 2016
This code is similar to the last except the updateStament counts downwards



#include <stdio.h>
int main(void)
{


int secs;

printf("Count down started!\n" );

for (secs = 5; secs > 0; secs--){
printf("\t %d !\n", secs);

}
printf("We have ignition!\n" );
getchar();
}



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
ProgrammingRe: Lets Learn C by stack1(op): 2:43am On Jul 26, 2016
lets see another basic for loop usage...
ProgrammingRe: Lets Learn C by stack1(op): 2:43am On Jul 26, 2016


//count from 1-10

#include <stdio.h>
int main()
{
int count;

for(count = 0; count < 10; count++){

printf("%d \n", count);

}
getchar();
}




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

for(count = 0; count < 10; count++){


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

count < 10

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
ProgrammingRe: Lets Learn C by stack1(op): 2:28am On Jul 26, 2016
lets see a very basic example of using the for loops, this example counts and prints from 1-10
ProgrammingRe: Lets Learn C by stack1(op):
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 syntax of a for loop is:

for (initializationStatement; testExpression; updateStatement)
{
// codes
}



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
ProgrammingRe: Lets Learn C by stack1(op): 2:15am On Jul 26, 2016
romme2u:
u are back, that is great cool

like bestiyke said, u are skipping the basics and posting obfuscated codes before explanation. that ain't cool sad

don't let what dthml18 said come true huh. i believe treating operators, primitive data types and loops with common library functions will make ur programs to be self-explanatory.


Don't post obfuscated codes, it scares beginners and makes it harder for anybody including urself to read the codes at a later time even with verbose comment.

let ur code flow line by line in simpler statements and expression with good indentation such that ur code is readable even without comments.
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
ProgrammingRe: Lets Learn C by stack1(op): 1:48am On Jul 26, 2016
ok, before i go into the details of the line counting program, can anyone provide us summary of how it works?
ProgrammingRe: Lets Learn C by stack1(op): 1:45am On Jul 26, 2016
/** 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();
}
ProgrammingRe: Lets Learn C by stack1(op): 1:44am On Jul 26, 2016
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


while ( ((c = getchar()) < 8 || c > 13) && c != EOF)


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


&& c != EOF


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
ProgrammingRe: Lets Learn C by stack1(op): 1:27am On Jul 26, 2016
/** 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();
}
ProgrammingRe: Lets Start A Real Web-deveopment Course Here Html+css+javascript+php+mysql by stack1(op): 1:24am On Jul 26, 2016
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


p{width: 80%;
height: 4em;
border: solid 4px #D3D3D3;
font-size:2em;
}

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
ProgrammingRe: Lets Start A Real Web-deveopment Course Here Html+css+javascript+php+mysql by stack1(op): 1:17am On Jul 26, 2016
<!-- Lets look at some text properties that can be controlled via CSS -->


<!DOCTYPE html>
<html>
<head>
<title>CSS text styling...</title>

<!-- below is a style tag to contain our CSS declarations -->
<style>
p{
width: 80%;
height: 4em;
border: solid 4px #D3D3D3;
font-size:2em
}

#par1{
color: rgb(0,0,255);

}
#par2{
letter-spacing: 15px;

}
#par3{
text-align: right;

}
#par4{
text-decoration: overline;

}
#par5{
text-transform: capitalize;

}
#par6{
word-spacing: 20px;

}

</style>


</head>
<body>



<p id="par1"> I am a div paragraph </p>
<p id="par2"> I am a div paragraph </p>
<p id="par3"> I am a div paragraph </p>
<p id="par4"> I am a div paragraph </p>
<p id="par5"> I am a div paragraph </p>
<p id="par6"> I am a div paragraph </p>





</body>
</html>
ProgrammingRe: Lets Start A Real Web-deveopment Course Here Html+css+javascript+php+mysql by stack1(op): 1:05am On Jul 26, 2016
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
ProgrammingRe: Lets Start A Real Web-deveopment Course Here Html+css+javascript+php+mysql by stack1(op):
<!-- Here we create four four block elements of different sizes and apply colors and other style properties to them -->


<!DOCTYPE html>
<html>
<head>
<title>Block elements things...</title>

<!-- below is a style tag to contain our CSS declarations -->
<style>
#div1{
width: 60%;
height: 6em;
background-color: red;
}
#div2{
width: 20em;
height: 6em;
background-color: #ffa;
}

#div3{
width: 60%;
height: 8em;
background-color: rgb(255,0,0);
margin-top:2em;
}

#par1{
width: 30%;
height: 8em;
background-color: #0000FF;
margin-top:2em;
margin-left:15em;
border:solid 5px black;
color:#fff
}

#par2{
width: 30%;
height: 8em;
background-color: #8FBC8F;
margin-top:2em;
margin-left:-15em;
border:solid 5px #FF1493;
color:#fff
}

</style>


</head>
<body>

<center> <!-- the center tag centers everything it contains -->
<div id="div1"> I am a div element </div>
<div id="div2"> I am a div element </div>
<div id="div3"> I am a div element </div>

<p id="par1"> I am a paragraph </p>
<p id="par2"> I am a paragraph </p>



</center>



</body>
</html>
ProgrammingRe: Lets Start A Real Web-deveopment Course Here Html+css+javascript+php+mysql by stack1(op): 12:39am On Jul 26, 2016
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...
ProgrammingRe: Lets Learn C by stack1(op): 5:23pm On Jul 25, 2016
bestiyke:
Ok dear, thank you. Continue your good job for I am following.
aiit, but em, sorry o, u using *dear in that sentence, i be male o tongue
ProgrammingRe: Lets Learn C by stack1(op): 5:22pm On Jul 25, 2016
bestiyke:
Ok dear, thank you. Continue your good job for I am following.
aiit, but em, sorry o, u using *dear in that sentence, i be male o
ProgrammingRe: Lets Learn C by stack1(op): 3:20pm On Jul 25, 2016
bestiyke:
You have been using while loop. Though you have try to explain what it is and what it does, but I want you to know that it will still be blunt for a complete newbee who doesn't have any knowladge of loop statement and make it difficult for the person to flow with the tutorial at this stage. I feel you would talk more on operators as you have explain data types and introduce control and loop statements later. Well I don't know how you have organize your lessons. But I believe you future lessons will make clear any confussion now. And people are free to ask questions where they are confuse. Carry on. Atleast If you complete this tutorial, I boost of learning C language from Nairaland Programing session and from stack1 the tutor. Move with good work.
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
ProgrammingRe: Lets Learn C by stack1(op): 3:05pm On Jul 25, 2016
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
ProgrammingRe: Lets Learn C by stack1(op): 3:02pm On Jul 25, 2016
jidez007:
@op the course makes sense. Permit me to repost them on my forum, because my forum can filter unnecessary comments and show only the important posts, instead of having the user to keep sieving through all the comments.
permitted bro
ProgrammingRe: Lets Start A Real Web-deveopment Course Here Html+css+javascript+php+mysql by stack1(op): 3:00pm On Jul 25, 2016
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
PhonesRe: The Real Value Of Airtel N25 For 500MB Night Plan by stack1(m): 3:03am On Jul 25, 2016
odiagabros:
Wen its stops working after 5 minutes... Then its network just switch off n on ur data again to continue blazing... I was able to use 1. 48gb today
tried that didnt work oo
ProgrammingRe: Sql Database Error Gurus Pls Come In by stack1(m): 2:32am On Jul 25, 2016
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
RomanceRe: 15+2 Pics Sexual Perverts Can Relate To: by stack1(m): 2:28am On Jul 25, 2016
ok, that was damn funny, but OP you get time oo..
PhonesRe: The Real Value Of Airtel N25 For 500MB Night Plan by stack1(m): 2:21am On Jul 25, 2016
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
RomanceRe: 13 Situations Only Girls Understand by stack1(m): 2:18am On Jul 25, 2016
seunny4lif:
Girls
\

Lool
ProgrammingRe: Lets Learn C by stack1(op): 1:36am On Jul 25, 2016
shirgles:
Can u teach me in person
hmm, i guess..
ProgrammingRe: Share Your Gdevit.com Experience Here! All Trolls, Bots etc - stay out!! by stack1(m): 1:36am On Jul 25, 2016
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/
ProgrammingRe: Which is more awesome to use for 'server-side programming', NodeJS or PHP? by stack1(m): 1:09am On Jul 25, 2016
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
ProgrammingRe: Lets Learn C by stack1(op):
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

Next we start the main function again the line
[code] int main( void )


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

while(c != EOF){

putchar(c);
c = getchar();
}


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..
ProgrammingRe: Lets Learn C by stack1(op):
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

#include <stdio.h>
int main(void){

char c;
c = getchar();

while(c != EOF){

putchar(c);
c = getchar();
}

getchar();

return 0;
}




program listing 04

#include <stdio.h>
int main(void){

char c;

while((c = getchar()) != EOF){

putchar(c);
}
}

1 2 3 4 5 6 7 8 9 10 11 12 (of 12 pages)