Tutorial #4 Summer 2005.

Slides:



Advertisements
Similar presentations
Recursive Descent Technique CMSC 331. UMBC 2 The Header /* This program matches the following A -> B { '|' B } B -> C { '&' C } C -> D { '^' D } D ->
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Selective Structures.
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
Tutorial #6 Summer functions ( parameters list ) { body }
Tutorial #5 Summer while x = -2; while (x < 0) { printf("x is still negative :(\n"); x++; } printf("x is no longer negative.\n");
Liza Fireman Tutorial #3 Summer Liza Fireman Keywords whilevolatilevoidunsigned uniontypedefswitchstruct staticsizeofsignedshort returnregisterLongint.
1 Flow of Control True and False in C Conditional Execution Iteration Nested Code(Nested-ifs, Nested-loops) Jumps.
Declarations/Data Types/Statements. Assignments Due – Homework 1 Reading – Chapter 2 – Lab 1 – due Monday.
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
1 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions.
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
A simple C program: Printing a line of text #include main() { printf(“hello, world\n”); } Program output: hello, world.
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Tutorial ICS431 – Introduction to C.
Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=
Computer Science 210 Computer Organization Arrays.
Adv. UNIX:io/91 Advanced UNIX v Objectives of these slides: –look in some detail at standard input and output in C Special Topics in Comp.
A First C Program /* Print a Message */ #include main() { printf("This is a test!\n"); } The program is compiled and run as cc pg32.c  a.out  This is.
IPC144 - LOOPS while - do while - for. Review - SWITCH The switch statement can also be used to select code to be executed and code to be skipped. It.
Assignment Operators = +=-= *= /=%= Statement Equivalent Statement a = a + 2 ;a += 2 ; a = a - 3 ;a -= 3 ; a = a * 2 ;a *= 2 ; a = a / 4 ; a /= 4 ; a =
1 C Programming Week 2 Variables, flow control and the Debugger.
CS140: Intro to CS An Overview of Programming in C (part 3) by Erin Chambers.
1 Flow of Control True and False in C Conditional Execution Iteration Nested Code(Nested-ifs, Nested-loops) Jumps.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
Program to calculate product of odd numbers b/w 1 and 15 #include main() { int prod = 1, x; for(x = 1; x
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
Problems Reads a letter grade from user and displays the corresponding range for that letter grade (A  , B  80-89, C  70-79, D  60-69, otherwise.
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
CSCI 171 Presentation 3. Operators Instructs C to perform some operation Assignment = Mathematical Relational Logical.
1 Lecture03: Control Flow 9/24/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Introduction to Programming Lecture 5: Interaction.
 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,
Dr. Sajib Datta Sep 8,  char type technically is an integer type  Computer uses numeric codes to represent characters, and store characters.
ECE Application Programming
Zhang Hongyi CSCI2100B Data Structures Tutorial 3
CSE 220 – C Programming Pointers.
Administrative things
Flow of Control True and False in C Conditional Execution Iteration
最新C程式語言 蔡明志 編著.
Chapter 4 (Conditional Statements)
Chapter 2 Overview of C.
Condition Statements.
CSE1320 INTERMEDIATE PROGRAMMING Operators+Conditionals+Loop
מבוא כללי למדעי המחשב תרגול 2
Control Structures Lecture 7.
Introduction to Programming
Compiled and ready to run Memory Stack /*
Chapter 13 Control Structures
Nhập và Xuất trong C Chương 4 Input and Output in C.
Introduction to CS Your First C Programs
C Programming Variables.
מ- C++ ל- C קרן כליף.
CSCE 206 Lab Structured Programming in C
Chapter 5 Decision Making and Branching
CSC215 Homework Homework 04 Due date: Oct 14, 2016.
שיעור רביעי: פונקציות, מבוא לרקורסיה
Incremental operators
Week 2 Variables, flow control and the Debugger
UMBC CMSC 104 – Section 01, Fall 2016
CSC215 Homework Homework 03 Due date: Oct 07, 2016.
Computer Programming Techniques Semester 1, 1998
Control Structures Lecture 6.
Recursion.
Character Arrays char string1[] = “first”;
CSCE 206 Lab Structured Programming in C
Chapter 13 Control Structures
Control Structure គោលបំណងនៃមេរៀន អ្វីជា Control structure ?
Presentation transcript:

Tutorial #4 Summer 2005

If int main() { if (30) printf("this will ___ be printed"); } return 0;

If int main() { if (0) printf("this will ___ be printed"); } return 0;

If int main() { if (-24) printf("this will ___ be printed"); } return 0;

If int main() { int digit; … if ((digit == 0) || ((digit > 3) && (digit != 7))) …. }

if … else if … else #include <stdio.h> int main() { char c; if ((c = getchar() != ‘\n’) { if ((c >= ‘a’ && c <= ‘z’) || (c >= ‘A’ && c<= ‘Z’) printf(“A letter\n”); } else printf(“not a letter”); printf(“End of line”);

if … else if … else #include <stdio.h> int main() { char c; if ((c = getchar() != ‘\n’) if ((c >= ‘a’ && c <= ‘z’) || (c >= ‘A’ && c<= ‘Z’) printf(“A letter\n”); else printf(“not a letter”); printf(“End of line”); }

if … else if … else #include <stdio.h> int main() { char c; if ((c = getchar() != ‘\n’) if ((c >= ‘a’ && c <= ‘z’) || (c >= ‘A’ && c<= ‘Z’) printf(“A letter\n”); else printf(“not a letter”); }

if … else if … else #include <stdio.h> int main() { char c; if ((c = getchar() != ‘\n’) if ((c >= ‘a’ && c <= ‘z’) || (c >= ‘A’ && c<= ‘Z’) printf(“A letter\n”); else printf(“End of line”); }

if … else if … else #include <stdio.h> int main() { char c; if ((c = getchar() != ‘\n’) if ((c >= ‘a’ && c <= ‘z’) || (c >= ‘A’ && c<= ‘Z’) printf(“A letter\n”); else; else printf(“End of line”); }

if … else if … else #include <stdio.h> int main() { int a = 2, b = 1; if (a > 2 || b < 3) printf(“1”); else printf(“2”); if (a > 2) printf(“1”); else if (b < 3) else printf(“2”); }

if … else if … else #include <stdio.h> int main() { int a = 2, b = 1; if (2 < a < 3) printf(“2 < a < 3”); }

if … else if … else #include <stdio.h> int main() { int color; scanf(“%d”, &color); if (color == 1) printf(“red\n”); else if (color == 2) printf(“yellow\n”); else if (color == 3) printf(“blue\n”); return 0; }

Switch #include <stdio.h> int main() case 2: { int color; scanf(“%d”, &color); switch (color) case 1: printf(“red\n”); break; case 2: printf(“yellow\n”); break; case 3: printf(“blue\n”); } return 0;

Switch #include <stdio.h> case 2: int main() printf(“yellow\n”); { int color; scanf(“%d”, &color); switch (color) case 1: printf(“red\n”); break; case 2: printf(“yellow\n”); break; case 3: printf(“blue\n”); default: printf(“No color\n”); } return 0;

Switch #include <stdio.h> int main() { int color; scanf(“%d”, &color); switch (color) case 1: printf(“red\n”); case 2: printf(“yellow\n”); case 3: printf(“blue\n”); } return 0;

Operators… #include <stdio.h> #include <stdio.h> int main() { int a, b; scanf(“%d%d”, &a, &b); if (a == 0 || b/a > 1) ... return 0; } #include <stdio.h> int main() { int a, b; scanf(“%d%d”, &a, &b); if (b/a > 1 || a == 0) ... return 0; }

Operators… #include <stdio.h> int main() { int a; … if (a++) printf("a was not zero"); else printf("a is not zero"); return 0; }

Operators… #include <stdio.h> int main() { int x, y; scanf(“%d %d”, &x, &y); if ((x = y) != 0) ... return 0; }

Operators… #include <stdio.h> int main() { int x = 2, y, z; return 0; }

Operators… int x = 2, y = 1, z = 0; x = x && y || z; #include <stdio.h> int main() { int x = 2, y = 1, z = 0; x = x && y || z; printf(“%d\n”, x); printf(“%d\n”, x || !y && z); x = y = 1; z = x++ -1 printf(“%d\n”, z); z += -x++ + ++y; printf(“%d\n”, x); z = x / ++x; return 0; }

scanf double age, height; int res; printf("enter your age and height"); res = scanf("%lf %lf", &age, &height); if (res != 2) { printf(“scanf failed”); return -1; }

?: char ch; int x; … if (ch >= ‘a’ && ch <= ‘z’) x = 8; else

?: char ch; int x; … x = ((ch >= ‘a’ && ch <= ‘z’) ? 8 : 7);

?: char ch; int x; … (ch >= ‘a’ && ch <= ‘z’) ? x = 8 : x = 7;

while x = -2; while (x < 0) { printf("x is still negative :(\n"); x = x + 1; } printf("x is no longer negative.\n");

while #include <stdio.h> int main() { char ch = ‘a’; while (ch <= ‘z’) printf(“%d: %c\n”, ch, ch); ch++; }

While loops double age; printf("enter your age: "); scanf("%lf", &age); while (age < 18) { printf("Invalid age, please try again: "); } printf("Welcome to C-Stuff.com, the"); printf("#1 adult site on the web!\n");

While Loop #include <stdio.h> int main() { int n = 3; while (n) printf(“%d\n”, n--); return 0; }

While Loop #include <stdio.h> int main() { int n = -3; while (n) printf(“%d\n”, n++); return 0; }

While Loop #include <stdio.h> int main() { char ch; ch = getchar(); while (ch == ‘ ‘ || ch == ‘\n’) return 0; }

For Loop #include <stdio.h> int main() { int secs; } for (secs = 2; secs > 0; secs--) printf(“%d seconds!\n”, secs); printf(“The End!”); return 0 ; }

for #include <stdio.h> int main() { char ch; for (ch = ‘a’; ch <= ‘z’; ++ch) printf(“%d: %c\n”, ch, ch); }

For Loop #include <stdio.h> int main() { int i; } for (i = 0; i < 60; i += 10) printf(“%d \n”, i); return 0; }

For Loop #include <stdio.h> int main() { int i; } for (i = 0; i < 60; i += 10) printf(“%d \n”, i); return 0; } ;

For Loop #include <stdio.h> #include <stdio.h> int main() { int count = 1; for ( ; count <= 5; ++count) printf(“%d \n”, count); return 0; } #include <stdio.h> int main() { int count = 1; while (count <= 5) printf(“%d \n”, count); ++count; } return 0;