Print till Z with while loop boundary crossing – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Code Learning #CPP#2038 siteicon   siteicon   siteicon  

Print till Z with while loop boundary crossing

As user enters a lower case or upper case alphabet the program prints from that alphabet till z using the faster technique of while loop iteration and checking for outside boundary condition

Learning Objectives

  • Use of while loop with over crossing the boundary condition.

Source Code

TC++ #2038

Source Code

Alternate (?) :

Run Output

Enter an alphabet :R
RSTUVWXYZ

-OR-
Enter an alphabet :e
efghijklmnopqrstuvwxyz

-OR-
Enter an alphabet :4
Entry was not an alphabet

Code Understanding

Here we will use a simple if-else-if ladder for checking of uppercase or lowercase alphabet and if it is not a an alphabet we shall come out of program.

if(ch>=65 && ch<=90) while(ch!=91) cout<< ch++;
Here we check for uppercase alphabet which is in ascii code range 65 to 90 (both inclusive). Incidentally the value 91 is just outside the boundary of the last condition. Here we use this outside boundary condition as a check condition for the while loop so that the loop can run till the bouundary and as soon as it reaches outside boundary it terminates. Since boundary is at character Z so the character from the current character till the last character is printed since with every iteration using ch++ it is reaching the next character. It is interesting to note that ++ operator is working on char data type also as for arithmetic operation the char data type is treated just like an integer numeric value.

else if(ch>=97 && ch<=122) while(ch!=123) cout<< ch++;
Here the check for lower case character are done in the ascii range 97 to 122. The just outside last boundary condition is 123 here for termination of while loop

else cout<<“Entry was not an alphabet”;
Here the non-alphabet condition message is being given.

 

Notes

  • The just outside boundary check for loops is possible in this case as a valid ascii code exists for 91 and 123. This kind of checking may not always be possible if the similar thing is being done for array boundaries.
  • This program is fast, but students may tend to check for wrong conditions and loops may become non-terminating.
  • There are many ways to write this program. One can see an alternative solution by clicking the alternative solution button.

Common Errors

  • Loop boundary conditions should be correctly set after understanding it correctly else endless loops can make your programming environment hang.


Suggested Filename(s): print2zwb.cpp



Share

sunmitra| Created: 30-Dec-2017 | Updated: 15-Sep-2018|






×
Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Mult-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues


Back