Chapter 1 Part 2
Chapter 1 Part 2
C Basics Revisited
• The break statement breaks out of the switch block and stops the
execution
• This will stop the execution of more code and case testing inside the block.
• When a match is found, and the job is done, it's time for a break. There is no
need for more testing.
• Expression 3 is executed (every time) after the code block has been executed.
For Loop
This will print the numbers 0 to 4:
Example explained:
• Expression 3 increases a value (i++) each time the code block in the
loop has been executed.
Nested Loops
It is also possible to place a loop inside another loop. This is called a nested loop.
Nested Loops
The "inner loop" will be executed one time for each iteration of the "outer loop":
Nested Loops
The "inner loop" will be executed one time for each iteration of the "outer loop":
Try this!
To demonstrate a practical example of the for loop, let's create a program that counts to
100 by tens:
Try this!
In this example, we create a program that only print even numbers between 0 and 10
(inclusive):
Try this!
Here we only print odd numbers:
Try this!
In this example we print the powers of 2 up to 512:
Try this!
And in this example, we create a program that prints the multiplication table for a
specified number:
C Break and Continue
The break statement can also be used to jump out of a loop.
C Break and Continue
This example jumps out of the for loop when i is equal to 4:
From the example above, you would expect the program to print "John Doe", but it only prints "John".
Take String Input
When working with strings, we often
use the fgets() function to read a
line of text. Note that you must
include the following arguments:
the name of the string
variable,sizeof(string_name),
and stdin:
Supplementary Lessons
Read the article about C memory address in the link below.
C Memory Address
Thanks!
Any questions?
Quiz/Activity