C++

Objectives :

  • File Name : string_isalpha_null1.cpp
    Introducing NULL values and break statement in loop.
  • Break an iteration when NULL :
    //if(ch1[i]==NULL){ break; }
    if(ch1[i]=='\0'){ break; }
     
  • Code: string_isalpha_null1.txt
 

Step: 1 Create a source file.

Step: 2 Edit and save Source file

Step: 3 Runtime Views: In this example we are breaking out a for loop, when cursor hits a NULL character; in absence of this break compiler will generate gibberish characters, as we have seen in this example (Link )

Step: Brief Discussion:

When a character sequence of fixed length is supposed to get user's input which should be 20 characters length, but  but receives less number of characters, the rest of the spaces will have NULL values and the printer will print all including NULL values. As a result we see gibberish output, shown in the following two screenshots



We can instruct a printer or screen display to break out of the loop after encountering a NULL. The image below shows the effect of break statement.