You are looking at lesson 3. Your place is still lesson 12, task 1: malloc.
Comparisons, &&, || and !, if / else if / else, = or ==, switch with break, and why doubles are not compared with ==.
In topic 1 you saw the three blocks of structured programming. You already write sequences. This topic covers the other two: selection (this step) and repetition (the next one).
| Operator | Means | 7 ? 5 |
|---|---|---|
== |
equal | 0 |
!= |
not equal | 1 |
< <= |
less, not more | 0, 0 |
> >= |
more, not less | 1, 1 |
The result of a comparison is an int: 1 is true, 0 is false. C treats any non-zero value as true.
&& is and: both conditions are true. grade >= 1 && grade <= 10|| is or: at least one is true. grade < 1 || grade > 10! is not: it flips the result. !(grade >= 5)In maths you write 1 ≤ x ≤ 10, but not in C. 1 <= x <= 10 first works out 1 <= x (0 or 1), and then compares that 0 or 1 with 10. GCC catches it:
main.c:5:20: error: comparison of constant '10' with boolean expression is always true [-Werror=bool-compare]
5 | if (5 <= grade <= 10) {
| ^~
main.c:5:11: error: comparisons like 'X<=Y<=Z' do not have their mathematical meaning [-Werror=parentheses]
5 | if (5 <= grade <= 10) {
| ~~^~~~~~~~
Write 5 <= grade && grade <= 10.
if (average >= 9) {
printf("Excellent\n");
} else if (average >= 5) {
printf("Passed\n");
} else {
printf("Failed\n");
}
The conditions are checked from the top. The first branch that fits runs and the rest are skipped, so 9.5 prints only "Excellent". else is optional.
Always write the braces { }, even with one statement inside. Without them, a second statement you add one day won't be part of the branch.
if (grade = 10) stores 10 and then checks whether 10 is not 0, so the condition is always true. GCC warns:
main.c:5:9: error: suggest parentheses around assignment used as truth value [-Werror=parentheses]
5 | if (grade = 10) {
| ^~~~~
Comparison is ==.
When you compare one whole number with several exact values, switch is clearer:
switch (grade) {
case 10:
printf("Excellent\n");
break;
case 9:
printf("Very good\n");
break;
default:
printf("Other\n");
break;
}
case 10: runs from here when grade equals 10.break leaves the switch. Without it, execution "falls through" into the next case. The course flags treat that as an error: this statement may fall through.default: takes all other values.case 1: case 2: case 3: one after another, with no statements between them.switch works only with whole numbers and char, and only for exact equality. For ranges (>= 9) use if.
From topic 2: 0.1 + 0.2 is not exactly 0.3, so if (0.1 + 0.2 == 0.3) is false. Compare real numbers with an allowed error: a - b < 0.000001 && b - a < 0.000001.
Each one ticks itself when you do it.
All six done. You know your way around.
It is laid out like CLion: the same panels in the same places. This tour shows each one, and you can reopen it any time with Tour.
Switches the left panel: Lesson plan, Project files, Changes. Click the active icon again to hide the panel.
In CLion: the tool window bar runs down the left edge, too.
This part is not on this task: it appears when a task needs it.
Every task in this lesson. Now marks your place. Clicking a task only lets you look; it never moves your place. Project files and Changes open here too.
In CLion: this spot holds the Project view (the file tree).
This part is not on this task: it appears when a task needs it.
Opens the file for your task by itself. A tab appears for each file the step opens. The breadcrumbs above the code show the folder.
In CLion: the same editor tabs and breadcrumbs.
This part is not on this task: it appears when a task needs it.
Diff shows what changed, like git diff: red is the old code, green the new.
In CLion: the diff view in the Commit window looks the same.
This part is not on this task: it appears when a task needs it.
Terminal for build commands, Database to see your tables. They open by themselves when a task needs them. Drag the top edge to resize.
In CLion: Terminal, and the Database tool window.
This part is not on this task: it appears when a task needs it.
What to do now, right above the code, so you never miss it. Numbered notes in the code explain the important lines. Details shows what you should see and a hint.
In CLion: no equivalent: this is your lecturer's guide.
This part is not on this task: it appears when a task needs it.
Done is the only button that moves you forward. Look back lets you review without losing your place.
This part is not on this task: it appears when a task needs it.
Guided, Focus code or Floating. You can drag the panel edges too. Your layout is remembered for next time.
In CLion: you can also rearrange and hide tool windows.
This part is not on this task: it appears when a task needs it.
Six small missions. Each one ticks itself when you do it, so you know you have found everything.