Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

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.

In today's lesson, we will learn about functions. In a program, a function is a group of statements that perform a specific operation.

So far, in each and every C program, we started with a void main().

If you look carefully, you'll notice that the statements contained within main()perform a specific operation. So, going by the definition of a function, main() is also a function.

Notice the brackets next to main. Where else have you seen round brackets just next to a keyword?

Yes, that's right! printf() is another such example. Well, printf() is a function as well. But there's a difference between main() and printf(). Can you guess what it is?

We don't know what the statements that make up printf() are, but we do know what the statements within main() are. This is because the contents of printf()are stored within the stdio.h that we include in every program. It is what we call apre-defined function.

You can create your own functions as well. These are called user-defined functions. Let us create a function that will find out the square of a number—that is, the number multiplied by itself.


#include<stdio.h>

void square();

void main()
{
    int num=5;
    square(num);
}

void square(int num)
{
    int result;
    result = num * num;
    printf("The result is %i", result);
}
http://codepad.org/QLUCPxB7

 

The line void square(); tells the computer that we are about to work with a function called square(). We don't do this with the main(), but we need to write this line for every function that we build on our own; otherwise, the computer will not be able to recognize our function.

The line square(num); means that we are sending the value of num to the functionsquare().

We write the contents of square() in a manner similar to the way we write the contents of main(). The int num inside the round brackets next to void squaremeans that square() has to accept an int (integer) value in the variable callednum.

But we have already declared a variable called num in main(), you say. Why do we have to do this again? Well, that's because whatever you do inside main() stays inside main(). The moment you are outside main(), the values that you had worked with are lost, unless you specifically send a value to another function, which is, in our case, square().

So, the num variable has no existence outside main(). The num variable insquare() is a new variable that is being assigned the same value as the num ofmain(). You can use a different name for the num variable of square() and your program will work just as well: http://codepad.org/I7d4HIJ6.

What square() does essentially is multiply the given number by itself and display the final answer using yet another function, which is printf(). So, we see that a function can call another function to do its bidding.

In tomorrow's lesson, we will learn more about functions. Till then, happy coding!

Building a Nest

Diego Melo
Module by Diego Melo, updated more than 1 year ago
No tags specified