Welcome to WebmasterServe!

FREE TO JOIN! Join us now to engage in informative and friendly discussions about Webmastering, SEO, SEM, Internet Marketing, Programming, Graphic Design, Online Jobs and more. What are you waiting for? Ready to join our friendly community? It takes just one minute to register.

Dismiss Notice

Join WebmasterServe Forums 
Join the discussion! Have a better idea or an opinion? It takes just one minute to register Click Here to Join

Tutorial Difference Of If And While Statements

Discussion in 'C# Programming' started by jowjow, Jan 27, 2016.

  1. jowjow

    White Belt

    Joined:
    Jan 25, 2016
    Messages:
    105
    Ratings:
    +18 / -0
    If Statements and While Statements are very different things in programming. I will try to explain it briefly below its differences.

    Let's start with the IF statement. An IF statement checks if the instruction or expression is true or false, then runs the code. The code will be running once since it is just simply a true or false statement.

    A While statement however is more like a continuous loop. It will continue to execute or run the code as long as the instruction or statement is correct.
     
  2. Oldwriter

    Yellow Belt

    Joined:
    Dec 10, 2015
    Messages:
    64
    Ratings:
    +28 / -0
    Expanding a bit on the while part:

    - If you want to run a perpetual loop, you simply use while(true):

    Code:
    Select All
    // Perpetual loop while(true) { // Use the "break" statement to exit this loop }
    - If you want to be a certain your code executes at least once, you can use the do...while loop:

    Code:
    Select All
    do { // This code is executed at least once }while(condition);
    Every loop type certainly has its place in the arsenal :)
     
    #2 Oldwriter, Jan 30, 2016 at 11:20 AM
    Last edited: Feb 13, 2016
  3. steve taylor

    Yellow Belt

    Joined:
    Feb 12, 2016
    Messages:
    769
    Ratings:
    +25 / -0
    If is the conditional statement where as while is the looping statement .
    The syntax of the If statement are as follows :

    If (condition True ) // note that it contain only binary statement True or false
    {
    Control inter into the body of if statement .
    }
    otherwise control transfer to out of body.

    Syntax of while loop :

    While (condition true ) // condition may be like this while (a<=100)

    {
    control inter in the body of while statement.
    }
    other wise control goes to out of body.
     
  4. Manish Mishra

    Yellow Belt

    Joined:
    Dec 22, 2015
    Messages:
    1,025
    Ratings:
    +159 / -0
    If-else statement and while and other looping function has lot more differences. If-else statement and the nested if-else statement is something that passes by until the condition gets true or false. however, with the looping it is repeated unless the condition gets true or false.

    When they both are merged in a program it create more fun to use.
     

Share This Page