Friday, August 16, 2013

Nested loop in c++

What is nested loop in c++

There are 3 types of nested looping in c++ and these are, nested for loop, nested while and lastly the nested do.. while statement. Nested looping in c++ simply means, loop inside a loop or in other words loop statement within a loop statement. The main and subfolder structure is similar in the nested looping. See the figure bellow.

  • ABOUTMAIN FOLDER
    • C++SUB FOLDER
      • CPPSUB FOLDER
    • MakeSUB FOLDER
    • LearnSUB FOLDER
    • Turbo CSUB FOLDER

The color blue box represents as the main folder and it contains several folders inside, and as you noticed the c++ subfolder contain a subfolder inside, and this kind of structure is the best representation of a nested looping statement in cpp.

The nested looping can handle almost 256 level of nesting statement, how to count the level in nested looping? In the figure above the level one is the blue box entitled About, level 2 is the green boxes and the level 3 is the color red box entitled CPP. The second figure show, how the curly brace identified the looping within the main loop.

  • WHILE(CONDITION)MAIN or PARENT Loop
    • {OPEN Curly brace
      • WHILE(CONDITION)SUB or Child Loop
        • {OPEN Curly brace
          • CODE TO EXECUTESUB or Child Loop code to execute
        • }CLOSE Curly brace
      • CODE TO EXECUTEMAIN or PARENT Loop code to execute
    • }CLOSE Curly brace

Try to hover your mouse on the top of main while(condition), as you notice the color of paired curly braces become grey, it means that all codes or statement enclose by the paired of grey curly braces is instantly executing, it will not stop its execution as long the condition in the main while loop is true. The main while loop contain while loop inside and that is the reason why it become nested, simply loop within a loop. Now try to hover your mouse in the sub while(condition) as you notice the paired curly braces become grey, and codes that will executed is only the codes enclose by the grey curly braces, the code outside of the grey curly braces will not execute or triggered.

The different types of nested looping.

The nested for loop statement in c++ syntax:

for (initialization; condition; increment or decrement)
{
   statement/s; ###YOU CAN PUT MORE STATEMENTS###
   for (initialization; condition; increment or decrement)
   {
      statement/s; ###YOU CAN PUT MORE STATEMENTS###
   }
}

The nested while loop statement in c++ syntax:

while(condition)
{
   statement/s; ###YOU CAN PUT MORE STATEMENTS###
   while(condition)
   {
      statement/s; ###YOU CAN PUT MORE STATEMENTS###
   }
}

The nested do while loop statement in c++ syntax:

do
{
   statement/s; ###YOU CAN PUT MORE STATEMENTS###
   do
   {
      statement/s; ###YOU CAN PUT MORE STATEMENTS###
   }while(condition);

}while(condition);
for (initialization; condition; increment or decrement){
   do
   {
   while(condition)
   {
      statement/s; ###YOU CAN PUT MORE STATEMENTS###
   }
   }while(condition);
}

I hope you learn some something in c++ nested looping statement, and I forgot to mention you can mix those three(3) types of nested loop (nested for loop, nested while loop, nested do while loop) to generate a mix nested loop (code above).

Sunday, August 11, 2013

Calculator in c, c++, cpp or turbo c

How to make an calculator in C++

The source code bellow is the simple sample calculator in cpp, c++ or c. This will serve as tutorial for the beginners who want to learn c language. This simple calculator that I make, allows the user to inputs 2(two) numbers and choose an operation to be used.

Instead of using symbols such as +(add), -(minus), *(multiply) and /(divide), I use letter to represent a single operation.

    This are the following letter that I used:
  • A = Addition
  • S = Subtraction
  • M = Multiplication
  • D = Division

If the user input another letter that does not include to the choices, 'invalid operation' will show.

Basic knowledge

If else statement, declaring variables, data types, printf, scanf,

    Advantage
  • It can compute number with decimal point.
  • Easy to use and understand.
  • It shows the complete answer together with the user’s input.
    Disadvantage
  • Only 2 numbers are allow to input.
  • It can’t perform multiple operations at a time.
  • Decimal point always shows (because I use float data type)

*My Compiler is turbo c.

The calculator source code in c++,c or cpp

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#define p printf
#define s scanf
 main()
 {
 float fnum,snum,ans;
 char operation, ope;
 clrscr();
 p("Simple Calculator \n A=adddition, S=subtraction, M=multiplication, D=division\n");
 p("First number:");
 s("%f",&fnum);
 p("Operation:");
 operation=getche();
 operation=toupper(operation);
 p("\nSecond Number:");
 s("%f",&snum);
  if(operation=='A'){
  ope='+';
  ans=fnum+snum;}
  else if (operation=='S'){
  ope='-';
  ans=fnum-snum;}
  else if (operation=='M'){
  ope='x';
  ans=fnum*snum;}
  else if (operation=='D'){
  ope='/';
  ans=fnum/snum;}
  else p("Invalid Operation!");
  p("%f %c %f the Answer is %f",fnum, ope, snum, ans);
 getch();
}

I hope you like my tutorial in making an easy and simple calculator in C, C++ or CPP. So for the beginners (like me) out there, what are you waiting for try it now!

Sunday, August 4, 2013

Jack N Poy in C++,C,Turbo C

How to make a simple jak n poy program in C++.

First of all you must have the basic knowledge in
  • If else statement.
  • Must be familiar in logical operators.
  • Deriving the variables value.
In code bellow is example of jak n poy program in c++, the process of this code: there are 2 players (player 1 and player 2). The turn starts at player 1 then player 2. The allowed inputs of both players are:
  • P – Stands for paper
  • R – meaning Rock
  • S - for scissors
Either P,R or S are the allowed inputs, if the user tries to input other character the code will terminated or end. Instead of scanf I use getche for the process of input.
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#define p printf
main()
{
 char p1,P1,p2,P2;
 p("Player1");
 p1=getche();
 P1= toupper(p1);
 if(P1=='P' ||P1=='S' ||P1=='R'){
  p("\n Player2");
  p2=getche();
  P2= toupper(p2);
  if(P2=='P' ||P2=='S' ||P2=='R'){
   if(P1==P2)
   p("\N Invalid input player 2");
   else if(P1=='P'&&P2=='R')
    p("\n Player1 wins!\n");
   else if(P1=='S'&&P2=='P')
    p("\n Player1 wins");
   else if(P1=='R'&&P2=='S')
    p("\n Player1 wins");
   else if(P1=='P'&&P2=='S')
    p("\n Player2 wins");
   else if(P1=='S'&&P2=='R')
    p("\n Player2 wins");
   else if(P1=='R'&&P2=='P')
    p("\n Player2 wins");
  }
  else{
  p("\n invalid");
  }
 }
 else{
  p("\N Invalid input player 1");
 }
 getch();
}
My Source code formatter : codeformatting.blogspot.com



The code toupper serve as character converter, it convert the lower case character to uppercase. So that we dont need to make any condition for the difference text format of the user. That is the simple source code of c++ jak and poy.Thank you.