## Loops and Conditions

Real programs can do more than just getting input, performing some math, and then printing out the results.

 ``` if (condition_1) { ...block of statements executed if condition_1 is true ...statements are either more ifs and conditions or normal normal lines of code that end with semi-colons } else if (condition_2) { ...statements executed if condition_2 is true and condition_1 was false } else { ...statements executed otherwise meaning 1 and 2 were false } ``` and any variant that derives from the above. Either by omitting branches or by including nested conditionals. Meaning having an if inside of another if. Now that we can make an if we need to make the condition that goes inside of the parenthesis of the if. Conditions are logical operators involving the comparison of quantities (of the same c type). Here are a list of the basic conditional operators. ``` < smaller than <= smaller than or equal to == equal to != not equal to >= greater than or equal to > greater than ``` and the boolean operators ``` && and || or ! not ``` Now that you know the operators here are some sample conditions. For instance using the above example. ```if (numcandy > 1000) { printf("The store does not have that much candy in stock!"); } else .... print out the cost ..... ``` The boolean operators are for combining more than one condition into a single if statement. So for instance ```if (money>500 && cost<10) ... ``` This condition would only be true if both money was greater than 500, and cost less than 10. If you replace the && with || then the condition would be true if either money is bigger than 500 OR cost less than 10. I think you get the picture. Another couple things to notice about conditional statements in C is that the equality operator is == (two equals) and not = one equals sign. The single equals sign as we learned in the previous lesson is the assignment operator. You use it to assign values to variables so when you want to test if a variable is equal to something use two equals signs == and not one. Remember THIS! It is very important. Next with C conditional statements you can also omit (leave out) the operator. if you had a conditional statement: ```if (numcandy) {...} ``` This would evaluate to true and the statements inside the {} brackets would be executed, if numcandy was not equal to zero. Understand? If you have no operators inside the parenthesis then C will simply check to see if the value is anything but zero. Another conditional use is in the switch construct: ``` switch (variable) { case const_1: { ...block of statements... break; } case const_2: { ...block of statements... break; } default: { ...block of statements.. } } ``` The appropriate block of statements is executed according to the value of the expression, compared with the constant expressions in the case statement. The break statements insure that the statements in the cases following the chosen one will not be executed. If you would want to execute these statements, then you would leave out the break statements. This construct is particularly useful in handling input variables. Here is an example, if you had a integer variable called race position.```switch (race_position) { case 1: { printf("First place, you get gold!\n"); break; } case 2: { printf("Second place, you get silver!\n"); break; } case 3: { printf("Third place, you get bronze!\n"); break; } default: // all other numbers (anything but 1,2,3) { printf("You dont get anything, you lose!\n"); break; } } // end switch``` You could have easily done this same thing with three if's and and else. But we wanted to demonstrate the switch. As an excersize rewrite the above code segment to use if's instead of the switch. That's it for conditional statements, now we come to the next part of lesson three loops. C provides two basic types of loops a while and a for loop. While and for loops continue to repeat basically until certain conditions are met. Here is the syntax for while and for loops: ``` while (condition) { ...block of statements to execute... } ``` and the for loop: ``` for (expression_1; expression_2; expression_3) { ...block of statements to execute... } ``` The while loop continues to loop until the condition becomes false. The condition is tested upon entering the loop. Any logical construction (see below for a list) can be used in this context. The for loop is a special case, and is equivalent to the following while loop: ``` expression_1; while (expression_2) { ...block of statements... expression_3; } ``` For instance, the following structure is often encountered: ``` i = initial_i; while (i <= i_max) { ...block of statements... i = i + i_increment; } ``` This structure may be rewritten in the easier syntax of the for loop as: ``` for (i = initial_i; i <= i_max; i = i + i_increment) { ...block of statements... } ``` Infinite loops are possible (e.g. for(;;)), but not too good for your computer budget! C permits you to write an infinite loop, and provides the break statement to ``breakout'' of the loop. For example, consider the following (admittedly not-so-clean) re-write of the previous loop: ``` angle_degree = 0; for ( ; ; ) { ...block of statements... angle_degree = angle_degree + 10; if (angle_degree == 360) break; } ``` The conditional if simply asks whether angle_degree is equal to 360 or not; if yes, the loop is stopped. Finally, to wrap up this lesson we will introduce #define's and then give the sample program. You can define constants of any type by using the #define compiler directive. Its syntax is simple--for instance ```#define ANGLE_MIN 0 #define ANGLE_MAX 360 ``` would define ANGLE_MIN and ANGLE_MAX to the values 0 and 360, respectively. C distinguishes between lowercase and uppercase letters in variable names. It is customary to use only capital letters in defining global constants. Then inside your program you could insert ANGLE_MAX anytime you wanted to use the number 360. Now here is the sample program. You should be able to understand all the code there in. Basically there is a #define for a number of lollipopps, and you sell lollipopps until you run it, but there is also a limit too how many you can get at one time. Then after all are sold, the amount of money for all purchases is printed out. ```#include #define START_NUMBERLOLLIPOPS 100 #define MAX_AT_ONCE 30 void main() { int numcandy; double cost; int numberlollipopps; double totalcost; numberlollipopps = START_NUMBERLOLLIPOPS; // set start value to our constant totalcost = 0; while (numberlollipopps > 0) { printf("(%d left) How many lollipops do you want (-1 quits): ", numberlollipopps); scanf("%d", &numcandy); if (numcandy == -1) // since this if has only one statement brackets are not needed break; // exit out of the while loop else if (numcandy > MAX_AT_ONCE || numcandy <= 0 || numcandy > numberlollipopps) { printf("You cannot have that many, enter another number\n"); } else { cost = 0.55 * numcandy; printf("\nPlease pay \$%.2f to the cashier!\n", cost); totalcost = totalcost + cost; numberlollipopps = numberlollipopps - numcandy; } } printf("All the lollipopps have been sold for : \$%.2f\n", totalcost); } ```