Tutorial Difference Of If And While Statements

jowjow

Well-Known Member
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.
 

Oldwriter

Content Writer
Expanding a bit on the while part:

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

Code:
// 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:
do
{
	// This code is executed at least once
}while(condition);
Every loop type certainly has its place in the arsenal :)
 
Last edited:

steve taylor

White Belt
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.
 

Manish Mishra

Content Writer
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.
 
Top