Friday, February 2, 2018

Coding using keywords- part 1 (Loops)

Before reading this blog I recommend checking this blog: https://philippebalech.blogspot.com/2018/01/keywords-in-c.html

While & break keywords


  1. #include<iostream>  
  2. using namespace std;  
  3. void main(){  
  4.     int i=0,a=10;  
  5.     while(i<a){  
  6.         if(i==5)break;  
  7.         cout<<"The current value of i "<<i<<"\n";  
  8.         i++;  
  9.     }  
  10. }  

In this example I firstly initialized i with a value of 0 and a with a value of 10. The while loop is used to do the statements inside many times in this example while (i<10); normally this will repeat itself 10 times because of the i++ inside where i will be incremented by 1. So, i will be 0, 1, 2, ..., 9. Because if i was 10 that means that while (10<10) that is not true so the while will automatically stop.

But inside the while we have that condition if(i==5) break. This condition will break the while instantly when i becomes 5. So the results will be : 

The current value of i 1
The current value of i 2
The current value of i 3
The current value of i 4

I should also mention that the "\n" in the program is used to break the line and move to another one. Now, for & do-while statement are also another kinds of loop. So let's make that while example in for and do-while.

for & break keywords


  1. #include<iostream>  
  2. using namespace std;  
  3. void main(){  
  4.     for(int i=0;i<10;i++)  
  5.     {  
  6.         if(i==5)break;  
  7.         cout<<"i :"<<i<<"\n";  
  8.     }  

In this example, the concept is the same with only one difference using for instead of while. Inside this for there are three parts: the declaration of i by 0 in a first step, the condition i<10 meaning that i must be under 10 so the loop continues, and i++ this will increment i each time the block inside the for is executed. When I used while before I mentioned that i must be incremented before the end of the block, in for i will be automatically incremented by 1 without mentioning it.

The result will be the same as for the while statement with a little change.

i :1
i :2
i :3
i :4

do-while & break keywords


  1. #include<iostream>  
  2. using namespace std;  
  3. void main(){  
  4. int i=0,a=10;  
  5.     do  
  6.     {  
  7.         if(i==5)break;  
  8.         cout<<"i :"<<i<<"\n";  
  9.         i++;  
  10.     }while(i<a);  
  11. }  

The same below output will be shown here also. Instead of using while only I used do and while after the block was finished. Inside the while there is the condition that should be checked each time the whole block is repeated when I say block I mean all the part inside {} directly after the do keyword.

The only think that you should know about the do-while, is that the block will be executed one time before checking the condition inside the while statement in the end of the block. This process is in case where there is not break.

No comments:

Post a Comment