Can computers make decisions for us? Note that computers can remember stuff, do repetitive tasks, but can't think their own? So programmer has to tell the computer what decisions to make!
The computers have to be told to take decisions based on conditions.
When
- you are thirsty
- your throat is feeling dry
- you did not drink water for a long time
Then you should drink water.
When human meet certain conditions he takes some decision.
Using If… Then… Else
The computers use simple and implying words such as If and Then to guide their actions.
OK Computer [main()]:
IF time is After 8 PM
Then Display “Good Night!”
In c language, if programmer wants to implement above task into his code form then below is the details:
main()
{
int currentTime = 7;
//change 7 to 9, msg will be displayed
if(currentTime > 8)
puts("Good Night");
}
Output:
[if currentTime = 7, nothing prints]
Good Night [if currentTime = 9,msg prints]
That was simple and pretty easy right?
The conditions help the computer to decide what actions to perform and when.
Latest try making decisions using sequences
OK Computer:
Remember marks is 50
IF marks is less than 35
Then Display “You have failed!”
Else Display “You have passed!”
Output:
You have passed!
Now what if the condition is not met? In such situations programmers use the word Else.
OK Computer:
IF time is After 8 PM
Then Display “Good Night!”
Else Display “Good Day!”
In c language, if programmer wants to implement above task into his code form then below is the details:
main()
{
int currentTime = 7;
//change 7 to 9, different msg will be displayed
if(currentTime > 8)
puts("Good Night");
else
puts("Good Day");
}
Output:
Good Day [if currentTime = 7]
Good Night [if currentTime = 9]
This ability to perform different actions in different situations is what makes the computer look smart.
The conditions make the programs more intelligent.
Let’s take Tom’s example, in his cake shop Tom offers two types of cakes: normal cake and normal cake with decoration.
Now below is the sequence, by which preparing cake with If and Then structures:
OK Computer:
IF customer wants Decorated Cake
Then Prepare Normal Cake with Decoration
IF customer wants Normal Cake
Then Prepare Normal Cake
See how conditions make the things much easier and intelligent!
Using Else if
What if programmer has to take care of alternate scenarios? The programmer must use the mighty else if structure.
If Tom’s cake shop offers three varieties of cakes: normal cake, normal cake with decoration and normal cake with name carved out on it.
How would programmer do this sequence using if elseif else
OK Computer:
IF customer wants Named Cake
Then Prepare Normal Cake with Name
ELSEIF customer wants Decorated Cake
Then Prepare Normal Cake with Decoration
ELSE
Prepare Normal Cake
In c language, if programmer wants to implement above task into his code form then below is the details:
main()
{
int cakeCode = 0;
//change cakeCode to 0,1,& 2, diff msg will be displayed
if(cakeCode == 2)
puts("Client Ordered Normal Cake with Name");
else if(cakeCode == 1)
puts("Client Ordered Normal Cake with Decoration");
else
puts("Client Ordered Normal Cake ");
}
Output:
[change cakeCode = 0,compile and run for last msg] [change cakeCode = 1,compile and run for middle msg]
[change cakeCode = 2,compile and run for first msg]
Why programmer need these If and Else, that he calls conditions?
- They make the programs more intelligent.
- They are based on the situations.
- The computers are able to decide actions based on these conditions.
Summary
The computers can't decide themselves.
The programmer helps the computer to decide by creating conditions based on situations.
These conditions make the programs seem intelligent.
The programs with conditions help the computer to perform the right actions in the right situations.
PS: I have composed this post-series based on my pdf eBook on playstore:
Basics of Computer, Programming and C
The series is posted continuously one each next day, and reader can found any post by referring blogs’ time line tab.
--till next post, bye-bye and take care.
No comments:
Post a Comment