Lecture 4: Operators and Control Flow PDF

Summary

This document is a lecture note about programming operators and control flow. It explains various aspects of programming in a structured manner. The document provides examples and code snippets for better understanding.

Full Transcript

Introduction Programmeringsteknik DT143G Lecture 4: Operators and control flow Pascal Rebreyend 14-11-2024 Pascal Rebreyend Programmeringsteknik DT143G Introduction Today’s contents Unary operators (no need to focus on +,-,/,*...

Introduction Programmeringsteknik DT143G Lecture 4: Operators and control flow Pascal Rebreyend 14-11-2024 Pascal Rebreyend Programmeringsteknik DT143G Introduction Today’s contents Unary operators (no need to focus on +,-,/,* !) Orders Control flow The first real programming stuff! Pascal Rebreyend Programmeringsteknik DT143G Introduction Unary operator #include int main() { int x, y; x = 2; x++; printf(\x = %d\n", x); x = 2; y = x++ + 3; printf("y = %d\n", y); x = 2; y = ++x + 3; printf("y = %d\n", y); return 0; } Pascal Rebreyend Programmeringsteknik DT143G Introduction ++ ++ (and —) are widely used; Take care of order of precedence (++i vs i++) Good idea to avoid complex expression too difficult to read Parentheses or splitting an expression on several lines can be safer (and easier to read!) Important: To be able to read the documentation (and understand it!) Pascal Rebreyend Programmeringsteknik DT143G Introduction Operator order 1 ++ —... 2 */% 3 + -... Pascal Rebreyend Programmeringsteknik DT143G Introduction Control Flow - Example #include int main() { int x; scanf("%d", &x); if(x != 0) printf("%d\n", 100 / x); return 0; } Pascal Rebreyend Programmeringsteknik DT143G Introduction If-Then - Basic statement1; if(expression) statement2; statement3; Identation: just for the readability! Expression is evaluated and then decision is taken. (statement2 is executed only expression is TRUE.) We can use a block ({}) if we want to have several statements Pascal Rebreyend Programmeringsteknik DT143G Introduction if then else if(expression) statement1; else statement2; statement3; Either statement1 either statement2 is executed (and not both of them) We can use blocks too! Pascal Rebreyend Programmeringsteknik DT143G Introduction Readability if(age < 18) if(age < 18) if( age > 12) if( age > 12) printf("Teenager\n"); printf("Teenager\n"); else else printf("Child\n"); printf("Child\n"); else else printf("Adult\n"); printf("Adult\n"); Pascal Rebreyend Programmeringsteknik DT143G Introduction Relational and Logical operators 1 , = 2 ==, ! = 3 &&, ||, ! Example 1: x != 0 && 1/x = 0 && y >= 0) Example 3: if (x>10) && (x 4) else printf("None!\n"); printf("None!\n"); return 0; return 0; } } Pascal Rebreyend Programmeringsteknik DT143G Introduction Switch #include int main() { int x; scanf("%d", &x); switch(x){ case 1: printf("One\n"); break; case 2: printf("Two\n"); break; case 3: printf("Three\n"); break; case 4: printf("Three\n"); break; default: printf("None!\n"); break; } return 0; } Pascal Rebreyend Programmeringsteknik DT143G Introduction Switch Importance of the break case !!! case : Label must be a constant Nicer than a set of if! (and safer, with default!!!) The variable should an integral type (int, char, enum). Pascal Rebreyend Programmeringsteknik DT143G Introduction Conclusion ++ very common Order of precedence Order of evaluation! Write in a readable way! Use indentation! if, switch,... : branching instructions Pascal Rebreyend Programmeringsteknik DT143G

Use Quizgecko on...
Browser
Browser