‘Else if’ and ‘If’

The last post explained what I first assimilated and coded. Today’s post will explain two concepts: ‘else if’ and ‘if’ system and ‘factorial’ codes. The picture below shows a project that uses ‘else if’ and ‘if’ systems.

In the picture there are components that were mentioned in the last post. This includes ‘#include <stdio.h>’, ‘int main()’, ‘int a’, brackets, ‘scanf(“”);’, ‘printf(“\n”);’ and ‘return 0;.’ One of the novel concepts in today’s post is ‘if ().’ ‘if’ shows what might happen and how the computer will react to the input and execute it. Another new concept is ‘else if().’ ‘else if()’ states another option besides ‘if.’ Inside ‘else if()’ you can put ‘a == 2’ to tell the computer to react when the input fits the requirement. Once it fits the requirement the computer will print a response. The final notion is ‘else.’ ‘else’ is supposed to be used at the end of the ‘if’ and ‘else’ line. It prints identical output when the input does not meet the conditions of ‘if’ and ‘else if.’ Then you need to put ‘printf(“Does not equal anything\n”);.’

Factorials can be seen a lot with ‘if’, ‘else if()’, and ‘else()’ in a form similar to the picture below.

To make this, make a new function, a function is a group of codes that are working together to complete a task. In the picture it is set as ‘void factorial.’ Second create a parameter. The parameter is the value that passed from the outside to the function. Third make a ‘int i’ variable so that there is space inside the memory. ‘int result = 1;’ shows that there is space inside the memory for ‘result’ and that result equals 1. Fourth make ‘for(i=f; i>0; — i){’ in line seven that states the integer ‘i’ should be equal to ‘f’ more than 0. ‘ — i’ stands that every time it is passed through it should be subtracted by ‘i’ which is 1. In line number eight it says ‘if(i==1){‘ this shows that if the integer ‘i’ is equal to 1 it should do as following. In line number nine it says ‘printf(“%d = %d\n”, i, result);’ this says that ‘i’ is equal to ‘result.’ In line ten it says ‘} else{‘ this shows that it is the last option that is not the ‘if.’ In line eleven ‘printf(“%d * ”, i);’ shows that ‘i’ is factorial calculation. In line twelve ‘result = result * i;’ this shows that the result is changed and is now equal to ‘result’ multiplied by ‘i.’ In line 17 ‘int main()’ is the original function and represents to return the exit status. In line 19 ‘int f’ makes a space for ‘f’ inside the memory. In line twenty ‘scanf(“%d”, &f);’ a command that takes in the input for ‘f.’ In line twenty-one ‘factorial(f);’ is a system that calls the ‘void factorial(int f)’ to start running. In line twenty-two ‘return 0;’ is the return value to main so that it signals the computer that the program has ended.

The result of this program is that when you input 1 on the screen it will execute ‘1 = 1’. If you input a number bigger than one such as 2 it will show ‘2 * 1 = 2.’ Let’s say the input was 12. It will show ’12 * 11 * 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 = 479001600’, but 17 and above is too big that it is beyond the limit of the program that it might come out in random numbers or zero.

This type of program can be used in checking requirements such as checking the age requirement for watching movies. For example if the age is 19 and over and your age is 13 then it will say that you are too young. In short, the above explains the basic analysis of the ‘if()’, ‘else if()’, and ‘else’ and its breakdown. Next post will explain about structs and arrays.