2330 : Introduction to Programming in C

June 23rd - Dust Loops

(Bigi: Who can tell me what the title today means? haha. It *is* game-related.)
Anyway, as you can see, our main topic today is going to be loops. But before we can do that, you need to understand conditional statements, as that is an essential part of looping. Therefore we will start with the most basic use of conditional statements, which is:
"if" conditional statement
The "if" conditional statement is one of the most basic fundamental elements in programming. It exists in almost any programming languages (it does in all high-level languages, even in a lowest-level one such as Assembly, there are still some form of if conditional statements, because without them, programming is not possible). Most of you should be somewhat familiar or at least aware of what the "if" conditional statement is, as it is quite basic. The basic uses of it will be discussed in this lecture.
Here is the general syntax of an "if" conditional statement for reference:
if ( conditions )
{
	// comment: do stuff here
}
else
{
	// comment: do stuff here
}
Loops
After understanding how conditional statements work, you can begin to use loops. This is because loops work based on conditions. Looping basically means running certain procedures or lines of code repeatedly until certain conditions are met. There are two kinds of loops you will use in C: the "for" loop and the "while" loop. Essentially, any looping you will need can be achieved with either of them, but two of them exist for the convenience of the programmer (you). In this lecture, we will discuss the uses of them.
Here is the general syntax of a "for" loop for reference:
for ( initialization ; conditions ; loop ender )
{
	// comment: do stuff here
}
Here is the general syntax of a "while" loop for reference:
while ( conditions )
{
	// comment: do stuff here
}
Here is the general syntax of a "do-while" loop for reference:
do
{
	// comment: do stuff here
} while ( conditions );

Homework assignment

Continue working on and finish the program you started at the end of class today. That is, write a program that takes in an integer from user input, and print out the digits of this number backwards. Example: User inputs 1579, program outputs 9751.
Hint: Use the modulus operator (%) with a "do-while" loop to achieve this. Remember what Luis wrote on the board in lecture: rightdigit = n % 10;
Read chapter 5 of the textbook, on Functions.