Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Section 4:

Lead the flow


Conditional Execution
• A conditional flow is created by using the "if"-"else" statement.
• The syntax is if (condition) { first execution block } else { alternative execution block}

Condition Construction
• Equality condition is expressed with == (while a single = means assignment)
• And the opposite (inequality) is tested with != operator.
• Condition precedence:
o “And” beats “Or” (same logic as “multiplication beats addition”)
• Condition construction operators:
o && means “And”
o || means “Or”
o ! means “Not”

Conditional Expressions Hazards


• Wrong: if (2 < 4 < 6)
o Correct: if (2 < 4 && 4 < 6)
• Wrong: if (a = 8) (thought compiles and runs! We’ve explained why)

The Switch
• Condition with multiple outcomes requires a switch:
• Syntax:
o switch(<variable>) { case <first possible value>: … break; case <second..>: … break; …
; default: … break; }

The Trinary Shortcut


• if (condition) { A } else { B } is equivalent to (condition) ? A : B;
• the trinary operator evaluates to its outcome. Therefore the following will work:
• int a = (a < 0) ? -1 : 1;
o (finding the sign of a)

Loops, Part 1 - the While Loop


• Loop = recurring block of code, that depends on some condition.
• While loop executes its code block till the condition fulfills:
• while (condition) { do the commands enclosed in those curly braces! }
• Variables live only in the scope where they’re defined. Curly braces enclose a scope.
• Scopes can be introduced in code by themselves, regardless of loops and conditions.
Loops, Part 2 - the For Loop
• A For loop is used when number of iterations is known, and depends on a counting variable.
• For (int counter =0 ; counter < num_of_iterations; counter++) { do something; }

Loops, Part 3 - the Do-While Loop


• The do-while syntax is used when the condition can be checked only after running the code
block for the first time.
• Syntax: do {something;} while (condition);

Variable Scopes
• Using a variable outside of the scope where it was defined will issue a compiler error.
• Defining variables outside of any scope turns them into “global” variables accessible from
anywhere.

Should I Continue Breaking?


• Two important keywords that control the execution from within the loop:
• continue = skip right now to the next loop iteration.
• break = break out of the loop at all. Nevermind the condition.
• This is much better and more readable than nested conditions inside a loop

Under the Hood of Branching (and Looping)

Arrays, Part 1: One Dimensional Arrays


• An "Array" in C is a large block of memory that stores many elements of the same kind. An
Array is defined like this:
Type ArrayName[size];
for example:
char name[10];
• Ahe size parameter in this definition cannot be a variable.
• Accessing the i-th element: ArrayName[i];
Inline initialization: int ages[] = {55, 26, 17, 9, 82, 74};
Arrays, Part 2: Two Dimensional Arrays
• Two-dimensional array can be thought of as one dimensional array with offsets. We’ve seen
the back and forth conversions from 1D to 2D with simple math.
• There’s indeed a special syntax that hides this math from you: int board[8][8]

Cheers!
Shmuel.

You might also like