Thread: use of WHILE loop to read the grades

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jackson6612
    What is that bold thing? I understand it's a function call with argument "0".
    It is the direct initialisation of RN with 0.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by jimblumberg View Post
    While not wrong having these extra braces can lead to subtle bugs. These braces denote a block. Anything created inside this block will be destroyed when you exit the block.
    Thanks a lot, Jim. I'm having little difficulty in interpreting the bold part. Are you saying that when I enclose both the if statement and the if block within the braces, then when I get out of the the braces, any data created will be go away? As you see I'm confused, please help me.

    Quote Originally Posted by jimblumberg View Post
    Now I do recommend that new programmers use braces on each and every control statement whether or not they are needed. Now to add braces to your if statement they should look like:
    Code:
    if ( RN >= 10000 )
    {
       break;
    }
    /// OR
    if ( RN >= 10000 ){
       break;
    }
    Okay. I understand it now that the proper way to use the braces for the block of if statement is not to include the if statement within the braces itself.

    You used the word "control statement". I was understand the impression that the if statement is decision statement and the statements such as "while (. . .)" are condition statements. Is the word "control statement" used to refer to both decision and condition statements. Please advise me.

    I'm very much grateful for all the help and guidance.

    Quote Originally Posted by laserlight View Post
    It is the direct initialisation of RN with 0.
    Hi laserlight

    I understand it now. But this format seems confusing and misleading to a novice like me. The things like "RN( )" are called function calls and when put some value within the parentheses it's called argument of the function call. But as we know RN() is not a function call in the present context, at least. So, what do you say on this? Please let me know. Thanks.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  3. #18
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by jimblumberg View Post
    If the braces belong to the if statement yes. The way you had your braces
    Code:
       {
       	  if ( RN >= 10000 )
            break;
          }
    With the braces in this position the braces are not part of the if statement. The if statement is contained in the braces. While not wrong having these extra braces can lead to subtle bugs. These braces denote a block. Anything created inside this block will be destroyed when you exit the block.

    Now I do recommend that new programmers use braces on each and every control statement whether or not they are needed. Now to add braces to your if statement they should look like:
    Code:
    if ( RN >= 10000 )
    {
       break;
    }
    /// OR
    if ( RN >= 10000 ){
       break;
    }
    Do you see the difference?

    Jim
    Quote Originally Posted by jackson6612 View Post
    Thanks a lot, Jim. I'm having little difficulty in interpreting the bold part. Are you saying that when I enclose both the if statement and the if block within the braces, then when I get out of the the braces, any data created will go away? As you see I'm confused, please help me.

    Okay. I understand it now that the proper way to use the braces for the block of if statement is not to include the if statement within the braces itself.
    Hi

    I think two posts from this thread are gone because of recent problem with the forums.

    I'm still struggling to understand what will really happen if I enclose both the if statement and the if block within the braces such as:

    Code:
    {
    if (...)
    //statements
    }
    I understand one can define local variables within the block without making them visible outside the block. Please guide me.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  4. #19
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I'm still struggling to understand what will really happen if I enclose both the if statement and the if block within the braces such as:
    Code:
    {
    if (...)
    //statements
    }
    The primary problem with this brace placement is that if you add another statement after the if that statement would not be part of the if statement. To illustrate the problems that improper brace placement can cause enter the following program and compile.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
       int varToShowInit(1000);
       cout << "varToShowInit before block " << varToShowInit << endl;
       {
          if(false)
             cout << "Part of the if statement. Shoule not print " << endl;
          cout << "NOT part of the if statement." << endl;
          cout << "varToShowInit in block  " << varToShowInit << endl;
          int varToShowError = 100;
          cout << "varToShowError in block " << varToShowError << std::endl;  // Ok, variable still in scope.
          int varToShowInit = 20;
          cout << "varToShowInit in block  " << varToShowInit << endl;
       }
       cout << "varToShowInit after block " << varToShowInit << endl;
       cout << varToShowError << endl; // Error this variable is not defined.
       return 0;
    }
    Note: This code should not compile. Do you understand why?

    Now comment out the line that is causing the error.

    Now this code should compile and run, do you see any possible problems with varToShowInit printed values?



    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while loop within a read function
    By aadil7 in forum C Programming
    Replies: 12
    Last Post: 12-12-2010, 03:33 PM
  2. loop doesn't read files
    By Yumin in forum C++ Programming
    Replies: 5
    Last Post: 05-21-2006, 06:43 PM
  3. Read a file into a loop program
    By TimeClock in forum C Programming
    Replies: 5
    Last Post: 07-17-2003, 06:29 PM
  4. a proper read loop
    By Axolotl in forum C Programming
    Replies: 6
    Last Post: 02-24-2003, 09:46 PM
  5. Replies: 13
    Last Post: 08-15-2002, 09:20 AM