For 2023 (and 2024 ...) - we are now fully retired from IT training. We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.
Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!
I am also active in many other area and still look after a lot of web sites - you can find an index ((here)) |

Well House Consultants
You are on the site of
Well House Consultants
who provide
Open Source Training Courses
and business
hotel accommodation. You are welcome to browse and use
our resources subject to our copyright statement and to add in links from your pages to ours.
Other subject areas - resources
Java Resources
Well House Manor Resources
Perl Resources
Python Resources
PHP Resources
Object Orientation and General topics
MySQL Resources
Linux / LAMP / Tomcat Resources
Well House Consultants Resources
Extras Resources
C and C++ Resources
Ruby Resources
Tcl/Tk Resources
Web and Intranet Resources
|
C and C based languages module C203
Conditionals and Loops
Exercises, examples and other material relating to training module C203. This topic is presented on public courses Learning to Program in C, Learning to program in C and C++, Programming in C, C and C++ Programming, Learning to program in C and C++, C and C++ Programming
Background In this module, we study conditional statements like if and
else that you use in C to have parts of your code run only if
certain conditions are true, and loops such as while and for
while provide the facilities through which you get parts of your
code to repeat a number of times, or until a certain condition
is met.
Articles and tips on this subject | updated | 4337 | Learning to program sample program - past its prime, but still useful On a "Learning to Program in xxxx" day, we start off from basics and develop (first) a very simple input - calculate - output program then (second) we extend it to include conditional code and repeated code.
During the course of the day, delegates will ask "what about xxxx" and "could it do yyyy", ... | 2014-12-02 | 4323 | Learning to program - Loop statements such as while If your program always ran each statement just once (indeed skipping over statements which were in blocks in false conditions) it would run very quickly and would have little use. You couldn't (for example) run a program which went through a whole series of results from a database query and displayed ... | 2014-11-22 | 4322 | Learning to Program - the conditional statement (if) Every language has some sort of conditional statement. That's a way of looking at some sort of setting or status in the program and performing some sort of action based on that setting or status.
such statements take the form ...
if {some sort of condition is true} then {run a group of ... | 2014-11-22 (longer) | 3200 | How a for loop works Java, Perl and other languages Java, Perl, PHP, Ruby, C, C++, Tcl and many other languages support a "for" loop construct.
When your program enters the top of the loop, it performs the first statement in the brackets ... in this Perl example, that's to initialise the $now variable to 1. It then evaluates the expression given as ... | 2014-02-02 | 3397 | Does a for loop evaluate its end condition once, or on every iteration? All the languages that we teach have a for loop or the equivalent, which is a clean way of repeating a block of code with a rising or falling index number. It's used in many circumstances - for example in iterating through the months of the year (for m goes from 1 to 12) of in stepping through all ... | 2012-04-15 | 3384 | Loops - a comparison of goto, while and for Almost every program you write, in any language, will have repeating code in it in the form of a loop. Programs without loops are limited to really simple "read a piece of data, calculate on it and print a result" tasks, and ever with them there will be loops within the function calls that you make ... | 2011-08-10 | 3243 | Breaking the running sequence - an introduction to conditional statements and loops A program is a series of instructions which are run in sequestially - or so you'll be told when you start programming. And, indeed, that's the default - the way it is unless you write code to do something different. "A program is a series of statemnets each separated by [something] which run one after ... | 2011-04-11 | 962 | Breaking a loop - Ruby and other languages When you're in a loop there are occasions you want to say get me our of this loop NOW, or "I'm done with the current iteration. And those are the 'classic' break and continue statements from C, C++ and Java. Languages like Perl changed break to last and continue to next ... and added a redo that asks ... | 2011-01-29 | 3121 | New year, new C Course Well - not completely a new course. New delegates, and lots of new examples written for them during the first day to that they can see not only WHAT work but also HOW the design of what works is put together during the day. Click on the individual source code examples, and you'll find programs to ... | 2011-01-08 | 3004 | Increment operators for counting - Perl, PHP, C and others Look at this Perl statement:
$counter = $counter + 1;
"Take the value in $counter, add 1 to it, and put it back in $counter". It's a common programing requirement - indeed, so common that you can write it in a shorter form in many languages:
$counter += 1;
"Add one to the value ... | 2010-10-20 | 2570 | Function Prototypes in C Programs other than the shortest are name up of named blocks of code (functions) into which parameters are passed and from which results are returned. And rather than have all the functions that make up an application in one file of source, you'll share them between several. That way, maintainable ... | 2010-01-13 | 2002 | New C Examples - pointers, realloc, structs and more Every time I program in C, I marvel at how clever the language is ... yet at the same time I curse some of the devices that are used to perform certain actions, which make the code that much more of a 'puzzle' to right.
I've finished a 2 day C Programming course today ... and written a whole raft ... | 2009-01-20 | 1696 | Saying NOT in Perl, PHP, Python, Lua ... "Isn't there one standard way to say NOT?" asked one of my delegates on today's course - and it's an excellent question. But the answer to a question about a negative subject is itself in the negative - no, there isn't just a single way!
In fact .. I can think of no fewer that 12 ways!
• 1. ! ... | 2008-07-05 | 1582 | Ruby, C, Java and more - getting out of loops break and continue statements have been available for loop control for many years, and others functionallity has been added such as Perl's redo. From today's Ruby Course, here's a table that compares these loop controls in Ruby to similar commands in the other languages that I have been discussing ... | 2008-03-19 | 1220 | for loop - how it works (Perl, PHP, Java, C, etc) When writing a program, you'll often want to repeat a block of code, counting up through a table or performing a block of code with an input value (loop counter) that goes up 1, 2, 3, 4 etc.
You COULD do this using a while loop, but this means you have to specify each of
• how to start (initialise) ... | 2007-06-07 | 1191 | Smart English Output - via PHP and Perl ? : operator It's smart to have your program say "there IS in stock" or "there ARE 2 in stock" ... but if you write your code using an "if" and "else" type structure it can become quite verbose.
The ? ... : operator - available in many languages including C, Perl and PHP - allows you to write a single "if,else" ... | 2007-05-18 | 353 | Wimbledon Neck What use is the "+=" operator? Why did the language designers bother to provide it? Yet they did ...
In Perl and PHP
$number_on_bus = $number_on_bus + $goton_here;
becomes
$number_on_bus += $goton_here;
In Python
number_on_bus = number_on_bus + goton_here
becomes
number_on_bus += goton_here
In ... | 2006-06-05 |
Examples from our training material
cb.c | Using { and } to scope a block | demo.h | Function prototypes for err.c and otherbits.c demo | eer.c | Program that makes use of ANSI C function prototypes | eight.c | loop showing weight loss needed until user is healthy | ex_203.c | Exercise - throw a die until you get a 6 | fresh.c | looping via a goto | freshmeat.c | while loop - compared to goto loop and for loop | freshveggies.c | for loop - compare to goto loop and while loop | greet.c | Learning to program in C ... first day's illustrations | lp1.c | repeating code in a while loop | lp2.c | for loop - bad and good examples | makefile | makefile for module C203 | otherbits.c | Functions used in function prototype demo (ANSI C) | seven.c | Conditional statements in C and C++ | sindol.c | Singapore Teapot answer | sindy.c | Singapore teapot in C | tall.c | Temperature conversions using if | tall2.c | Temperature conversions using switch | tall3.c | Temperature conversions in a while loop | tall4.c | Temperature conversions with ++ and += | tall6.c | Temperature conversions in a while loop | third.c | Conditional - if and else - example |
Background information
Some modules are available for download as a sample of our material or under an Open Training Notes License for free download from [here].
Topics covered in this module
The if statement. Writing conditions in C. Exercise. if, else, elseif. switch, case, default. while. ++ and += operator families. Exercise. do while. for. break and continue. goto and labels. Layout of your blocks, and comments revisited. Exercise.
Complete learning
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule page.
Well House Consultants specialise in training courses in
Ruby,
Lua,
Python,
Perl,
PHP, and
MySQL. We run
Private Courses throughout the UK (and beyond for longer courses), and
Public Courses at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if you live in a different
country or continent to us.
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre.
|
|
|