0
What is difference between while , for and if?
Using program
3 Antworten
+ 1
while, do, and for are loop statements as stated before:
while(condition){
code if condition is true
}
while statements will only run if the condition is true
do statements
do{
code to execute
}while(condition)
do statements will execute the code at least once
for(init; test; post){
code to execute
}
for statements are controlled loops usually set by a fixed number of intervals
if, if-else are for branching decisions switch can also be used for branching:
if(condition(s)){
code
}
only runs the code if the condition is true and only once unless its in a loop statement in which case it is still only running once but through each iteration of the loop
if(condition(s)){
true code
} else {
false code
}
this if-else is like a true-false, if the condition is true it runs the true code, otherwise it runs the false code
you can also nest if and if-else
if(condition(s) one){
code
} else if(condition(s) two){
true code
} else {
false code
}
In this type of set up it would test condition(s) one if they were true it would run the immediate code otherwise it will test condition(s) two and run the true or false code as appropriate.
Hope this helps.
+ 4
If - an operational statement (if this do that)
While, for - conditional statements (for x times or while this do that).
They can be written differently between languages but the premise is the same.
P.s this is a VERY simple answer for a very simple question...suggest you do some courses as these are explained in detail, with working examples etc.
+ 3
While and for are loop but if is a decision making statement
.
You can use for and while loop for repeat execution a perticular part of program.
For loop access execution if condition is true and while loo runs till condition is false.
If statement makes a decision like if condition is true then your program will be execute once and if condition is false then it will skip that part of program.