-3
using System;
using static System.Console;
class EnterUppercaseLetters
{
   static void Main()
   {
       string userInput;
       char letter;
       const char QUIT = '!';

       Write("Enter an uppercase letter: ");
       userInput = ReadLine();
       letter = Convert.ToChar(userInput);

       while(letter != '!')
       {
           if(letter >= 'A' && letter <= 'Z')
           {
               WriteLine("OK");
           }
           else
               WriteLine("Sorry - that was not an uppercase letter");
               WriteLine("Enter an uppercase letter or {0} to quit", QUIT);
               userInput = ReadLine();
               letter = Convert.ToChar(userInput);    
       }
   }
}

The code works as intended without brackets around my 'else' statements but creates an infinite loop with them. Am just wondering why that happens?

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
ShruteBuck
  • 23
  • 3
  • 2
    You don’t have brackets say curly braces after else which treats the first statement as else condition remaining statements are are not part of the else statement – Hameed Syed Oct 08 '19 at 03:59
  • Why you define const char QUIT and then while(letter != '!') ? – Mate Oct 08 '19 at 03:59
  • Multilines need to be placed inside curly braces. Only one line expression can be without them. – daremachine Oct 08 '19 at 04:00
  • Welcome to StackOverflow. It can help a lot if you quickly learn how to debug program code. – Jeroen Heier Oct 08 '19 at 04:18
  • 1. Ok “curly braces”. The rest of your response made no sense. – ShruteBuck Oct 08 '19 at 04:20
  • 2. Sorry it was an oversight and your response has nothing to do with the question. – ShruteBuck Oct 08 '19 at 04:21
  • @Shrute You have two answers to your question. If they are not adequate, please state how they fall short or leave some comments for the answerers. Please refrain from complaining that people are trying to help you. Hameed's comment makes perfect sense and is illustrated by the answers you have received. – ProgrammingLlama Oct 08 '19 at 04:37

2 Answers2

2

You need to include curly braces after 'else' if you need to execute multiple lines inside of it. Otherwise it just executes only the first line after the else condition. If you need to include multiple lines inside else, you need to do as below,

 else{
               WriteLine("Sorry - that was not an uppercase letter");
               WriteLine("Enter an uppercase letter or {0} to quit", QUIT);
               userInput = ReadLine();
               letter = Convert.ToChar(userInput);
} 
NSR
  • 315
  • 2
  • 5
  • 18
  • It will execute the last 3 lines regardless of the curly braces. The difference is that it will always execute them, not only as part of the `else`. – ProgrammingLlama Oct 08 '19 at 04:12
  • yeah. According to the way you have coded, the last 3 lines are not considered as it is inside the else statement. It will execute as long as the condition in 'while' is true. If you need to include them inside else condition, you need to apply those three lines inside the curly braces too. – NSR Oct 08 '19 at 04:22
  • I recommend editing your answer rather than leaving clarification as a comment. (P.S. In case it's not clear, I'm not OP). – ProgrammingLlama Oct 08 '19 at 04:36
0
while(letter != '!')
       {
           if(letter >= 'A' && letter <= 'Z')
           {
               WriteLine("OK");
           }
           else
             {
               WriteLine("Sorry - that was not an uppercase letter");
              }
               WriteLine("Enter an uppercase letter or {0} to quit", QUIT);
               userInput = ReadLine();
               letter = Convert.ToChar(userInput);    
       }
   }
}

Note about the closing of curly braces. The normal syntactic saying is that no need of brackets if there is only one statement but it’s always ALWAYS good practice to add flower brackets even if it’s a single statement this makes code more understandable and readable.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Hameed Syed
  • 3,939
  • 2
  • 21
  • 31