0% found this document useful (0 votes)
12 views44 pages

Chapter 1 Part 2

The document provides an overview of C programming concepts, focusing on control structures such as switch statements and loops (for, while, do-while). It explains how these structures can be used to execute code blocks conditionally and repeatedly, along with examples and the use of break and continue statements. Additionally, it covers user input methods in C, including scanf and fgets, highlighting their limitations and usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views44 pages

Chapter 1 Part 2

The document provides an overview of C programming concepts, focusing on control structures such as switch statements and loops (for, while, do-while). It explains how these structures can be used to execute code blocks conditionally and repeatedly, along with examples and the use of break and continue statements. Additionally, it covers user input methods in C, including scanf and fgets, highlighting their limitations and usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Chapter 1

C Basics Revisited

RALFH EDWIN W. PANTI


Switch
Switch Statement
Instead of writing many if..else statements, you can use the switch
statement.
Switch Statement
The switch statement selects one of many code blocks to be executed:
Switch Statement
The switch statement selects one of many code blocks to be executed:
Switch Statement
This is how it works:

• The switch expression is evaluated once

• The value of the expression is compared with the values of each


case

• If there is a match, the associated block of code is executed

• The break statement breaks out of the switch block and stops the
execution

• The default statement is optional, and specifies some code to run if


there is no case match
Switch Statement
The example uses the weekday number to
calculate the weekday name:
The break Keyword
• When C reaches a break keyword, it breaks out of the switch block.

• 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.

A break can save a lot of execution time because it "ignores" the


execution of all the rest of the code in the switch block.
The default Keyword
• The default keyword specifies some code to run if there is no case match

Note: The default keyword must be


used as the last statement in the
switch, and it does not need a break.
The default Keyword
• The default keyword specifies some code to run if there is no case match

Note: The default keyword must be


used as the last statement in the
switch, and it does not need a break.
Switch vs If-Else
Loops
In programming, loops are used to execute a block of code repeatedly
until a certain condition is met. C provides three main types of loops: for,
while, and do-while. These loops help automate repetitive tasks, making
programs more efficient and reducing redundant code.
Why Use Loops?
Loops are useful when we need to:

Execute a block of code multiple times.

Iterate over a sequence of numbers, arrays, or user inputs.

Perform tasks like searching, sorting, and processing data.


While Loop
The while loop loops through a block of code as long as a specified
condition is true:
While Loop
In the example below, the code in the loop will run, over and over again, as
long as a variable (i) is less than 5:

Note: Do not forget to


increase the variable used in
the condition (i++), otherwise
the loop will never end!
Do-While Loop
The do-while loop is a variant of the while loop. This loop will execute the
code block once, before checking if the condition is true, then it will repeat
the loop as long as the condition is true.
Do-While Loop
The example below uses a do-while
loop. The loop will always be executed
at least once, even if the condition is
false, because the code block is
executed before the condition is
tested:
Do not forget to increase the variable used in
the condition, otherwise the loop will never
end!
Real-Life Examples
Simple "countdown" program
Real-Life Examples
Print even numbers between 0 and
10 (inclusive)
Real-Life Examples
While loop combined with an if
else statement

If the loop passes the values


ranging from 1 to 5, it prints "No
Yatzy". Whenever it passes the value
6, it prints "Yatzy!".
For Loop
When you know exactly how many times you want to loop through a block
of code, use the for loop instead of a while loop:
For Loop
• Expression 1 is executed (one time) before the execution of the code block.

• Expression 2 defines the condition for executing the code block.

• 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 1 sets a variable before the loop starts (int i = 0).

• Expression 2 defines the condition for the loop to run (i must be


less than 5). If the condition is true, the loop will start over again, if
it is false, the loop will end.

• 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:

This will stop the iteration.


Output: 0,1,2,3
C Break and Continue
The continue statement breaks one iteration (in the loop), if a specified
condition occurs, and continues with the next iteration in the loop.
C Break and Continue
This example skips the value of 4:

This will print 0,1,2,3,5,6,7,8,9


C User Input
To get user input, you can use the scanf() function:
Multiple Inputs
The scanf() function also
allow multiple inputs (an
integer and a character in the
following example):
Multiple Inputs
The scanf() function also
allow multiple inputs (an
integer and a character in the
following example):
Take String Input
When working with strings in
scanf(), you must specify the
size of the string/array.
Take String Input
However, the scanf() function has
some limitations: it considers space
(whitespace, tabs, etc) as a
terminating character, which means
that it can only display a single word
(even if you type many words).

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

You might also like