w2-2-one

w2-2-one - 198:211 Computer Architecture Lecture 4 Fall...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
198:211 Computer Architecture 198:211 Computer Architecture z Topics: z Control Statements z Advanced Types z Pointers z Arrays Lecture 4 Fall 2009
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Control Structures z Same control structures as Java. Same syntax z Conditional z if z if-else z switch z Iteration z while z for z do-while z also has the break and continue expressions.
Background image of page 2
Sequencing and grouping z Statement_1 ; statement_2; statement _n; z executes each of the statements in turn z a semicolon after every statement z not required after a {. ..} block
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The if statement z Same as Java if (condition 1 ) {statements 1 } else if (condition 2 ) {statements 2 } else if (condition n-1 ) {statements n-1 }| else {statements n } z evaluates statements until find one with non-zero result z executes corresponding statements #include <stdio.h> main(){ inti=3,j=5; if(i<j) printf("i<j\n"); } #include<stdio.h> main() { int i=5, j =3; if (i > j){ i=i+1; printf("%d",i); } else j=j+1; } Examples from: http://www.java2s.com/Tutorial/C/
Background image of page 4
The switch statement z Allows choice based on a single value switch(expression) { case const1: statements1; break; case const2: statements2; break; default: statementsn; } z Effect: evaluates integer expression z looks for case with matching value z executes corresponding statements (or defaults)
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The switch statement int fork; switch(fork) { case 1: printf(“take left’’); case 2: printf(“take right”); break; case 3: printf(“make U turn”); break; default: printf(“go straight”); }
Background image of page 6
Repetition z C has several control structures for repetition one or more times, while condition is 0 do {. ..} while(c) zero or more times, with initialization and update for (start; cond; update) zero or more times, while c 0 Remember: True means any non-zero value while(c) {} repeats an action. .. Statement
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
While and for loop z #include <stdio.h> main(){ int i,n = 5; for( i=0;i<n;i=i+1){ printf("the numbers are %d \n",i); } } #include<stdio.h> main(){ int i = 0; while (i<5){ printf(" the value of i is %d\n", i); i=i+1; } } Examples from: http://www.java2s.com/Tutorial/C/
Background image of page 8
The break statement z break allows early exit from one loop level for (init; condition; next) { statements1; if (condition2) break; statements2; } #include<stdio.h> main(){ int i = 0; while (1) { i=i+1 ; printf(" the value of i is %d\n",i); if (i>5) { break; } } }
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The continue statement z continue skips to next iteration, ignoring rest of loop body z #include<stdio.h> main(){ int i; for(i = -10; i < 11; i++){ if (i<0) { continue; } printf(" the value of i is %d\n", i); } }
Background image of page 10
Structures in C z A struct is a mechanism for grouping together related data items of different types . z Recall that an array groups items of a single type. z Example: We want to represent an airborne aircraft: z int altitude; int longitude; int latitude; int heading; double airSpeed; z We can use a struct to group these data together for each plane.
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Defining a Struct z We first need to define a new type for the compiler and tell it what our struct looks like.
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/24/2011 for the course CS 211 taught by Professor Chakraborty during the Spring '08 term at Rutgers.

Page1 / 44

w2-2-one - 198:211 Computer Architecture Lecture 4 Fall...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online