473,386 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

break exit what?

Expand|Select|Wrap|Line Numbers
  1. string name="peter@#%#(*pen";
  2.  
  3. int i=0;
  4. while(name.c_str()[i])
  5. {
  6.     if (ispunct(name.c_str()[i]))
  7.     {
  8.         cout << "Name not correct!" << endl;
  9.         break;
  10.     }
  11.     i++;
  12. }
I want to confirm if the "break;" line exit the if-statement or the while loop.
which one is correct?

if there are more while loop outside the while loop written above, which does the break; line refer to?

are there any other ways that i can use ispunct function for string type data?
Mar 29 '08 #1
6 2757
Ganon11
3,652 Expert 2GB
I want to confirm if the "break;" line exit the if-statement or the while loop.
which one is correct?
The break statement exits the 'most current' loop block. In your example, that is the while loop.

if there are more while loop outside the while loop written above, which does the break; line refer to?
The break statement will exit the innermost loop. For example:

Expand|Select|Wrap|Line Numbers
  1. while (condition1) {
  2.    while (condition2) {
  3.       if (condition3) {
  4.          cout << "If statement." << endl;
  5.          break;
  6.       }
  7.       cout << "End of loop2." << endl;
  8.    }
  9.    cout << "End of loop1." << endl;
  10. }
In this code, if the break statement is reached, "End of loop2" will not print, because the rest of the body of the second loop is skipped. But the first loop is not affected, and prints "End of loop1." before continuing (if condition1 is true, of course).

are there any other ways that i can use ispunct function for string type data?
You can actually just say:

Expand|Select|Wrap|Line Numbers
  1. ispunct(myString[i])
The STL string class has overloaded the square bracket operators to return the character at the specified position, as if it were a character array.
Mar 29 '08 #2
You can actually just say:

Expand|Select|Wrap|Line Numbers
  1. ispunct(myString[i])
The STL string class has overloaded the square bracket operators to return the character at the specified position, as if it were a character array.
Expand|Select|Wrap|Line Numbers
  1. string name="peter@#%#(*pen";
  2. int i=0;
  3. while(name.c_str()[i])
  4. {
  5.     if (ispunct(name[i]))
  6.     {
  7.         cout << "Name has punct!" << endl;
  8.         break;
  9.     }
  10.     i++;
  11. }
Do you mean I can check like this? but what is the condition in the while loop ?
Mar 29 '08 #3
Expand|Select|Wrap|Line Numbers
  1. string name="peter@#%#(*pen";
  2. int i=0;
  3. while(name.c_str()[i])
  4. {
  5.     if (ispunct(name[i]))
  6.     {
  7.         cout << "Name has punct!" << endl;
  8.         break;
  9.     }
  10.     i++;
  11. }
Do you mean I can check like this? but what is the condition in the while loop ?
Actually, u can modify the while(...) test too. Use while(name[i]), when the end of string is reached, name[n-1] will return '\0' which just evaluates to false and the while loop exits, either that or the if(...) test will take care of the exit when it encounters a punctuation character.
Mar 29 '08 #4
Ganon11
3,652 Expert 2GB
You should probably be using a for...loop:

Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < name.length(); i++) {
  2.    // Loop body here.
  3. }
I'm not entirely sure if std::string terminates its character array with a '\0'.
Mar 30 '08 #5
Good point! In my defence though, he used a string literal ("...") and those are null-terminated, so when reached, the while will loop will evaluate to false.
Mar 30 '08 #6
hsn
237 100+
for your code the bread will break you while loop
if you had

while(){

while(){

while(){
break;
}}}

the third while will break
Mar 30 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Michele Simionato | last post by:
Is there a way of "ending" a module? I would like something like this: # mod.py print 'something here' end() # some mysterious function print 'you should not get here' # main.py import mod...
10
by: Scott Brady Drummonds | last post by:
Hi, everyone, I have a bug in a script of several hundred lines of code that I cannot figure out. I have attempted (unsuccessfully) to duplicate this problem in a smaller script that I can post...
3
by: Matt | last post by:
Hi I am trying to figure out how to exit a cursor loop if a specified condition occurs. I have a select count(*) on a table like this select lagplats, count(*) from arsi where artnr =...
8
by: Richard | last post by:
The program I write ask two questions. If user enter of the question invalid or out of range, then the program will exit. My code cannot do that with a break. Can anyone take a look and help. ...
3
by: swingingming | last post by:
If some condition is met, I would like to move to the next record. I tried 'Exit Do', which exits, this is not what i want; i tried 'break' or 'continue loop', none of them works. so, now i used a...
3
by: deko | last post by:
I have a logging routine that's supposed to silently log errors caught by error handler code on certain functions. The problem is sometimes stuff happens and the error handler can get caught in a...
5
by: tony collier | last post by:
To break out of a loop i have seen some people use RETURN instead of BREAK I have only seen RETURN used in functions. Does anyone know why RETURN is used instead of BREAK to kill loops?
5
by: theath | last post by:
hi, i'd like to do something like while(x) if(y) break ... end while what is the equivalent of java's break statement in vb.net? i don't want to use a goto :)
4
by: kenneth6 | last post by:
for(int i=0; i<100; i++) { if(mark==TMark) { return i; cout << i << endl; break; } else {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.