Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,537 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,767 people online right now. Registration is fast and FREE... Join Now!




Using &&-operator inside a for-statement

 
Reply to this topicStart new topic

Using &&-operator inside a for-statement

Zeddicus
17 Aug, 2008 - 09:15 PM
Post #1

New D.I.C Head
*

Joined: 14 Jul, 2008
Posts: 41


My Contributions
I want to print out the value in index [3], 27, but cannot make it work. Am I using the AND-operator wrongly or is it something else?

CODE
   int[] ints = new int[4];
            ints[0] = 24;
            ints[1] = 25;
            ints[2] = 26;
            ints[3] = 27;
          

            for (int i = 0;i < 4 && i > 2; i++)  
                Console.WriteLine(ints[i]);



User is offlineProfile CardPM
+Quote Post

Zeddicus
RE: Using &&-operator Inside A For-statement
17 Aug, 2008 - 09:28 PM
Post #2

New D.I.C Head
*

Joined: 14 Jul, 2008
Posts: 41


My Contributions
Sorry, I was so caught up in the conditional part of the for statement that I forgot about the importance of setting i to a high enough value, or else the loop will end prematurely. sad.gif

addition: Plz excuse me for posting this in a rush, didn't take enough time to think for myself.

This post has been edited by Zeddicus: 17 Aug, 2008 - 09:33 PM
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Using &&-operator Inside A For-statement
18 Aug, 2008 - 02:21 AM
Post #3

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,031



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
QUOTE(Zeddicus @ 18 Aug, 2008 - 01:15 AM) *

Am I using the AND-operator wrongly


Sort of. Consider,
CODE

i=0
i<4 == true
i>2 == false
i<4 && i>2 == (true && false) == false


So, you're never really going to get off the ground here.

If you wish to start counting at three, try:
CODE

for (int i = 3;i < 4; i++)  


Although at that point you don't need a loop. You just need
CODE

Console.WriteLine(ints[3]);


Hope this helps.


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 10:23PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month