Use of conditional and loop statements in C programming

Thread Starter

Parth786

Joined Jun 19, 2017
642
Hi
I am having difficulty to remember the use of statement (if else ) and loop (for, while ) in c programming. I don't understand when I need to use statement (if else ) and loop (for, while ).
if else : if switch is ON , LED should be ON
else LED should be off

Code:
if (condition )
{
   /* if the condition is true, switch is ON, than LED will ON*/
}

else {
   /* if the condition is false, switch is OFF than LED will OFF  */
}
so I think if there is only two condition true and false than I have to use If else conditions.

while loop : check the condition. if condition is true than do true things and things will repeat all time like if the switch is ON , blink LED for every 2 seconds
Code:
while (condition)
{
   statements;
}
For loop: if the switch is ON than blink LED only for 2 second
so I am struggling with basic. how to remember permanently to the use of statement and loop in c programming ?
 

JohnInTX

Joined Jun 26, 2012
4,787
These topics have been pretty well covered in several of your other threads. For example:
https://forum.allaboutcircuits.com/...n-lcd-through-user-input.138089/#post-1158066
shows the very constructs you are asking about in the code you yourself posted.

It would be better to review those, your text (it's in there, I've read it), and ask for detail clarifications in those threads. Starting yet another thread here will only cause members to waste time duplicating effort.

EDIT: Thread was locked for reasons noted. Reopened per TS' request.
EDIT 2: More descriptive title
 
Last edited:

ArakelTheDragon

Joined Nov 18, 2016
1,362
There is no limitation, you can use whatever you want as long as you understand it.

EDIT:
And please you use google at least once before you post.
 

WBahn

Joined Mar 31, 2012
30,058
It's difficult to understand where the difficulty is coming from.

You correctly indicated that you use a loop if you want to repeat some action whereas that is not a factor in the if, or if-else statement.

These statements fairly closely parallel our use of these terms in everyday life.

For instance, imagine I give my daughter an empty cup and tell her to get water from the faucet and put it in the dog's water dish.

Consider the difference between the following two sentences:

IF the water dish is less than half full, put a cup of water in it.

WHILE the water dish is less than half full, put a cup of water in it.

In the first case, she will check the dish and, if it is less than half full, she will put one cup of water in it. It doesn't matter whether it is now more or less than half full -- I only told here to put at most one cup of water in the dish.

In the second case, I told her to continue putting cups of water into the dish as long as (i.e., while) the dish is still less than half full.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
I am explaining some of example with interfacing device such as LED, motor, LCD ..etc.
LED:
1. If else statement
If switch is ON , Turn ON LED , else turn off LED

2. for loop
Blink LED only for 2 seconds

3. while loop
Blink LED for every 2 seconds ( repeat operation )

Motor
1. If else statement
If switch is ON , Turn ON motor , else turn off Motor

2. for loop
Turn on Motor only for 12 seconds

3. while loop
turn ON motor for every 12 seconds ( repeat operation )

LCD display
1. If else statement
If switch is pressed , Turn ON Display , else turn off Display

2. for loop
Display welcome message only for 2 seconds

3. while loop
display welcome message for every 2 seconds ( repeat operation )

does it make any sense ?
 

WBahn

Joined Mar 31, 2012
30,058
does it make any sense ?
Some, but not a lot.

A loop is a loop. Anything you can do with one type of loop you can do with any other type of loop.

What kind of statement would you write for the following:

If the switch is closed and the LED is off, turn it on. Otherwise, don't change it.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Some, but not a lot.

A loop is a loop. Anything you can do with one type of loop you can do with any other type of loop.

What kind of statement would you write for the following:

If the switch is closed and the LED is off, turn it on. Otherwise, don't change it.
I think
if (switch is closed && LED is off)
turn on LED;
else
( LED off );
I know we don't write this type of program. its just for understanding
 

wayneh

Joined Sep 9, 2010
17,498
The initial conditions can alter the logic of the conditional statement. For instance I have an app that controls whether a window is open or not (among other things). It needs to check whether the probability of rain is greater than some arbitrary threshold, say 30%, and keep the window closed no matter what is that's true. I don't necessarily have to specify the "else" condition if that's what all the other code does. I only have to specify what to do IF there's rain in the forecast, ie. close the window.

The point is, you have a number of conditional tools and you choose the one that, first priority, gives you the logic you need and then, second priority, does it as concisely as possible. Some might say as elegant as possible. Obviously the logic is a necessary condition and sufficient for many cases. If you want nice code that can be read by others - including yourself in the future - it's usually prudent to also make it elegant and to document it as you go.
 

WBahn

Joined Mar 31, 2012
30,058
I think
if (switch is closed && LED is off)
turn on LED;
else
( LED off );
I know we don't write this type of program. its just for understanding
So what if the switch open and the LED is ON?

Your logic would turn it OFF, but the problem says that it should remain in whatever state it was in. So if it was already ON, it should stay ON.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
So what if the switch open and the LED is ON?

Your logic would turn it OFF, but the problem says that it should remain in whatever state it was in. So if it was already ON, it should stay ON.
ok I am trying to explain with flow chart
upload_2017-8-21_12-29-16.png
 

WBahn

Joined Mar 31, 2012
30,058
Your flowchart is a loop. The statement I am asking you to implement says nothing about doing something over and over. Nor does it say anything about initializing anything.

IF the switch is closed and the LED is OFF, then turn it ON; else, leave it unchanged.

You are only asked to carry out this action one time. You do not know if the switch is open or closed. You do not know if the LED is ON or OFF.

You are only asked to write a statement that performs the action requested.

Think of all the possibilities of the switch and the LED. What is the outcome for each? What is the simplest statement you can write that is consistent with this?
 

WBahn

Joined Mar 31, 2012
30,058
Now. look at your flowchart.

Your test doesn't look at the switch -- it only looks at the LED.

Also, after turning on the LED, you then immediately ask if it is OFF and, if it is NOT, then you exit.

Was there anything in the problem statement that said anything about checking the state of the LED after you turn it ON?
 

MrAl

Joined Jun 17, 2014
11,474
Hi
I am having difficulty to remember the use of statement (if else ) and loop (for, while ) in c programming. I don't understand when I need to use statement (if else ) and loop (for, while ).
if else : if switch is ON , LED should be ON
else LED should be off

Code:
if (condition )
{
   /* if the condition is true, switch is ON, than LED will ON*/
}

else {
   /* if the condition is false, switch is OFF than LED will OFF  */
}
so I think if there is only two condition true and false than I have to use If else conditions.

while loop : check the condition. if condition is true than do true things and things will repeat all time like if the switch is ON , blink LED for every 2 seconds
Code:
while (condition)
{
   statements;
}
For loop: if the switch is ON than blink LED only for 2 second
so I am struggling with basic. how to remember permanently to the use of statement and loop in c programming ?
Hi,

Most basically the if then else statement is used as logic so your program can decided what to do when certain conditions are met.

The loop on the other hand, is for doing something repeatedly.
The 'for' loop does things with a limit on the count of the number of times it should do it.
The 'while' loop does things until some condition is met.

In some cases we might use a 'while' loop to loop forever with no exit. That's when we want the program to keep looping and keep doing the same things over and over again and that would be for the normal operation of the device we are constructing.
On the other hand, a while loop that loops forever by pure accident resulting from poor programming is not something we want to see happen and will hang the program indefinitely.
 

WBahn

Joined Mar 31, 2012
30,058
Hi,

Most basically the if then else statement is used as logic so your program can decided what to do when certain conditions are met.

The loop on the other hand, is for doing something repeatedly.
The 'for' loop does things with a limit on the count of the number of times it should do it.
The 'while' loop does things until some condition is met.
To clarify -- a for() loop MAY place a limit on the count of the number of times it should do it. This is known as a counter-controlled loop. But while() and do/while() loops can also be counter-controlled. Similarly, while() and do/while() loops are commonly sentinel-controlled, which means they look for some condition to be met, but a for() loop can also be sentinel-controlled.

A for() loop is almost entirely syntactic sugar for a while() loop. I say "almost" because the behavior of the two constructs is slightly different when a 'continue' statement is encountered.

Also, a while() loop does things WHILE some condition is met, not until (hence the name). There are languages that have until() loops, which do things UNTIL some condition is met.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Some, but not a lot.

A loop is a loop. Anything you can do with one type of loop you can do with any other type of loop.

What kind of statement would you write for the following:

If the switch is closed and the LED is off, turn it on. Otherwise, don't change it.
I think it should be something like this
Code:
main ()
{
LED OFF;
while (switch is close )
LED ON;
while (switch is open)
LED OFF;
}
 

JohnInTX

Joined Jun 26, 2012
4,787
There is no while loop in main() again. We already pretty much did this over here..
https://forum.allaboutcircuits.com/threads/how-to-make-flow-chart-for-program.138468/#post-1161447

You need to be able to trace your flowchart with your finger and think about what it is doing as you do trace. Repeat until it work satisfactorily. Stubbed ends and no main loop won't be satisfactory. The pseudo-code needs the follow the successful flowchart. The PDF in the link has both.

The for, if, while constructs and all the others will forever be a mystery until you can or will do that.
 
Last edited:

Thread Starter

Parth786

Joined Jun 19, 2017
642
Some, but not a lot.

If the switch is closed and the LED is off, turn it on. Otherwise, don't change it.
Some, but not a lot.

Otherwise, don't change it.
don't change it means what ?

Think of all the possibilities of the switch and the LED. What is the outcome for each?
condition 1 : if switch is open and LED is OFF than LED should be off
condition 2 : if switch is open and LED is ON than LED should be off
condition 3 : if switch is closed and LED is off than LED should be on
condition 4 : if switch is closed and LED is off than LED should be on

I think I am not understanding your question ?
 

WBahn

Joined Mar 31, 2012
30,058
don't change it means what ?
It means that if it was ON, then leave it ON, but if it was OFF, then leave it OFF.

condition 1 : if switch is open and LED is OFF than LED should be off
condition 2 : if switch is open and LED is ON than LED should be off
condition 3 : if switch is closed and LED is off than LED should be on
condition 4 : if switch is closed and LED is off than LED should be on

I think I am not understanding your question ?
The only time you do anything to the LED is if the switch is closed and the LED is OFF.

With that in mind, revisit your Condition 2.
 
Top