C Programming for Beginners

For Loop , Break and Continue

For Loop in C, Break and Continue

For Loop, Break and Continue

In the last chapter, we have studied while loop. In this chapter, we will study about for loop in C.

Well, basic is the same for every loop. Every loop requires initialization, condition and increment/decrements.

For Loop in C

Let’s write a small program. Now we need to print 1 to 10. Here we have main, declare a variable i. FOR, in curved brackets, we write i=1, then semicolon, then i < 10, then semicolon and then increment. In curly brackets, we write printf statement to print i.

main()
{
int i;
for (i=1; i<10; i++)
{
printf(“%d “, i);
}

}

You can view the source code from Newtum github repository  link over here.

Output :

1 2 3 4 5 6 7 8 9

Look at the code. This is “FOR” statement. Well, the only difference between a WHILE loop and a FOR loop is the way they are written. In for loop, we have initialization, condition, and increment in the same line separated by semicolons.

we have initialization, condition, and increment in one line and then we have statements in curly brackets.

Let’s open our source code again. Lets quickly see how this program works. When the program first reaches the FOR statement, it sets the value of i, that is 1. Then it goes for the condition. Since i is less than 10, it goes inside FOR loop.  And it printed the value 1, then it goes to increment. It increments the value of i becomes 2 and then again it goes to condition. If the condition is true then it prints the printf statement and then goes to increment.  This process will go on until it reaches the value of 10. And the loop will break.

While using for loop in C Programming please make a note that initialization, this part, is executed only once at the start of the loop and thereafter only condition and increments are checked. 

Remember, in IF and ELSE statements we don’t have to use curly brackets, if we have only one statement. Similarly, in case of FOR loop or WHILE loop, you can ignore the curly brackets if you have only one statement. So we remove curly brackets and rerun the program. We will get the same output.

Here make a note, in FOR loop in C Language these two semicolons are must even if you don’t write condition or increment.

Open our last example of FOR loop. We are removing the increment part over here and then we will put the increment part inside the FOR loop statements. Now run the program.

main()
{
int i;
for (i=1; i<10;)
{
printf(“%d “, i);
i++;
}

}

Output :

1 2 3 4 5 6 7 8 9

Program printed 1 to 9.  This was a small variation in loop that we can have. Please remember these two semicolons in FOR loop. 

Now we will have a look at the break statements. As the name suggests break statement is used to break the loop. Suppose you want to print 1 to 20. 

Let’s write a small program for this again, we have main, variable i and then we have a loop. Inside FOR loop, we will write printf to print i.

Look at the FOR loop statement. Here we will remove the condition.Well without condition the program will run infinite times.  Because if condition is not met , program won’t stop. 

To handle this we will use a BREAK statement inside FOR loop. Inside FOR loop we will write “if” condition that is if i >=20 , then break. Lets run the program. And here is our correct output.

main()
{

int i;
for (i=1; i<20; i++)
{
printf(“%d “, i);
if (i >=20)
{
break;
}
}

}

You can view the source code from Newtum github repository  link over here.

Output :

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

Open the source code again. The only change was condition and break statement. We have removed the condition and we have broken the loop using break statement in this condition.

Lets execute one more example to get more clarity on break statement.

Do you know prime numbers? 

A prime number is a number which is only divisible by 1 or itself. Like 2 ,3,5,7,11,13 all are prime numbers.

Now we will write a program which will determine whether a number is prime or not.

To develop this program, we need to write a loop which will divide the input number by every possible number. If no division possible then the number is prime else number is not a prime number.

Let’s write the program. We will have main, we will declare two variables , first is i to manage the loop second is a number to accept the input from the user. 

Then we write printf statement asking for user input  and then a scanf statement to accept user input.

Now let’s write a FOR loop which will check for division. Here is a FOR loop with initialization, condition and increment. Please make a note we have initialized with i = 2 , because a prime number is divisible by 1 or itself.

Inside for loop we will write condition which checks whether a number is divisible by every value of i or not. Number MOD i.  If it’s divisible by any number we will print “Not a Prime Number” and we will break the loop.

Outside FOR loop we will write if the number is equal to i then number is a prime number. We will iterate to understand how this works but first let’s just run it.

main()
{
int i, number;
int mod;
printf(“Determine whether a number is prime or not\n”);
printf(“Enter value for Number : “);
scanf(“%d”, &number);

for(i=2 ; i<number; i++)
{
mod=number%i;
if(mod == 0)
{
printf(“%d is Not a Prime number.\n”,number);
break;
}
}
if(number==i)
{
printf(“%d is a Prime number.”, number);
}
}

You can view the source code from Newtum github repository  link over here.

Enter the input 4.

Output :

 It says not a prime number.

Enter a number : 4
4 is not a prime number.

 Now run again and enter input as 5. 

Output :

It says “It’s a prime number

Enter a number : 5

5 is a prime number.

How did the computer determine this? When you entered number 4, loop started with i  = 2 , So system checks whether what is “number mod i” in this line. Well number 4 is divisible by 2. Well it immediately display that Number is Not a prime number and break from the loop. No further execution occurs. Value of i is still 2 , but still we broke the loop using break statement.

When system reaches this condition outside FOR loop , it checks if the number is equal to i or not. Well, the answer is no. Then system exits the program.

Consider other iteration. Now you entered value 5.

System enters the loop. It checks whether its divisible by 2 or not. Answer is negative. It checks with 3. Answer is negative. Again with 4.

Now value of I becomes 5. But here the loop breaks because we have written condition i < number i.e. I < 5.

After executing FOR loop, system checks whether “i == number” or not. Answer is true. And system prints number is a Prime Number.

Trick over here is , if the number is prime, counter will never reach the number value and will break before. If it’s a prime number then system will reach the end until value of  i becomes 5.

Well this was about for break statement.  

BREAK statement

Just like BREAK statement we have CONTINUE statement.

Suppose you want to print multiplication table. I guess you have done this many a times in school.

Well lets write the program for this. We have main , we will declare 3 variables  i , input and result. We will declare one more variable “response” as character.

Then we will write FOR loop. Inside FOR loop we will calculate the multiplication …… and print using printf statement to display the result. We will display the print with value of i , input and then result. Just like we used to write in our home work books.

main()
{
int input, i,result;
printf(“Enter an number: “);
scanf(“%d”,&input);
for(i=1; i<=10; ++i)
{
result = input * i;
printf(“%d * %d = %d \n”, input, i, result);
}
}

You can view the source code from Newtum github repository  link over here.

Just execute it. Enter the input 5. Yes we got the perfect output.

Output :

Enter an number: 5
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50

Now suppose you want to print many tables like table of 5, 11 , 23 etc. without stopping the program. What will you do?

We will write the condition , that when i > 10 , ask user if you want to continue or not. User will enter Y for Yes or N for No. We will store the input in response. If user says Y ,we will ask for input and then we will use the continue statement. Continue statement takes back the FOR loop at initial stage.

Now system asks “do you want to continue or not”. We will say  Y for Yes. Then enter the input as 9. We got our multiplication tables. It will go on until you say No to continue.

well this was about for loop , break and continue statements.

in the next chapter we will  study nested for loops and do while loop.

For becoming  a good developer don’t break the loop and keep  watching our videos and read blogs. And yes keep practicing.

 

Facebook Comments
(Visited 1 times, 1 visits today)