Welcome back!
Today's lesson will focus on commonly used operators that you will need for writing Cprograms. But before we do that, we are also going to get familiar with something called if statements.
An if statement checks whether a particular condition is true and performs the corresponding action. Let us take an example:
#include<stdio.h>
void main()
{
int a=5;
if (a>4)
printf("a is greater than 4");
}
http://codepad.org/s8gh7YhE
This program checks if the value of a is greater than 4, and if it is indeed greater than4, it will do whatever is written in the statement immediately afterward and before the next semicolon. You can read it as “if a is greater than 4, display 'a is greater than 4'”.
See the > symbol? It is an operator. The equality symbol (=) is also an operator.
Operators are symbols that tell your computer to perform a specific operation. For example, the + operator performs addition.
List of common operators:
< : less than
<= : less than or equal to
>= : greater than or equal to
!= : not equal to
== : equality comparison
The equality comparison operator (==) is used to compare two values, while the equality operator (=) is used to assign a value. Let me clarify with an example:
#include<stdio.h>
void main()
{
int a=5;
if (a==5)
printf("a is equal to 5");
}
http://codepad.org/1HLFcDO4
Note that when we write a=5 we are assigning the value 5 to the variable a, but when we write a==5, we are checking whether or not the value of a is equal to 5. A common mistake is when people write if(a=5) instead of if(a==5). What happens if you do this is the value 5 gets assigned to the variable a instead of checking whether or not the value of a is equal to 5, which ultimately leads to incorrect results.
Other commonly used operators that you should know about are the logical AND and OR operators.
The AND operator is specified by the symbol && and checks whether both the conditions specified are true, while the OR operator is specified by the symbol ||and checks if at least one of the conditions specified is true.
Let's see an example:
#include<stdio.h>
void main()
{
int a=5;
if(a>0 && a<10)
printf("This is a positive non-zero number less than 10");
}
http://codepad.org/9uVBShRZ
Now what happens if we need to do two different things based on whether or not a condition is satisfied? Well, C has got your back. Just use the else keyword.
#include<stdio.h>
void main()
{
int a=5;
if(a==10)
printf("Number is equal to 10");
else
printf("Number is not equal to 10");
}
http://codepad.org/a02qfnNZ
You can make your program do more than one operation if it satisfies a condition. In such cases, you need to enclose all the statements within curly braces.
#include<stdio.h>
void main()
{
int number1=10, number2=5;
if(number2!=0)
{
printf("%i \n", number1/number2);
printf("Division successful!");
}
else
printf("Cannot divide by zero!");
}
http://codepad.org/hrdk5oFk
That's all for today. Look out for tomorrow's email for the next lesson. Till then, keep practicing.
Hello! Welcome back to Getting Started with C Programming! Just a quick reminder: you are already halfway through the course. Yay you!
When writing programs, sometimes we need to repeatedly perform the same operation. For example, you want your program to display the numbers from 1 to 100. One way to do that would be to use hundred printf statements. But you are coder, and you are going to use a loop statement.
#include<stdio.h>
void main()
{
int number=1;
while (number<=10)
{
printf("%i \n", number);
number = number + 1;
}
}
http://codepad.org/zetRhVhe
This is one of the simplest loop structures. The computer checks if the number is less than or equal to 10. If it satisfies this condition, the number is displayed, and then incremented by 1. This process continues until the number 10 is displayed. Since the statement for incrementing the number by 1 is present immediately after the display statement, the number becomes 11 and the while loop is again executed. This time, the condition is not satisfied, so we come out of the while loop and go to the next line. As we don't have any other statement after this, the program ends when it reaches the final closing curly bracket.
Another commonly used loop structure is the for loop.
#include<stdio.h>
void main()
{
int counter, number=10;
for(counter=1 ; counter<=10 ; counter=counter+1)
printf("%i \n", counter);
}
http://codepad.org/oCa
This program does everything that the previous program did but is more compact. See the for statement. At first, the value 1 is assigned to the variable counter, followed by a semicolon to signal the end of value assignment. Then, the maximum value that counter is allowed to reach is specified. And finally, the last part increments counter by 1 for each iteration of the loop.
The assignment of value to counter happens exactly once. Then the condition specified by the for statement is checked, and if the condition is satisfied, the computer performs the statements contained within the for loop—that is, in our case, display the value of counter. Next, the third and final part of the forstatement is performed; that is, counter is increased by 1, and the computer goes back to checking the condition specified by the second part of the for loop. This goes on until the condition is not satisfied, at which point we come out of the for loop and move to the next line in the program code.
The for loop may seem slightly confusing at first, but once you get the hang of it, you will find yourself using it more than the while loop.
See you tomorrow with more on loops. Till then, happy coding!