|
Decisions - small ones, or big ones?
When you're traveling, you'll sometimes come to a point at which you make a decision - to go one way or to go another. Sometimes the decision is a small one - for example, if I'm driving through Marlborough there's a choice of the town centre or the road around the back; whichever I take, they come back together again very quickly. And at other times the decision is much bigger - we decided to fly on our recent trip to Slovenia, and once that decision was made and we started to act on it, the whole course of our time from Sunday lunchtime through to the following Friday evening was mapped out as alternative "B" rather than "A".
All the programming languages that we teach have conditional statements - if statements, or commands - and there's a requirement in every language to define the start and end of the conditional code - where the two tracks merge again, as well as where they separate. There are a variety of ways of doing this - using insets (Python), using an endif, if or do, done pair (Shell programming) but perhaps the most common is to use curly braces. That applies in PHP and in C (today's example) and - slightly varied - in Perl and Tcl.
#include <stdio.h>
int main() {
int dayslong;
printf("How long is the course ");
scanf("%d",&dayslong);
/* Condition applies only to first printf */
if (dayslong < 5)
printf ("Less than a week\n");
printf ("No weekend activities\n");
printf ("First Job done\n");
/* Condition applies to TWO printf-s */
if (dayslong < 5) {
printf ("Less than a week\n");
printf ("No weekend activities\n"); }
printf ("Second Job done\n");
}
In the first section, no curly braces are used and so the condition applies ONLY to the first printf, but in the second section with curly braces, the condition applies to the first and second printfs.
Let's run that ...
How long is the course 7
No weekend activities
First Job done
Second Job done
[trainee@daisy cd07]$ ./cb
How long is the course 4
Less than a week
No weekend activities
First Job done
Less than a week
No weekend activities
Second Job done
[trainee@daisy cd07]$(written 2007-12-18 07:30:56)
Associated topics are indexed under H104 - PHP - Control StatementsY103 - Python - Conditionals and LoopsT203 - Tcl/Tk - Conditionals and LoopsP204 - Perl - Conditionals and Loops
Some other Articles
The Christmas LetterNext course - 7th January 2008, Regular ExpressionsFSB leaves its members feeling like mushroomsSome new C programming examples - files, structs, unions etcDecisions - small ones, or big ones?Shopping for Christmas and looking forwardTcl/Tk - updating your display while tasks are runningUsing Tcl/Tk resource files for flexible applicationsMaking a variable dynamically visible in a Tcl/Tk GUIThe Horse goes on and on
|
1889 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38 at 50 posts per page
This is a page archived from The Horse's Mouth at
http://www.wellho.net/horse/ -
the diary and writings of Graham Ellis.
Every attempt was made to provide current information at the time the
page was written, but things do move forward in our business - new software
releases, price changes, new techniques. Please check back via
our main site for current courses,
prices, versions, etc - any mention of a price in "The Horse's Mouth"
cannot be taken as an offer to supply at that price.
Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).
|
|