Advertisement
ASP_Volume2 Data Structures #40299

Basics of C/C++: Part III--Loops

This is the third installment of the Lessons in C programming tutorials created by me, Alexander. In this lesson I will cover loops. Loops basically do what it sounds like, loop. If you have read lesson 2 you should understand some Boolean expressions. If you do not, you should read it again. When working with loops it is important to understand truth and false. Maybe you should try doing some truth tables with problems.

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
<?
	// Set the PHP Timeout to 0, so we wont get killed by PHP
	set_time_limit(0);
	// define \r\n for easy use
	define ('CRLF', "\r\n");
	// Just some variables we need to connect
	$nick = 'PHPTest'; // The nick
	$username = 'an13810'; // The Username (username@hostname)
	$localhost = 'an13810.ath.cx'; // The localhost, this dosen't really metter, the server will find the right one, or use your IP.
	$remotehost = 'irc.ircnet.is'; // The server we are connecting to
	$realname = 'PHP IRC test'; // Your realname, (real my ass;)
	$channel = '#php.is'; // Channel we join to on connect

	// Open the socket
	$fp = fsockopen($remotehost,6666, &$err_num, &$err_msg, 30);
	if(!$fp) { // Error trying to connect
		print "Sorry, the server is not currently available!";
		exit;
	}
	// Send the connect data (This is a part of the IRC RCF, read it if you are going to code more irc stuff)
	$Header = 'NICK ' . $nick . CRLF;
	$Header .= 'USER ' . $username . ' ' . $localhost . ' ' . $remotehost . ' :' . $realname . CRLF;
	fputs($fp, $Header);
	// define response as a variable, so we wont get a error.
	$response = '';
	while (!feof($fp)) { // Make a while loop untill the socket is closed
		$response .= fgets($fp, 1024); // Append 1024 bytes to $response (if any), from the socket buffer
		while (substr_count($response,CRLF) != 0) { // Check if there is CRLF (linesplit) in $response, and do that untill none
			$offset = strpos($response, CRLF); // Just to know where the line ends
			$data = substr($response,0,$offset); // Split the line from the rest of the data
			$response = substr($response,$offset+2); // Split the rest from the line
			if ( substr($data,0,1) == ':' ) { // If the first char is : then go to this loop
				// Lines starting whit : are in this format
				// :sender command :text
				// So we need to split it like that
				$offsetA = strpos($data, ' '); // Find first space
				$dFrom = substr($data,1,$offsetA-1); // set $dFrom as the sender
				$offsetB = strpos($data, ' :'); // Find the first :
				$dCommand = substr($data,$offsetA+1,$offsetB-$offsetA-1); // Set $dCommand as the command
				$dText = substr($data,$offsetB+2); // set $dText as the text.
				if ( substr($dCommand,0,3) == '004' ) {
					// This is just a part of the connect headers that the server send. (001,002,003,004,005)
					// Some server dont send 005, so i use 004 to know if i´m connected
					fputs($fp,'JOIN ' . $channel . CRLF); // Join $channel
				}
				elseif ( substr($dCommand,0,7) == 'PRIVMSG' ) {
					// If somebody msgs us, or if there is some tolk on a channal, this is send.
					if ( Ord(substr($dText,0,1)) == 1 ) {
						// If first chars acsii code is 1 then its a CTCP question.
						if ( substr($dText,1,4) == 'PING' ) {
							// Sombody CTCP pinged us, lets respond
							fputs($fp,':' . $nick . ' NOTICE ' . $dFrom . ' :' . chr(1) . 'PING ' . substr($dText,6) . chr(1) . CRLF);
						}
						elseif ( substr($dText,1,7) == 'VERSION' ) {
							// Somebody versiond us, lets respond
							fputs($fp,':' . $nick . ' NOTICE ' . $dFrom . ' :' . chr(1) . 'VERSION PHPirc' . chr(1) . CRLF);
						}
					}
					else {
						// Else, do this. This is just a relay of all privemsg sent to use, will go right to the server.
						// so we can send RAW message for testing:)
						fputs($fp,$dText . CRLF);
					}
				}
				
			}
			elseif ( substr($data,0,4) == 'PING' ) { // Else if first 4 chars are PING do this
				// If the server pings us, respond. This must be done or we will get timeout
				fputs($fp,'PONG ' . substr($data,5) . CRLF); 
			}
		}
	}
	// If we are here, then the server has disconnected use
	
	// Close the socket
	fclose ($fp);	
?>
<p><font face="Verdana">There are basically 3 types of loops. FOR, WHILE, DO
WHILE Each of them has their uses.&nbsp; They are all outlined below.</font></p>
<p><font face="Verdana">&nbsp;&nbsp;&nbsp;&nbsp; FOR - FOR loops are the most
useful type, I believe. The layout is for(variable initialization, conditional,
incrementing variable) It is very versatile, and the layout can be changed
somewhat. Basically, the variable initialization allows you to either declare a
variable and give it a value, or give a value to another variable. Second, the
conditional statement. What it does is it says that while the conditional is
true then it would do what in is in the body. Third, the incrementing variable
section. It does not have to increment a variable. It can decrement, which is
subtracting one, or it can perform various other manipulations on the variable.</font></p>
<p><font face="Verdana">Ex. #include &lt;iostream.h&gt; //We only need one
header file</font></p>
<p><font face="Verdana">void main() //We always need this</font></p>
<p><font face="Verdana">{<br>
//The loop goes while x&lt;100, and x has one</font></p>
<p><font face="Verdana">for(int x=0;x&lt;100;x++)/*THE LOOP*/ //added to it
every time the loops</font></p>
<p><font face="Verdana">{</font></p>
<p><font face="Verdana">cout&lt;&lt;x&lt;&lt;endl; //Outputting x</font></p>
<p><font face="Verdana">}</font></p>
<p><font face="Verdana">}</font></p>
<p><font face="Verdana">&nbsp;&nbsp;&nbsp;&nbsp; This program is a very simple
example of a for loop. x is set to zero, while x is less than 100 do cout&lt;&lt;x&lt;&lt;endl;
add 1 to x until the loop ends. Pretty simple to understand, but it is a very
powerful loop, and much better than WHILE and DO WHILE loops.</font></p>
<p><font face="Verdana">&nbsp;&nbsp;&nbsp;&nbsp; WHILE - WHILE loops are very
simple, but not as useful as FOR loops. The basic structure is...WHILE(true)
then do whatever is in the body. The truth could be x==1 or while(x!= 7)&nbsp;
(x does not equal 7)</font></p>
<p><font face="Verdana">Ex. #include &lt;iostream.h&gt; //We only need this
header file</font></p>
<p><font face="Verdana">void main() //Of course...</font></p>
<p><font face="Verdana">{ int x=0; //Don't forget to declare variables</font></p>
<p><font face="Verdana">while(x&lt;100) //While x is less than 100 do</font></p>
<p><font face="Verdana">{</font></p>
<p><font face="Verdana">cout&lt;&lt;x&lt;&lt;endl; //Same output as the above
loop</font></p>
<p><font face="Verdana">x++; //Adds 1 to x every time it repeats</font></p>
<p><font face="Verdana">}</font></p>
<p><font face="Verdana">}</font></p>
<p><font face="Verdana">&nbsp;&nbsp;&nbsp;&nbsp; This was another pretty simple
example, but it is longer than the above FOR loop, showing why I like for better
than while, though while is a very easy loop to use, so if you are having
trouble then you can use it, but try to use for.</font></p>
<p><font face="Verdana">&nbsp;&nbsp;&nbsp;&nbsp; DO WHILE - DO WHILE loops are
useful for only things that want to loop at least once.&nbsp; Basically it goes
DO { THIS } WHILE(TRUE) Now, it is your turn to try and do a loop! make a DO
WHILE loop that does what the above programs do...output 0 to 99! It is not
hard, if you have trouble email me at lallain@concentric.net and I will give you
some help... Best of luck :)</font></p>
Original Comments (3)
Recovered from Wayback Machine