-->





Translate

Showing posts with label do. while. Show all posts
Showing posts with label do. while. Show all posts

Print alphabets from a to z c++ program do while

Leave a Comment
C++ simple program which displays alphabets from A to Z using do while loop. This program simply start the ASCII code from capital which is 65 and keep increment the ASCII code to Z. To do this a char variable is being used and initialized it form 'A' then in while loop condition it checks till character 'Z'.


//this program will print alphabets from A-Z.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{                                //working of program
    int character=0;            //initializing variable of type int from zero to remove garbage value
    char ch='A';                //initializing character type variable from first letter 'A'
    do                         //loop begins and steps 1-5 repeat till ch=Z
    {
         character=int(ch);   //1)character=int(A) =changes value of A to ascii code of type int so
                              // character=65
         cout<<ch<<" ";     //2)print ch=A
         character++;        //3)character=66
         ch=char(character); //4)ch=char(66)=changes value of 66 to alphabet
                            // ch=B
    }
    while(ch<='Z');         //5)check if B<=Z
    getch();
}

program output
print alphabets from z to z c++ code

Try this code to print lower case alphabets from a to z. Hint ASCII code of  'a' is 97.

Find more example here: c++ programming examples and solutions
Read More...

Introduction to Control Structures in C++

7 comments
Control Structures
"Statements used to control the flow of execution in a program".


  • Means a statement through which we control the behavior of our program like what kind of
  • function it will perform, when it will terminate or continue under certain circumstances or conditions.
  • These instructions enable us to group individual instructions into a single logical unit with one entry point and one exit point. To make it more clearer, suppose we have lot of statements in our program by using control structure we control their working like either all statements will evaluate at once on the basis of certain condition or will evaluate one by one or will not be evaluated.

All programs use control structures to implement the program logic.
There are three types of Control Structures
1. Sequence
2. Selection
3. Loops/ Repetition/ Iteration
 Sequence structure in C++:
As name refers instructions  are executed in the sequence in which they are written in the program.
Example: Compound Statement:
              {
                    Statements
                    Statements - 1
                    Statements - 2
                    - - - - - - - - -
                }
 Selection structure in C++:
A structure which select which statement or block of statements will execute on the basis of our
programming logic.
Examples:
1. if - else statement
2.  switch
Loops in C++:
These structures are used to repeat the statement or set of statements.
Examples:
1. for - loop
2. while - loop
3. do - while loop
Read More...