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!
In today's lesson, we are going to learn how to use one if statement within another, and then follow it up with how to use one for looping within another for loop.
There are certain cases where we need to check for multiple conditions, as we have already seen with the AND (&&) operator. However, sometimes, the conditions themselves depend on other conditions. In such cases, we use one if statement within another. Let's take an example.
#include<stdio.h>
void main()
{
int age=24, income=1000;
if(age>=18)
{
if(income<5000)
printf("You are eligible to vote and are exempted from paying tax.");
else
printf("You are eligible to vote.");
}
else
printf("You are not eligible to vote.");
}
http://codepad.org/8Rh5laje
The else on line number 9 is paired with the if in line number 7, and the else in line number 12 is paired with the if in line number 5.
Similarly, we can use one for loop within another for loop.
#include<stdio.h>
void main()
{
int column1, column2;
for(column1=1 ; column1<=5 ; column1=column1+1)
{
for(column2=1; column2<=4 ; column2=column2+1)
{
printf("%i %i \n",column1, column2);
}
}
}
http://codepad.org/kvDENIG5
See the output. At first, the value of column1 is assigned as 1, and now, the value ofcolumn2 changes from 1 to 4. Note that 4 is the maximum value that column2 is allowed to take. Now, column2 is again incremented and becomes 5. However, since column2 is not allowed to take a value of 5, the condition being checked by the second for is not satisfied. This makes us come out of the second for loop and go back to the first for loop. Here, the value of column1 is incremented by 1, and we move to the next statement.
In this next statement, the value 1 is again assigned to column2, and the same process of incrementing the value of column2 while the value of column1 remains unchanged continues until the result 2 4 is displayed. After this, the value ofcolumn1 is incremented again by 1. This process continues until the result 5 4 is displayed. After 5 4 is displayed, the value of column2 is incremented by 1, but we know that column2 cannot be 5. So this condition is not satisfied, and we move back to the first for loop. Here, the value of column1 is incremented by 1, but sincecolumn1 cannot hold the value 6, we come out of this for loop as well and move to the next statement, which, in this program, is the closing curly brackets that end the program.
The second for loop is inside the first for loop, so we call the second for loop theinner loop and the first for loop the outer loop.
The two programs we saw today had just one level of nesting, but you will probably use more than one level of nested statements as you progress to comparatively more advanced programs.