Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
pub
hello-world
c_cpp_and_make
Commits
7ab9d75b
Commit
7ab9d75b
authored
Oct 21, 2020
by
Erik Strand
Browse files
Add a demo of different control structures
parent
6efdccd2
Changes
5
Show whitespace changes
Inline
Side-by-side
02_control_structures/Makefile
0 → 100644
View file @
7ab9d75b
# Change your compiler here if you're not using gcc. CC is for C, and CXX is for C++.
CC
=
gcc
CXX
=
g++
CFLAGS
=
-Wall
-O3
.PHONY
:
all
all
:
loops
# Here I'm using some of Make's built in variables: $@ and $<. The former gets substituted with the
# name of the target. In this case, that's "loops". The latter gets substituted with the first
# prerequisite. In this case, that's "loops.c". These are hard to remember, but can save you some
# typing if you decide to rename things.
loops
:
loops.c
$(CC)
$(CFLAGS)
-o
$@
$<
.PHONY
:
clean
clean
:
rm
loops
02_control_structures/loops.c
0 → 100644
View file @
7ab9d75b
#include <stdio.h>
int
main
(
void
)
{
// Here we declare an integer called i, and set it equal to zero.
int
i
=
0
;
// This is a while loop. The code in the braces will run repeatedly as long as the condition is
// true. You don't actually need braces if there's only one statement (i.e. line). But it's not
// a bad habit to write them anyway, so that you don't accidentally skip anything. Unlike in
// Python, the indentation doesn't mean anything to the compiler -- it's just for you.
while
(
i
<
5
)
{
printf
(
"while loop: %i
\n
"
,
i
);
// This increments i. We could also use "i = i + 1" or "i += 1".
++
i
;
}
// This is a for loop. It's like a while loop, but you name the iteration variable, state the
// loop condition, and write your increment statement (or whatever else you want to do at the
// end of each iteration) all up front.
for
(
int
j
=
0
;
j
<
5
;
++
j
)
{
printf
(
"for loop: %i
\n
"
,
j
);
}
// In C, 0 means false, but any other integer means true (usually we use 1). So this loop would
// run forever, except we we have a trick up our sleeve: you can exit the loop with the keyword
// "break". This works in for loops too.
i
=
0
;
while
(
1
)
{
printf
(
"another while loop: %i
\n
"
,
i
);
if
(
i
==
2
)
{
printf
(
"leaving the while loop
\n
"
);
break
;
}
++
i
;
}
// Another useful loop keyword is "continue". It jumps to the next loop iteration early. Like
// "break", it works in for loops and while loops. Also note that we can reuse i here, like
// we've been doing in our while loops. Both loops work both ways, but it's more common to see
// for loops declare new variables.
for
(
i
=
0
;
i
<
5
;
++
i
)
{
// Here we use the modulo operator "%" to check if i is even.
if
(
i
%
2
==
0
)
{
continue
;
}
// This code only runs if i is odd.
printf
(
"%i is odd
\n
"
,
i
);
}
// This is a switch statement. It's not a loop, but it is handy to dispatch to different pieces
// of code based on the value of some variable.
i
=
42
;
switch
(
i
)
{
case
0
:
printf
(
"i is 0
\n
"
);
break
;
case
1
:
printf
(
"i is 1
\n
"
);
break
;
case
2
:
printf
(
"i is 2
\n
"
);
break
;
case
42
:
printf
(
"i is 42
\n
"
);
break
;
default:
printf
(
"i is something else
\n
"
);
}
}
0
2_command_line_tool
/Makefile
→
0
3_inputs_and_outputs
/Makefile
View file @
7ab9d75b
...
...
@@ -10,8 +10,6 @@ all: add_integers
add_integers
:
add_integers.c
$(CC)
$(CFLAGS)
-o
add_integers add_integers.c
# This rule deletes all the binaries. It's declared "phony" which means it doesn't actually make
# anything. Otherwise Make would get confused about when it should run this rule.
.PHONY
:
clean
clean
:
rm
add_integers
0
2_command_line_tool
/README.md
→
0
3_inputs_and_outputs
/README.md
View file @
7ab9d75b
File moved
0
2_command_line_tool
/add_integers.c
→
0
3_inputs_and_outputs
/add_integers.c
View file @
7ab9d75b
File moved
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment