While statements vs if statements

What is different between the two?

Page 1 of 1

7 Replies - 27828 Views - Last Post: 29 May 2015 - 03:35 PM

#1 Fuingurth  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 118
  • Joined: 13-August 09

While statements vs if statements

Posted 29 March 2010 - 04:32 PM

Hello again,

I'm sure this question applies to many other languages, but since the only one I really know anything about is C#, I figured I'd post this question here.
So I was kind of curious today and figured that I would learn about while statements.(I probably should have hit this topic when I started learning, but the book I used never mentioned them and I had never heard about them until a couple weeks ago.) After googling what a while statement is, I came upon the Microsoft C# Library page. When I looked at how it was used I noticed it seemed very much like if statements. Then, after seeing the example I thought "Well that could be just as easily done with an if statement". I became a bit confused as to why there would be 2 statements that are so similar. I hope you all at DIC can help me better understand the difference between the two.

So I guess what I'm asking is, what can be done with a while statement that can not be done with an if statement, and vice versa. What are the benefits of while, and what are the benefits of if?
Once again, thank you all!

Is This A Good Question/Topic? 0
  • +

Replies To: While statements vs if statements

#2 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 174
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: While statements vs if statements

Posted 29 March 2010 - 04:40 PM

They are two very different things.

An if statement checks if an expression is true, or false, and then runs the code inside the statement. The code is run once.

A while statement is a loop. It continues to execute the code in the statement as long as the expression is true.

It's hard to compare the pros/cons of the two, since they do two separate things.

This post has been edited by lesPaul456: 29 March 2010 - 08:06 PM

Was This Post Helpful? 3
  • +
  • -

#3 Fuingurth  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 118
  • Joined: 13-August 09

Re: While statements vs if statements

Posted 29 March 2010 - 05:57 PM

ok that makes so much more sense now.I wasn't picking up on the fact that while is run until it is no longer true. Thank you!

This post has been edited by Fuingurth: 29 March 2010 - 05:59 PM

Was This Post Helpful? 0
  • +
  • -

#4 MentalFloss  Icon User is offline

  • .
  • member icon

Reputation: 577
  • View blog
  • Posts: 1,500
  • Joined: 02-September 09

Re: While statements vs if statements

Posted 30 March 2010 - 04:06 PM

*
POPULAR

Perhaps you mean to ask what the difference between while and for? Those are both looping constructs with conditions.

While will loop until the condition is false (ultimately false... you can have while something is false do this, but that evaluates to true because it's expected as false). For will loop for a number of times until its iterated value's condition is true.

Then there's do-while which is essentially exactly like while except it is guaranteed to run once. Sometimes this is useful - most times it is improperly used.

Then there's foreach which iterates over an IEnumerable collection.

When to use what?

You have an unknown number of iterations to process. It's impossible to know when it will stop but you do know how it stops: while/do-while.
You have a fixed number of iterations to process and you know what will end it: for.
You have a collection that implements IEnumerable that does not require modification to the collection (no swapping and such): foreach.
You have a collection that implements IEnumerable but it does require swapping values by index: for.

Hope this helps.
Was This Post Helpful? 5
  • +
  • -

#5 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 174
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: While statements vs if statements

Posted 30 March 2010 - 07:56 PM

Whether that's what Fuingurth meant to ask or not, that was a nice explanation. :^:
Was This Post Helpful? 0
  • +
  • -

#6 Philip D  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 1
  • Joined: 03-May 15

Re: While statements vs if statements

Posted 03 May 2015 - 08:03 PM

It's mostly about the big O's. Order Of Operations!
When an "if" statement is made, the "test" is first, the "actions" are second.
In a "while" statement, the "actions" precede the "test".
Was This Post Helpful? -1
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12186
  • View blog
  • Posts: 45,250
  • Joined: 27-December 08

Re: While statements vs if statements

Posted 03 May 2015 - 08:06 PM

This is very incorrect information. A do-while examines the condition after a single iteration. However, a while loop examines the condition first. A while loop can be written to be functionally equivalent to an if statement, and the asymptotic complexity will be the same between the two.

By the way- please be cognizant of dates when posting. This thread is more than five years old.
Was This Post Helpful? 1
  • +
  • -

#8 bunny03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 08-June 14

Re: While statements vs if statements

Posted 29 May 2015 - 03:35 PM

Well,

While is a loop;

if is true or false : else : elseif

I think that the creator of the topic have the answer to his question :basecase:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1