Sunday 22 October 2017

Jump Statement:Break,Go To and Continue statement

JUMP STATEMENTS

Jump statements are used to interrupt the normal flow of the program or you can say that the jump statements are used to jump a particular statement or piece of code.

Types of jump statements:-
  • Break
  • Continue 
  • GoTo
BREAK STATEMENT:-

The break statement is used inside the loop or switch case.It is used to come out from the loop or switch case.When we use break statement it terminates the loop or switch case and shows the result.

Syntax:-
.....
.....
break;
To check how break statement works?click here

CONTINUE STATEMENT:-

Continue statement is used inside the loop.This statement is used to skip a particular part of the code and resume the loop.

Syntax:-
while(test Expression) //you can use for loop and do while loop 
{
//codes
if(condition for continue)
{
 continue;
}
//codes
}
Program to print reverse numbers from 10 to 1 except 5:-
#include<iostream>
using namespace std;

 int  main()
{
    for(int n=10;n>=1;n--)
   {
        if(n==5) continue;
        cout<<"\n"<<n;
    }
    return 0;
}
Output:
10
9
8
7
6
4
3
2
1

The above program print numbers from 10 to 1 except 5 because we use continue statement when n==5 that means it will skip 5 and print remaining numbers.

GO TO STATEMENT:-

GoTo statement is another type of jump statement. it can be used to jump from anywhere to anywhere within in the function, sometimes it is also referred as an unconditional jump statement.

Syntax:-
goto label;
.
.
.
label:

Or
label:
.
.
.
goto label;
The label is an identifier.When goto statement is encountered, control of the program jumps to label: and start executing the code.

Program to print the square of positive numbers:-

#include<iostream>
#include<conio.h>
using namespace std;

int main ()
{
    int n;
    float result;
start:
    cout<<"Enter any no:";
    cin>>n;
    if(n<=0)
    {
        goto start;
    }
    else
    {    
        result =n*n;
        cout<<"Square is :"<<result;
   }
    return 0;
}
Output:-
Enter any no:-1
Enter any no:0
Enter any no:2
Square is :4
In the above program, whenever the number is less or equal to zero, it will jump to start statement and ask to enter the number again when the number is greater than zero, it shows the result. 

NOTE: Avoid use of GoTo Statement because it makes the program logic very complex and difficult to modify.

you can use continue and break statement instead of GoTO statement.

Whether you use GoTO statement or not....
If the use of GoTo statement makes your program simpler than you should use it else avoid it.

KEEP CODING....





Friday 20 October 2017

Do While Loop

DO... WHILE LOOP      

Do... while is another type of looping.It is quite different from the for loop but similar to the while loop with one important difference. In do... while loop initialization at the top of the loop, update condition inside the loop and test condition at bottom of the loop.The body of do..while loop is executed once, Before checking the test expression. Hence the do...while loop is executed at least once.

syntax:-
do
{
   // codes
}
while (testExpression);
program to add numbers until the user enters zero:-
#include <iostream>
using namespace std;
int main()
{    

    double number, sum = 0;
    
    do
    {
        cout<<"Enter a number: ";
        cin>>number;
        sum += number;
    }
    while(number != 0.0);

    cout<<"Sum: "<<sum<<endl;

    return 0;
}
Output:-
Enter a number: 5
Enter a number: -5
Enter a number: 21
Enter a number: 0
Sum: 21
Now take a look how do...while loop works?

The code inside the body will be executed once.
Then the test condition is evaluated.If the condition is true then the loop body will be executed again until the test condition becomes false.
when the test condition becomes false, the do...while loop will be terminated.

INFINITE DO... WHILE LOOP:-

#include<iostream>
using namespace std;
int main(){
 
 do{ 
 
  cout<<"hello world"<<endl;
  
  
 }while(1);
 
 return 0
}
Just provide 1 instead of the test condition, the loop will run infinite time.
NOTE:- press CTRL+C to terminate the loop.

NESTED DO...WHILE LOOP:-

#include<iostream>
using namespace std;
int main(){
 int i=0;
 do{
  
  int j=0;
  do{
   
   cout<<j;
   
   j++;
   
  }while(j<=5);
  i++;
  
  cout<<"\n";
  
 }while(i<=5);
 
 return 0;
}
Output:-
012345
012345
012345
012345
012345
012345
In nested do...while another do...while is present inside the body of the loop.It is similar to nested for and while loop.

Let take an example how do...while loop is different from other loops.

#include<iostream>
using namespace std;

int main(){
 
 int i=6;
 do{ 
 
 cout<<i<<endl;
 
 i++; 
  
 }while(i<=5);
 
 return 0;
}
 will this program work? yes,  it will work and print 6 on the screen because in do...while the condition is checked at the end of the program. 
we use do...while loop in game programming because when the game ends, you need to show the scoreboard to the player. we use do...while loop so at least once to render the scoreboard.

HAPPY CODING...




                                            

Saturday 14 October 2017

While Loop

WHILE LOOP

while studying for loop we have seen that all the three parts of the loop are present in a line but in while loop, there is a difference in the syntax.In while loop, we initialize the loop outside the loop then test condition where the loop is defined and update condition in the body of the loop.

syntax:-
initialization expression;
while (test_expression)
{
   // statements
 
  update_expression;
}
program to print even number between 0 to 10:-

#include<iostream>
using namespace std;
int main(){
 int i=0; //initialization 
 while(i<=10)   //test condition
 {
  
  cout<<i<<endl;
  i=i+2;     //update condition
  
 }
 return 0;
}
output:-
0
2
4
6
8
10
In the above program, we declare and initialize i=0, then it will compare the value of i with the test condition, if it is true then the body will execute and give increment to the i and the condition run itself again and again. when i is greater then test condition, the condition became false and the loop will terminate. 

INFINITE WHILE LOOP:-
In this loop, there is no initialization, test condition, and update condition just provide 1 instead of the test condition.

syntax:-
#include<iostream>
using namespace std;
int main(){
 
 while(1)  
 {
 
  cout<<"hello world"<<endl;
 
  
 }
 return 0;
}
Note: you will use CTRL+C to terminate the loop.

NESTED WHILE LOOP:-
In nested while loop, a while loop is present inside the body of the while loop.

syntax:-
 initialization;
 while(test condition)  
 {
  initialization;
      while(test condition)
      {
       //statements
       update condition;
          }
   update condition;
       
 }
HOW TO USE WHILE LOOP IN COMPETITIVE PROGRAMMING:-

program:-
#include<iostream>
using namespace std;
int main()
{
 int t;
 cin>>t;
 int n;
 while(t--)
 {
  cin>>n;
  if(n<10)
   cout<<endl<<"what an obedient servant you are!";
  else
   cout<<endl<<"-1";
 }
 return 0; 
}
Now understand the program, you want to run the program for different test cases, you will use while loop in the above manner.

KEEP CLAM AND CODE C++.....





Tuesday 10 October 2017

How to Install Codeblocks IDE on windows 7 ,8 and 10?

How to install and use codeblocks IDE on windows 7,8 and 10?
hello Folks,
In this post, I will share the steps to download and install codeblocks on windows OS and also share how to use it and compile the program on it.

step by step procedure to install codeblocks

1. As we know codeblocks is a free, open source, cross-platform IDE. To download codeblocks go to  www.codeblocks.org/.



2. Click on downloads and different ways to download will open.

3. Click on Download the binary release. You can download other ways too but for learning C++ binary release is best. 

4.  Different types of files will open then go to codeblocks-16.01mingw-setup.exe file and click on sourceforge.net.your setup will start downloading, the file is about 98Mb.


5. Go to the disk where you download the file.

6.  Double click on the setup file. The system will ask for running permission.click on yes.

7. A setup window will appear. Now click on the NEXT ->I AGREE -> NEXT -> INSTALL.






8. Now it will ask for the permission to run the codeblocks.Click on YES.

9. Now compiler auto detection window will appear.click on OK.

10. Codeblocks will open. Select the environment " yes, associate  code::blocks with c/c++ file types".Your codeblocks will be installed on the system.


How to use it.

1. Click on the codeblocks icon the desktop. it will open.



2. Go to top left corner of the IDE. Click on the FILE  -> NEW -> PROJECT.

3. Now select the application type.In this post, we are making console application so select console application.


4. Console application will open. Select NEXT.


5. Select the language you want to use.For this post, we use C++.


6. Write project name then click on finish.



7. Go to the left side. click on the source. A default main function will open.


Now you can use codeblocks for making C/C++ program.

HAPPING CODING