If, Else if, Else

You can make functions with if, else if, else statements. They are used to run conditional statements. 
Case 1 = if (if conditions of case 1 are true then outcome 1)

Case 2 = else if ( if conditions of case 2 are true then outcome 2)

Case 3 = else (everything else, output 3)

The conditions for the outcomes are set in the parentheses. There are several ways to do this:

var 1 is less than var 2 (var1 < var2), remember this will not include equal to. To do that use (var1 <= var2).

If you want it to be anything but a value use (var1 =! var2) this is the equivalent to var1 equals not var2.

If you want to include two conditions you can use “&”. (var1 == var2 && var 1 == var3). If both of these conditions are not met it will not run the loop.

If you want the same loop to run for either condition you can use an or statement. (var1 == var2 or var1 == var3)

Let’s write an example!

#include <stdio.h>

#include <stdlib.h>

int A = rand()%10 /*this rand function makes a random number the %10 makes the bounds be within 0 and 9. A will be any number between 0 and 9. Making a random variable */

char B [0];//we are initializing an array 1 space long named B

int main(){

scanf(“%s”, B);/*scanf is a function which reads in user input length 1 
and is assigned to int B. You may input any value for B and if it matches
 your statement it will be true.*/

if (A == 3){

printf(“A was equal to 3”);

}

else if (B [0] == ‘A’){ printf(“Array B is equal to A”);}

//else if b at position 0 is equal to A print…  but waitttt
//since A is in the apostrophes that means that it is the character A not the variable A

else{ printf(“A is actually\t %i”, A);}/*%i, A means its printing the integer A's actual value*/

return 0;}

Conclusion

Alrighty that was a tough one. Arrays (covered in the first few weeks of class) are tricky at first and if you need help please see http://stackoverflow.com/questions/20347170/char-array-and-char-array. So first we created a random integer between 0 and 9 (the rand function produces value starting at 0 and going up 10 integers. i.e. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9) knows as A. Then we read in user data into a character array. These variables are then run through our if else statement. If A is equal to 3, console prints, “A was equal to 3” If B was equal to the character A (remember this is not the variable A it is the character that the user puts into the code) prints, “array B is equal to A” and if both of these conditions are not true the it prints the value of the variable A.

Additional Information

These variables were a bit tricky to keep track of, but it is important to pay attention to it. This is also a good lesson to make sure to give your variables names that you can remember. Feel free to play around with these variables in your script to make them easier to recognize.

Also the rand function will be talked about at the end of the semester, however it proves a valuable tool in testing to make sure that your code is working correctly without bringing in user input. These are only 2 ways to bring in user input. C allows for a much easier way of importing and exporting data to the console by writing #include <iostream> then data in for an integer value previously defined is cin>>value>> name of integer>>\n; and writing to the console with cout<<”text %i”<< name of integer<<\n;. scanf is an effective operand for reading in user input in c++. It assigns user input to a variable.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s