9 Replies - 1273 Views - Last Post: 09 October 2009 - 12:08 PM Rate Topic: -----

#1 kenryuakuma   User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 408
  • Joined: 14-December 08

For loop

Posted 07 October 2009 - 12:05 PM

I know there is a way to make this program run more efficiently, which is the use of for loop, I know how the for loop works, but when it comes to the fact that I have to use it, I surrender because I don't know how to really use it, and the outcome, if using it, will be completely different from what I want.

This is the code I wrote, and it is awkward and not really efficient, I hope somebody could help and optimize it or tell me how to use the for loop to work with it.

using System;
using System.Collections.Generic;
using System.Text;

namespace BooleanExpression
{
	class Program
	{
		static void Main(string[] args)
		{
			// Display what the purpose of this program is
			Console.Write("This is a boolean expression program that compares the user input");

			// Prompt user for input
			Console.Write("\n\nPlease enter a value between 01 and 10: ");

			// convert the user input into byte
			// without exceptional handling
			byte num;
			bool result = Byte.TryParse(Console.ReadLine(), out num);
			
			// Check user input
			if (num > 10 || num < 1)
			{
				Console.Write("Wrong! Please enter a number between 01 and 10: ");
				Console.ReadLine();
			}
			else
			{
				// Display the output of the user input after the check
				Console.Write("\n{0} is greater than {1}, which is {2}", num, 5, (num > 5));
				Console.Write("\n{0} is less than {1}, which is {2}", num, 9, (num < 9));
				Console.Write("\n{0} is equal to {1}, which is {2}", num, 3, (num == 3));
				Console.Write("\n{0} is not equal to {1}, which is {2}", num, 6, (num != 6));
				Console.Write("\n{0} is greater than or equal to {1}, which is {2}", num, 4, (num >= 4));
				Console.ReadKey();
			}
		}
	}
}


This is what I would like the program to work and for those who are interested in helping. Here are the guidelines:

- Make it more run efficiently with a for loop
- Well, stupid program as you can see, when the user types in a number that is less than 01 and greater than 10, the error message is displayed and then prompts the user again for another input, and when the user types in a number, then the program closes in a flash. This is no good. What I'd like to do with this program is if the user types in some stupid stuff, the error message pops up, and if the user keeps doing this, the error message will keep popping up until the user types in the correct or proper number, and the program will continue with it unfinished job, rather than disappear in a flash.

I hope somebody could help really. I really appreciate it.

This post has been edited by kenryuakuma: 07 October 2009 - 12:14 PM


Is This A Good Question/Topic? 0
  • +

Replies To: For loop

#2 Aeternalis   User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 291
  • Joined: 13-July 09

Re: For loop

Posted 07 October 2009 - 12:15 PM

comments removed .. didn't read the bottom portion of OP's post.

This post has been edited by Aeternalis: 07 October 2009 - 12:19 PM

Was This Post Helpful? 0
  • +
  • -

#3 aks29921   User is offline

  • D.I.C Regular

Reputation: 116
  • View blog
  • Posts: 345
  • Joined: 24-August 09

Re: For loop

Posted 07 October 2009 - 12:49 PM

combine for and while loops for your purpose
for (i=0;i<10;i++)
{
while(num[i] > 10 || num[i] < 1)
{
//ask user to enter num[i] and give message
}
}


i think that you have to store 10 numbers, so i have used num as array...
Was This Post Helpful? 1
  • +
  • -

#4 kenryuakuma   User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 408
  • Joined: 14-December 08

Re: For loop

Posted 07 October 2009 - 02:44 PM

is it ok to write a complete code? I mean the main part of the code that I am asking and that would be a great help if possible commented.
Was This Post Helpful? 0
  • +
  • -

#5 SixOfEleven   User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: For loop

Posted 07 October 2009 - 03:16 PM

This isn't a problem that I would use a for loop for. This is a problem for a do while loop. The reason I say this is that you want to keep doing something until a certain condition is met. ie while(something == true). The for loop is more for repeating a task a specific number of times. What you want to do is repeat this code until the user gives you a proper result. You would want to wrap this in a do while loop.

			// convert the user input into byte
			// without exceptional handling
			byte num;
			bool result = Byte.TryParse(Console.ReadLine(), out num);
		   
			// Check user input
			if (num > 10 || num < 1)
			{
				Console.Write("Wrong! Please enter a number between 01 and 10: ");
				Console.ReadLine();
			}



This is a case where the compiler might complain if you don't initialize the variable first.

byte num = 0;
bool result = false;

do {
			result = Byte.TryParse(Console.ReadLine(), out num);
		   
			// Check user input
			if (num > 10 || num < 1)
			{
				Console.Write("Wrong! Please enter a number between 01 and 10: ");
				Console.ReadLine();
			}

} while(result == false && (number < 1 || number > 10));



It is important to note that result needs to be defined outside the loop or it will lose its scope. Because you are using the out keyword the compiler may complain that num was not defined. I don't have C# loaded at the moment so I can't say for sure.

Just another note.

This is not a case of efficiency. This is a case of robustness and error proofing. Efficiency is taking a solution and reducing the instructions/code required. At this point please do not be worried about efficiency. That will come in time. Get used to trying to write robust code that prevents exceptions and your program from crashing.
Was This Post Helpful? 0
  • +
  • -

#6 kenryuakuma   User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 408
  • Joined: 14-December 08

Re: For loop

Posted 07 October 2009 - 03:55 PM

Yes...This is sort of what I want. I tried the for loop, but I wasn't able to come up with a solution, so instead, I just wrote some junk of non-robust code as you said. I forgot that there was a do while loop, I think I now understand how the do while loop works by reading your codes because I wasn't really familiar with the do while loop. I do not have the visual studio with me as well, and I will check out this code by tomorrow. And if I come across with any problem, I will let you know.
Was This Post Helpful? 0
  • +
  • -

#7 aks29921   User is offline

  • D.I.C Regular

Reputation: 116
  • View blog
  • Posts: 345
  • Joined: 24-August 09

Re: For loop

Posted 07 October 2009 - 11:50 PM

Quote

This isn't a problem that I would use a for loop for.

i myself heve used the while loop for this problem, thats the way it ought to be done...
reg the for loop, i only used it to loop through the array, look again carefully at my code above
Was This Post Helpful? 0
  • +
  • -

#8 kenryuakuma   User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 408
  • Joined: 14-December 08

Re: For loop

Posted 08 October 2009 - 09:03 AM

byte num = 0;
bool result = false;

do {
			result = Byte.TryParse(Console.ReadLine(), out num);
		  
			// Check user input
			if (num > 10 || num < 1)
			{
				Console.Write("Wrong! Please enter a number between 01 and 10: ");
				Console.ReadLine();
			}

} while(result == false && (number < 1 || number > 10));


well...When the user input a letter, it keeps looping, but when the user's input is a number which is greater than 10, the program crashes.
Was This Post Helpful? 0
  • +
  • -

#9 SixOfEleven   User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: For loop

Posted 08 October 2009 - 03:05 PM

View Postkenryuakuma, on 8 Oct, 2009 - 10:03 AM, said:

well...When the user input a letter, it keeps looping, but when the user's input is a number which is greater than 10, the program crashes.


Not entirely sure but it might have something to do with the code I gave you.

Okay, I simplified things a little. When you test if the number is in the proper range just set result to false and skip the rest of the condition in the while part as result will be false and the second half of the while condition will not be needed. I did test this code in my IDE and it performs properly.

			byte num = 0;
			bool result = false;

			Console.WriteLine("Please enter a number between 01 and 10: ");
			do
			{
				result = Byte.TryParse(Console.ReadLine(), out num);

				// Check user input
				if (num > 10 || num < 1)
				{
					Console.WriteLine("Wrong! Please enter a number between 01 and 10: ");
					Console.ReadLine();
					result = false;
				}

			} while (result == false);


This post has been edited by SixOfEleven: 08 October 2009 - 03:06 PM

Was This Post Helpful? 0
  • +
  • -

#10 kenryuakuma   User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 408
  • Joined: 14-December 08

Re: For loop

Posted 09 October 2009 - 12:08 PM

Thanks so much!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1